Andrew Mercer

Ad-hoc fact queries

ansible localhost -m setup -a 'filter=ansible_os_family'
ansible -i ~/ansible/inventories/[ hostname ] localhost -m setup -a 'filter=ansible_os_family'

Module specific

Copy

ansible all -m copy -a "src=[ hostname ] dest=/tmp/[ hostname ]"

Ping

ansible all -m ping

User

ansible all -s -m user -a "name=[user_name]"

Hosts

Test connection to a host

ansible -i ~/ansible/inventories/cluster_hosts.yaml [ host_name ] -m ping

e.g.

ansible -i ~/ansible/inventories/cluster_hosts.yaml lab -m ping

Collections

Create a new collection

cd ~/ansible/collections/ansible_collections && \
ansible-galaxy collection init [ namespace ].[ collection ]

e.g.

cd ~/ansible/collections/ansible_collections && \
ansible-galaxy collection init [ hostname ]
cd ~/ansible/collections/ansible_collections/containers/docker && \
tree
.
├── docs
├── [ hostname ]
├── install
   ├── defaults
      └── [ hostname ]
   ├── files
   ├── handlers
      └── [ hostname ]
   ├── meta
      └── [ hostname ]
   ├── [ hostname ]
   ├── tasks
      └── [ hostname ]
   ├── templates
   ├── tests
      ├── inventory
      └── [ hostname ]
   └── vars
       └── [ hostname ]
├── meta
   └── [ hostname ]
├── plugins
   └── [ hostname ]
├── [ hostname ]
└── roles
    └── install
        ├── defaults
           └── [ hostname ]
        ├── files
        ├── handlers
           └── [ hostname ]
        ├── meta
           └── [ hostname ]
        ├── [ hostname ]
        ├── tasks
           ├── [ hostname ]
           ├── [ hostname ]
           ├── [ hostname ]
           └── [ hostname ]
        ├── templates
        ├── tests
           ├── inventory
           └── [ hostname ]
        └── vars
            └── [ hostname ]

Create a new role within a collection

~/ansible/collections/ansible_collections/[ namespace ].[ collection ].roles && \
ansible-galaxy role init [ role ]

e.g.

cd ~/ansible/collections/ansible_collections/containers/docker/ && \
> ansible-galaxy role init install
cd ~/ansible/collections/ansible_collections/containers/docker/roles/install && \
tree
.
├── defaults
   └── [ hostname ]
├── files
├── handlers
   └── [ hostname ]
├── meta
   └── [ hostname ]
├── [ hostname ]
├── tasks
   ├── [ hostname ]
   ├── [ hostname ]
   ├── [ hostname ]
   └── [ hostname ]
├── templates
├── tests
   ├── inventory
   └── [ hostname ]
└── vars
    └── [ hostname ]

Playbooks and roles

Playbooks

  • https://[ hostname ]/ansible/latest/user_guide/playbooks_intro.html

Playbooks are a completely different way to use ansible than in ad-hoc task execution mode, and are particularly powerful.

Roles

  • https://[ hostname ]/ansible/latest/user_guide/playbooks_reuse_roles.html

Roles are ways of automatically loading certain vars_files, tasks, and handlers based on a known file structure. Grouping content by roles also allows easy sharing of roles with other users.
Roles expect files to be in certain directory names. Roles must include at least one of these directories, however it is perfectly fine to exclude any which are not being used. When in use, each directory must contain a [ hostname ] file, which contains the relevant content:

tasks - contains the main list of tasks to be executed by the role.
handlers - contains handlers, which may be used by this role or even anywhere outside this role.
defaults - default variables for the role (see Using Variables for more information).
vars - other variables for the role (see Using Variables for more information).
files - contains files which can be deployed via this role.
templates - contains templates which can be deployed via this role.
meta - defines some meta data for this role. See below for more details.

Re-usable roles

  • etc_hosts # populates /etc/hosts file with cluster members
  • external_ip # retrieves external ip

Handlers

Global handlers workaround

Write reusable handlers that all roles can use

Create a role named handlers

  • roles/handlers

Include the handlers role in the role's dependencies

  • roles/[ my_role ]/meta/[ hostname ]
dependencies:
  - handlers

Simply write a notify in your role's task

For example:

- name: Download systemd service unit file
  [ hostname ].get_url:
    dest: /etc/systemd/system
    url: https://[ hostname ]/containerd/containerd/main/[ hostname ]
  **notify: systemd daemon-reload**

Variables (vars)

Global variables

  • ~/ansible/group_vars/all/[ hostname ]

List all variables

cd ~/ansible && \
ansible-inventory -i inventories/[ hostname ] --list --yaml

Inventories

Controlling execution flow in Ansible

Conditionals

  • https://[ hostname ]/ansible/latest/playbook_guide/playbooks_conditionals.html

Loops

  • https://[ hostname ]/ansible/latest/playbook_guide/playbooks_loops.html

Delegation

  • https://[ hostname ]/ansible/latest/playbook_guide/playbooks_delegation.html

Variables

Error handling

Debugging

  • https://[ hostname ]/projects/ansible/latest/playbook_guide/playbooks_debugger.html
  • https://[ hostname ]/blog/ansible-debug

[ hostname ]

Debug strategy

Debug using environment variables

Verbose output

-v: Displays basic debugging information, such as task names and results
-vv: Includes more detailed outputs, like variable values
-vvv: Shows additional debugging data, such as task-level operations
-vvvv: Enables connection debugging, providing a deep dive into network communication

Check mode (dry-run)

ansible-playbook [ hostname ] --check|-C

Diff mode (show changes)

ansible-playbook [ hostname ] --diff|-D

Start at a specific task

Skips all tasks prior to the specified one

ansible-playbook [ hostname ] --start-at-task="Task Name"

Step through tasks

Prompts you before running each task

ansible-playbook [ hostname ] --step

Syntax check

Option to quickly validate the syntax of your Ansible playbook without running it

ansible-playbook [ hostname ] --syntax-check

Debug mode

Building custom modules

  • todo