Ad-hoc fact queries¶
ansible localhost -m setup -a 'filter=ansible_os_family'
ansible -i ~/ansible/inventories/inventory.yml localhost -m setup -a 'filter=ansible_os_family'
Module specific¶
Copy¶
ansible all -m copy -a "src=file.txt dest=/tmp/file.txt"
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 containers.docker
cd ~/ansible/collections/ansible_collections/containers/docker && \
tree
.
├── docs
├── galaxy.yml
├── install
│ ├── defaults
│ │ └── main.yml
│ ├── files
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ │ └── main.yml
│ ├── README.md
│ ├── tasks
│ │ └── main.yml
│ ├── templates
│ ├── tests
│ │ ├── inventory
│ │ └── test.yml
│ └── vars
│ └── main.yml
├── meta
│ └── runtime.yml
├── plugins
│ └── README.md
├── README.md
└── roles
└── install
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ ├── debian.yml
│ ├── fedora.yml
│ ├── main.yml
│ └── rhel.yml
├── templates
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml
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
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ ├── debian.yml
│ ├── fedora.yml
│ ├── main.yml
│ └── rhel.yml
├── templates
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml
Playbooks and roles¶
Playbooks¶
- https://docs.ansible.com/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://docs.ansible.com/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 main.yml 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/main.yml
dependencies:
- handlers
Simply write a notify in your role's task¶
For example:
- name: Download systemd service unit file
ansible.builtin.get_url:
dest: /etc/systemd/system
url: https://raw.githubusercontent.com/containerd/containerd/main/containerd.service
**notify: systemd daemon-reload**
Variables (vars)¶
Global variables¶
- ~/ansible/group_vars/all/main.yml
List all variables¶
cd ~/ansible && \
ansible-inventory -i inventories/inventory.yml --list --yaml
Inventories¶
Controlling execution flow in Ansible¶
Conditionals¶
- https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_conditionals.html
Loops¶
- https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_loops.html
Delegation¶
- https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_delegation.html
Variables¶
Error handling¶
Debugging¶
- https://docs.ansible.com/projects/ansible/latest/playbook_guide/playbooks_debugger.html
- https://spacelift.io/blog/ansible-debug
ansible.builtin.debug¶
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 playbook.yml --check|-C
Diff mode (show changes)¶
ansible-playbook playbook.yml --diff|-D
Start at a specific task¶
Skips all tasks prior to the specified one
ansible-playbook playbook.yml --start-at-task="Task Name"
Step through tasks¶
Prompts you before running each task
ansible-playbook playbook.yml --step
Syntax check¶
Option to quickly validate the syntax of your Ansible playbook without running it
ansible-playbook playbook.yml --syntax-check
Debug mode¶
Building custom modules¶
- todo