An Ansible inventory is a structured list of hosts or nodes that Ansible can manage, providing a foundation for orchestrating automation tasks across a network. This inventory file typically contains information about the hosts’ IP addresses, connection details, and grouping, allowing users to organize and categorize systems based on their roles, functions, or other criteria.
Ansible Inventories can be static or dynamic, enabling flexibility in managing diverse infrastructure environments. This inventory concept is central to Ansible, as it defines the scope and targets for automation, allowing users to specify where tasks and playbooks should be applied within their IT infrastructure.
Beginners generally start with static inventory files. They can be in INI or YAML format as well.
The default location of the Ansible inventory is the /etc/ansible/hosts
file when Ansible is installed by the OS package manager. Other, different inventory files can be configured in a local configuration file or in the command line interface, with the -i
option.
INI file format
The basic inventory file format looks like the following example:
[webservers]
192.168.1.91
192.168.1.92
[dbservers]
192.168.1.93
The inventory file contains two groups, the webservers
and the dbservers
.
YAML file format
If you choose to use a YAML inventory format, then the file will look like this example:
---
webservers:
hosts:
192.168.1.91:
192.168.1.92:
dbservers:
hosts:
192.168.1.93:
The above YAML file is the same as the previous INI format with two groups and three managed hosts.
Default groups
Ansible will automatically create two default groups:
- all – contains every host
- ungrouped – hosts that don’t have any other group aside “all”
Every host will always belong to at least 2 groups (all
and ungrouped
or all
and some other group you create like the webservers
in the example).
Inventory commands
You can interact with your inventory with the ansible-inventory
command. Providing the -i
option to the command following with the inventory file can read an INI or YAML inventory.
For example:
(.venv) [tmolnar@ansible03 ansible]$ ansible-inventory -i inventory.ini --graph
@all:
|--@ungrouped:
|--@webservers:
| |--192.168.1.91
| |--192.168.1.92
|--@dbservers:
| |--192.168.1.93
The above example shows that the two managed hosts are part of the webservers
group and one is part of the dbservers
group. All hosts are part of the all
group.
The --graph
option created a visually pleasing representation of our managed host structure.
If you use the --list
option instead, you will get a JSON representation with more information like host and group variables.
(.venv) [tmolnar@ansible03 ansible]$ ansible-inventory -i inventory.ini --list
{
"_meta": {
"hostvars": {}
},
"all": {
"children": [
"ungrouped",
"webservers",
"dbservers"
]
},
"dbservers": {
"hosts": [
"192.168.1.93"
]
},
"webservers": {
"hosts": [
"192.168.1.91",
"192.168.1.92"
]
}
}
There is another practical option to use, the --yaml
using with the --list
will show the output in YAML format instead of the default JSON.
(.venv) [tmolnar@ansible03 ansible]$ ansible-inventory -i inventory.ini --list --yaml
all:
children:
dbservers:
hosts:
192.168.1.93: {}
webservers:
hosts:
192.168.1.91: {}
192.168.1.92: {}
With this command and argument mix you can get a human readable representation of your inventory.
Behavioral inventory parameters
Connecting to hosts: behavioral inventory parameters
The behavioral inventory parameters control the way how Ansible interacts with the managed hosts. The list of these parameters can be found in the link above.
It is possible to use alias names for hosts, in this case you have to use the ansible_host
parameter for providing the IP of the remote hosts.
[webservers]
deb_ans ansible_host=192.168.1.91
ubu_ans ansible_host=192.168.1.92
[dbservers]
roc_ans ansible_host=192.168.1.93
Now you can use the alias in your Ansible commands instead of the IP addresses.
(.venv) [tmolnar@ansible03 ansible]$ ansible -i inventory.ini -m command -a 'uname -a' roc_ans --ask-pass
SSH password:
roc_ans | CHANGED | rc=0 >>
Linux ansible03.tomsitcafe.com 5.14.0-362.24.1.el9_3.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Mar 13 17:33:16 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
You can see that we used the roc_ans
alias in the command instead of the IP address of the node.
There are other behavioral parameters that can be used to configure the default users, ports, SSH keys and even login and privilege escalation methods in the inventory files.
(.venv) [tmolnar@ansible03 ansible]$ cat parameters.yaml
---
dbservers:
hosts:
roc_ans:
ansible_host: 192.168.1.93
webservers:
hosts:
deb_ans:
ansible_host: 192.168.1.91
ansible_user: ansible
ansible_become_method: ansible.builtin.su
ansible_ssh_private_key_file: /home/tmolnar/.ssh/id_rsa
ubu_ans:
ansible_host: 192.168.1.92
In the example above there are other parameters setting up the way Ansible interacts with the deb_ans
host.
Read the documentation carefully and use the variables that apply to your system. There are many more things that you can configure in an Ansible inventory, but that’s a topic for my upcoming Ansible Advanced course.
You are ready to learn about Ansible Playbooks!
If you want to discuss the topic with other technology-minded people, join my Discord: https://discord.gg/YbSYGsQYES
Now we have an IRC channel as well: irc.libera.chat / #tomsitcafe
One thought on “Ansible Basics 04: The Inventories”