Ansible redhat ex407 (Red Hat Certified Specialist in Ansible Automation) - capabdou/guide-devops GitHub Wiki

Preparing for the test

  • Tools for preparation
  • Quizzes
  • Flash Cards
  • Study Guide
  • Learning Activites
  • Interactive Diagram

A Brief Word on YAML

What is YAML and Why Does it Matter?

  • YAML is a markup language used for formatting data
  • YAML is generally considered easier for humans to read
  • Ansible Playbooks use YAML syntax

Basic YAML Syntax

  • YAML is composed of key-value pairs, lists, and dictionaries
  • Files items are designated with a single hyphen and a space
  • Each list item should have the same indentation
  • Dictionaries are designated with a colon and a space followed by indented key-value pairs

Multiple Line Values

  • It is frequently useful when working on Ansible playbooks to format input with line breaks
  • YAML has a means of letting us do this
  • The pipe and riht angle bracket (| ans >) may be used to allow for line breaks within YAML
  • Pipe will take each line break as part of the input in the data that follows it
  • The right-angle bracket will ignire line breaks in the data that follows it
  • A typical use case is breaking up parameters for readability

Quotes and When to Use Them

  • YAM has a relatively small set of special characters: [] {} : > |
  • You must use double quotes to espace a special character
  • Technically, you only need to escape a colon if it is followed by a space
  • You will frequently need to escape curly braces {} as they are special to both YAML and Ansible:

Ansible variables are formatted "{{variable_name}}"- the double quotes are required.

This will be discussed more when we go overvariables in Ansible

  • Boolean values auto-convert in YAML
  • If you want a literal "YES", you must use quotes
  • Floating Point Numbers are taken as numeric unless quoted

Understanding core components of Ansible

Overview

  • Inventories
  • Modules
  • Variable
  • Facts
  • Plays
  • Playbooks
  • Configuration Files

Inventories

  • Inventories are how Ansible can locate and run against multiple systems
  • You can think of an inventory as a list of hosts
  • By default, Ansible uses /etc/ansible/hosts as its inventory, but this is configurable
  • Inventories may be formatted as an INI file or as a yaml file. Note: file formats ar interchangeable
  • Inventory files may simply consist of a list of hostnames but can be musch more robust
  • It is also possible to define groups of hosts, host or group level variables, and groups of groups within the inventory
  • There are a number of variables that may be used within the inventory to control how ansible connects to an interacts with target hosts

$ vim -R /etc/ansible/hosts

[labservers]

scoldham2.mylabserver.com

$ cd ansible

vim inv.ini

scoldham2.mylabserver.com

[httpd]

scoldham3.mylabserver.com

scoldham4.mylabserver.com

[labservers]

scoldham[2:6].mylabserver.com

ansible scoldham2.mylabserver.com -m ping

scoldham2.mylabserver.com | SUCCESS => { "changed": false, "ping":"pong"}

ansible all -m ping

scoldham2.mylabserver.com | SUCCESS => { "changed": false, "ping":"pong"}

ansible -inv.ini httpd -m ping

scoldham3.mylabserver.com | SUCCESS => { "changed": false, "ping":"pong"}

scoldham4.mylabserver.com | SUCCESS => { "changed": false, "ping":"pong"}

Modules

  • Modules are essentially tools for particular tasks
  • Modules can take (and usually do) take parameters
  • They return JSON
  • Can run from the command line or within a playbook
  • There are a significant number of modules for many kinds of work
  • Custom modules can be written

Variables

  • Variable names should be letters, numbers, and underscores
  • Variables should always start with a letter
  • Can be scoped by a group, host, or even in a playbook
  • Typically used for configuration values and various parameters
  • Variables can also be used to store the return value of executed commands
  • Ansible variables may also be dictionaries
  • There are a number of predefiner variable used by ansible

Facts

  • Facts provide certain information about a given target host
  • Facts are discovered by Ansible automatically when it reaches out to a host
  • Fact gathering may be disabled
  • Facts may be cached between playbook executions, but this is not default behavior

Plays and Playbooks

  • The goal of a play is to map a group of hosts to some well-defined roles
  • A play may use one or more modules to achieve a desired end state on a group of hosts
  • A playbook is a series of plays
  • A playbook may deploy new web servers, install a new application to existing application servers, and run SQL against some database servers to support the new application

Configuration Files

  • Several possible locations (in order processed)

    • ANSIBLE_CONFIG ( an environment variable)

    • ansible.cfg (in the current directory)

    • .ansible.cfg (in the home directory)

    • /etc/ansible/ansible.cfg

  • Configuration can also be set in environment variables

  • Some commonly used settings:

    • ansible_managed
    • forks
    • inventory

Ad-Hoc Ansible Commands

overview

  • What is an ad-hoc command in Ansible?
  • Use cases for ad-hoc commands
  • Ad-hoc vs Playbook
  • Ansible command syntax
  • Common modules

What is an ad-hoc command in Ansible?

  • You can run ansible either ad-hoc or as a playbook
  • Both methods have the same capabilities
  • Ad-hoc commands are effectively one-liners

Use cases for Ad-hoc

  • Operational commands

    • Checking log contents
    • Daemon control
    • Process management
  • Informational commands

    • check installed software
    • check system properties
    • Gather system performance information (CPU, disk space, memory use)
  • research

    • work with unfamiliar modules on test systems

Ad-hoc vs Playbook

image

image

image

image

image

Demonstration

image

image

image

image

image

image

image

image

image

image

image

image

image

image