Chapter 3: Implementing an Ansible Playbook

Ansible playbooks cheat sheet

Everything from chapter 3 that you should be able to do without notes, plus the commands and file templates worth memorising.

The chapter in nine sentences

Templates to memorise

ansible.cfg

[defaults]
inventory = ./inventory
remote_user = devops
ask_pass = false

[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false

ansible-navigator.yml

---
ansible-navigator:
  execution-environment:
    image: utility.lab.example.com/ee-supported-rhel8:latest   # home lab: use  enabled: false  instead of image/pull
    pull:
      policy: missing
  playbook-artifact:
    enable: false

inventory

[web]
server[a:b].lab.example.com

[db]
serverc.lab.example.com

[prod:children]
web
db

site.yml

---
- name: Web server is configured
  hosts: web
  become: true
  tasks:
    - name: httpd is installed
      ansible.builtin.dnf:
        name: httpd
        state: present

    - name: httpd is started and enabled
      ansible.builtin.service:
        name: httpd
        state: started
        enabled: true

- name: Web server responds
  hosts: localhost
  become: false
  tasks:
    - name: Home page returns 200
      ansible.builtin.uri:
        url: http://servera.lab.example.com
        status_code: 200

Command cheat sheet

GoalCommand
Show one group as a treeansible-navigator inventory -m stdout --graph GROUP
Dump the whole inventory as JSONansible-navigator inventory -m stdout --list
Is this host in the inventory?ansible-navigator inventory -m stdout --host HOST
Use a different inventoryadd -i PATH to any command
See the effective configurationansible-navigator config
List modules in the EEansible-navigator doc -l -m stdout
Read a module's docsansible-navigator doc MODULE -m stdout
Browse collectionsansible-navigator collections
Check syntaxansible-navigator run -m stdout site.yml --syntax-check
Dry runansible-navigator run -m stdout site.yml --check
Runansible-navigator run -m stdout site.yml
Run with more detailadd -v … -vvvv
Prompt for SSH passwordadd --ask-pass (artifacts disabled, -m stdout)
FileLookup order (first found wins)
ansible.cfg$ANSIBLE_CONFIG → ./ansible.cfg → ~/.ansible.cfg → /etc/ansible/ansible.cfg
ansible-navigator.yml$ANSIBLE_NAVIGATOR_CONFIG → ./ansible-navigator.yml → ~/.ansible-navigator.yml
Connection useransible_user → play remote_user → ansible.cfg remote_user → root in the EE
Which two groups always exist in an inventory?
`all` (every host) and `ungrouped` (hosts in no other group).
How do you make a group of groups?
A section named `[parent:children]` listing the child **group** names.
What does `server[01:03]` expand to?
server01, server02, server03. Inclusive, and leading zeros are kept.
Where does Ansible look for ansible.cfg first?
The file named by `ANSIBLE_CONFIG`, then `./ansible.cfg` in the current directory.
Are settings merged across several ansible.cfg files?
No. Only the **first file found** is used.
What is the difference between remote_user and become_user?
`remote_user` is who Ansible **logs in** as; `become_user` is who it **switches to** afterwards (usually root).
Two YAML indentation rules?
Same level → same indentation. Children → indented more than their parent. Spaces only.
What is an FQCN?
Fully qualified collection name: `namespace.collection.module`, e.g. `ansible.posix.firewalld`.
--syntax-check versus --check?
`--syntax-check` only parses the YAML. `--check` connects to hosts and reports what would change.
How do you make a command task idempotent?
Add `creates:` or `removes:`, or better, use a purpose-built module.
Which module works without Python on the host?
`ansible.builtin.raw`
What does hosts: localhost mean under ansible-navigator?
The tasks run inside the **execution environment container**.