Creating Ansible playbooks
As we discussed, an Ansible playbook is a YAML file. The following example is a simple playbook that contains instructions to update the Apt package manager class on the machine called by default in our inventory:
--- - hosts: default tasks: - name: update apt cache apt: update_cache=yes
We can run this playbook by running the ansible-playbook our-playbook.yml -i our-inventory-file
command. Ansible will then look up that this playbook is to be applied to the default machine, the default machine's details, connect to it, and if appropriate, run the command. We will walk through the execution process shortly.
Tasks are executed in the order that they appear within the playbook. However, we have the option to call other tasks to be run later once an action is completed, through the use of handlers, which we will discuss shortly.
Note
Because playbooks are written in YAML, the format and spacing/indentation in these files is critical. Incorrect indentation...