Chapter 03 · Ansible Playbooks
Ansible Playbooks
How it works with SSH
Ansible 'pushes' actions over SSH, often by running Python modules on the target.
When you run ansible -m ping, you are already performing playbook-style automation... without a playbook.
Basic commands
ansible: ad hoc (one command, one module)ansible-playbook: runs a YAML playbook- Useful options:
-i,-l,-v/-vvv,--check,--diff,--tags
Modules and tasks
Ad hoc example:
ansible -i inventory.ini web -b -m apt -a "name=nginx state=present update_cache=yes"
In a playbook:
- name: Install nginx
hosts: web
become: true
tasks:
- name: nginx installed
ansible.builtin.apt:
name: nginx
state: present
update_cache: true
Writing playbooks and using YAML
YAML: indentation, lists using
-, dictionaries key: value. Your main enemy: one extra space.- A playbook = a list of plays.
- A play =
hosts+tasks(+ variables, handlers, etc.).
Variables, loops and conditions
- name: Create users
ansible.builtin.user:
name: "{{ item }}"
state: present
loop:
- alice
- bob
- charlie
- name: Enable a site if the variable is set
ansible.builtin.command: a2ensite monsite
when: enable_site | default(false)
Roles and inclusion
Roles are automation 'packages'. You use them to: reuse, organise and version your automation.
ansible-galaxy init roles/webserver
Best practices
- Prefer Ansible modules over
shell/command(idempotence, check mode and security). - Use
handlersto restart a service only when necessary. - Limit
becometo tasks that need it. - Name your tasks (otherwise debugging becomes painful).
- Variables: clear, documented, and without 17 layers of Jinja 'for style'.
Practical exercise: your first 'web' playbook
- Create a directory called
ansible-lab. - Create
site.ymlthat installs nginx, deploys a page and starts the service. - Add a 'restart nginx' handler.
- Run in check mode:
ansible-playbook -i inventory.ini site.yml --check --diff. (don't be aware of error code, it's irrelevant) - Run in execution mode:
ansible-playbook -i inventory.ini site.yml. (Now, it' really applied)
Minimal example
- name: Web baseline
hosts: web
become: true
vars:
index_msg: "Hello from Ansible"
tasks:
- name: nginx installed
ansible.builtin.apt:
name: nginx
state: present
update_cache: true
- name: Home page
ansible.builtin.copy:
dest: /var/www/html/index.html
content: "<h1>{{ index_msg }}</h1>"
notify: restart nginx
- name: nginx service started
ansible.builtin.service:
name: nginx
state: started
enabled: true
handlers:
- name: restart nginx
ansible.builtin.service:
name: nginx
state: restarted
Multiple-choice quiz
1) Which command runs a playbook?
- A. ansible-playbook
- B. ansible-compile
- C. ansible-puppet
2) Why avoid
shell when a module is available?
- A. Only for readability
- B. For idempotence, check mode and security
- C. Because Bash is banned by the UN
3) What are handlers mainly used for?
- A. Rebooting after every task
- B. Responding to a change (restarting a service, etc.)
- C. Managing SSH keys
Answer key
1) A · 2) B · 3) B