Playbook

Playbook

  • A playbook is saved using the standard file extension .yml.

  • Indentation with space character indicates the structure of the data in the file.

  • YAML does not place strict requirements on how many spaces are used for the indentation, but there are two basic rules.

    • Data elements at the same level in the hierarchy (such as items in the same list) must have the same indentation.

    • Items that are children of another item must be indented more than their parents.

  • Only the space character can be used for indentation. Tab characters are not allowed.

Limiting Playbook Execution

  • A play has hosts: webservers in its definition

  • datacenter2 and webservers are both groups in the inventory

  • The play will only run on hosts that are in both webservers and datacenter2:

    • ansible-playbook site.yml --limit datacenter2

  • If retry file is enabled, to re-run failed hosts:

    • ansible-playbook site.yml --limit @site.retry

Validating a Playbook

  • Verify that playbook can be ingested by ansible

  • ansible-playbook --syntax-check webserver.yml

Dry Run

  • Report what changes would have occurred if the playbook were executed, but does not make any actual changes to managed hosts.

    • ansible-playbook -C webserver.yml

Variable

  • Global

    • The value is set for all hosts.

    • Example: extra variables you set in the job template

  • Host

    • The value is set for a particular host (or group).

    • Examples: variables set for a host in the inventory or a host_vars directory, gathered facts

  • Play

    • The value is set for all hosts in the context of the current play.

    • Examples: vars directives in a play, include_vars tasks, and so on

Variable Preference

  • If a variable is defined at more than one level, the level with the highest precedence wins.

  • A narrow scope generally takes precedence over a wider scope.

  • Variables that you define in an inventory are overridden by variables that you define in the playbook.

  • Variables defined in a playbook are overridden by “extra variables” defined on the command line with the -e option.

  • https://docs.ansible.com/ansible/latest/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable

Examples

  • When referencing one variable as another variable’s value, and the curly braces start the value, quotes must be used around the value.

  • This prevents Ansible for interpreting the variable reference as starting a YAML dictionary.

  • The following message appears if the quotes are missing:

Host and Group Variables

  • Host variables apply to a specific host.

  • Group variables apply to all hosts in a host group or in a group of host groups.

  • Host variables take precedence over group variables, but variables defined inside a play take precedence over both.

  • Host variables and group variables can be defined

    • In the inventory itself. In host_vars and group_vars directories in the same directory as the inventory.

    • In host_vars and group_vars directories in the same directory as the playbook. These are host and group based but have higher precedence than the inventory variables.

      • In the same directory as your playbook, create two directories, group_vars and host_vars.

        • To define group variables for the servers group, you would create a file named group_vars/servers.

        • In that file, set variables to values, using YAML syntax. (The example at right sets ansible_user to a string and newfiles to a list of values.)

        • To define host variables for a particular host, create a file with a name matching the host in the host_vars directory

Using password in Environment Variable

  • or store the same string in the inventory, group vars, host vars or even play vars.

Suppressing Output from a Task

  • Use: no_log: true

Task Iteration with Loops

When Loop is Inefficient

Inefficient

  • This will launch the yum module three times, once for each item in the loop which is inefficient

Efficient

  • The yum module can take a list of packages and install them all in one operation which will be faster

Run Tasks Conditionally

When Statements

  • Used to run task conditionally

    • When condition is met, the task runs

    • When condition is not met, the task is skipped

Some conditions

Operation
Example

Equal (value is a string)

ansible_machine=="x86_64"

Equal (value is numeric)

max_memory == 512

Less than

min_memory < 128

Greater than

min_memory > 256

Less than or equal to

min_memory <= 256

Greater than or equal to

min_memory >= 512

Not equal to

min_memory != 512

Variable exists

min_memory is defined

Variable does not exist

min_memory is not defined

Boolean variable is true. The values of 1, True, or yes evaluate to true.

memory_available

Boolean variable is false. The values of 0, False, or no evaluate to false.

not memory_available

First variable's value is present as a value in second variable's list

ansible_distribution in supported_distros

Constructing Conditions with Ansible Facts

Multiple Conditions

Or Condition

  • when: ansible_ditribution == "Redhat" or ansible_distribution == "Fedora"

And Condition

  • when: ansible_distribution_version == "7.5" and ansible_kernel == "3.10.0-327.el7.x86_64"

Complex Condition

Combining Loop and Conditional Tasks

Block

  • In playbooks, blocks are clauses that logically group tasks as a unit.

  • They can be used to control how tasks are executed.

  • For example, a block can have a when conditional applied to the entire block.

  • This means that all the tasks in the block will only run if the conditional is met.

  • In below example, the block groups two tasks that run only when ansible_distribution is RedHat

Block and Rescue: Recovering from Failure

  • Blocks can also be used to back out of a change if tasks in the block fail.

  • A block can have a set of tasks grouped in a rescue statement.

  • The tasks in the rescue statement only run if a task in the block fails.

  • Normally, the tasks in the rescue statement will recover the managed host from the failure. This is useful if the block exists because multiple tasks are needed to accomplish something.

Block, Rescue and Always

  • block also supports a third section, always

  • After the block runs, and the rescue if there was a failure in block, the tasks in always run

  • These tasks always run if the block runs at all, but they are subject to the conditional on the block

  • To summarize:

    • block: Defines the main tasks to run.

    • rescue: Defines the tasks to run if the tasks defined in the block clause fail.

    • always: Defines the tasks that will always run independently of the success or failure of tasks defined in the block and rescue clauses.

Reference

Last updated