Inventories
- Official Docs
- Inventory contains a list of servers/systems to connect to.
- Inventory can be written in two formats:
- INI format (
hostsfile) - YAML format
- INI format (
- Inventory allows us to group servers.
- By default, Ansible uses
allto represent all hosts in the inventory file. - The default inventory file location is
/etc/ansible/hosts.
Example 1 — Basic Inventory (No Groups)
INI format:
10.100.0.11
10.100.0.12
10.100.0.13
10.100.0.14
10.100.0.15
10.100.0.16
10.100.0.17
10.100.0.18
10.100.0.19
10.100.0.10
YAML format:
all:
hosts:
10.100.0.11:
10.100.0.12:
10.100.0.13:
10.100.0.14:
10.100.0.15:
10.100.0.16:
10.100.0.17:
10.100.0.18:
10.100.0.19:
10.100.0.10:
Example 2 — Inventory with Groups
INI format:
[webservers]
10.100.0.11
10.100.0.12
10.100.0.13
10.100.0.14
10.100.0.15
10.100.0.16
[dbservers]
10.100.0.17
10.100.0.18
10.100.0.19
10.100.0.10
YAML format:
all:
children:
webservers:
hosts:
10.100.0.11:
10.100.0.12:
10.100.0.13:
10.100.0.14:
10.100.0.15:
10.100.0.16:
dbservers:
hosts:
10.100.0.17:
10.100.0.18:
10.100.0.19:
10.100.0.10:
Example 3 — Range Pattern
[webservers]
www[01:06].example.com
Static vs Dynamic Inventories
- The above examples are static inventories.
- Inventories can also be dynamic, which means they point to:
- An executable (script) that generates a list of servers in a specific JSON format.
- Ansible plugins to fetch servers from cloud providers like AWS, Azure, GCP.
Ansible Debug Messages
Simple Playbook (without debug)
---
- name: sample playbook
hosts: all
become: yes
tasks:
- name: update packages and install tree
ansible.builtin.apt:
name: tree
update_cache: yes
state: present
Playbook with Explicit Debug Messages
Using the debug module:
---
- name: sample playbook
hosts: all
become: yes
tasks:
- ansible.builtin.debug:
msg: "Installing tree package"
- name: update packages and install tree
ansible.builtin.apt:
name: tree
update_cache: yes
state: present
- ansible.builtin.debug:
msg: "Installed tree package"
Ansible also supports printing verbose information — use the
-v,-vv,-vvv, or-vvvvflags withansible-playbookfor increasing levels of verbosity.
Bailout / Failing an Ansible Playbook
- To intentionally fail a playbook, use the
failmodule.
---
- name: sample bailout playbook
hosts: all
become: yes
tasks:
- ansible.builtin.fail:
msg: "Under development"
