Basic on Playbooks
– Plays are a collection of key value pairs. Keys in the same pair should share the same indentation. Playbooks are YAML files with the .yml file extension.
Indentation is super important and the indention should be configured in vim. The indentation is based on two spaces.
– For better vim editing with yaml add the following to the ~/.vimrc file
autocmd FileType yaml setlocal ai ts=2 sw=2 et
set cursorcolumn
Details on the autocmd
- #ai = auto indent
- #ts = tab stop
- #sw = Shift Width
- #et = expansetab
– Check syntax before starting
ansible-playbook playbook.yml –syntax-check
– Dry run the playbook
ansible-playbook playbook.yml -C
-v for verbose.. Up to 4v's
ansible-playbook playbook.yml "-vvvv"
– Starts with three dashes "—" and can end with three dots "…"
Sample playbook with name, hosts and tasks
#*****
---
- name: Playbook description
hosts: hosts
tasks:
- first
- second
- third
#*******
– Playbook tasks are ran in order
– Example playbook with task shown below
– "Print the output now" task details..
- Task uses the command module
- Registers the output of the module to the command_result variable
- Uses this variable to check the result and determine if it has failed
The second two register and failed_when are optional. These are best practice when working with the command module especially. Since the command module itself does not consider the command that is passed failing you will want to check the output for something to verify that the command succeeded or failed. You can also do something similar with changed_when for command and shell.
– Basic Tasks..
yum, copy, service, command, shell, etc……
ansible-doc task for more information on tasks
– Playbooks can have multiple plays
- name
hosts:
tasks:
- task1_play1
- name
hosts:
tasks:
- task1_play2