Skip to main content
RH254

Using Ansible Inventory Files

By September 2, 2019September 12th, 2022No Comments

Building Ansible Inventory

As we have already briefly seen, the hosts that want to manage are configured within the inventory file that we either configure in the ansible.cfg or specify with the option -i* There are different formats to the file the default /etc/ansible/hosts file in in INI format. We can list the supported formats by querying the configuration:


Query Enabled Inventory Formats
$ ansible-config dump | grep INVENTORY_ENABLED
INVENTORY_ENABLED(default) = ['host_list', 'script', 'auto', 'yaml', 'ini', 'toml']

Built-in Groups

So far, the inventory file we have is very basic with just the two IP addresses and now specific groups. Whilst I only have 2 systems this may work for me but we are always better to think of groups from the outset. Hosts can be in one or more groups and there are 2 built-in groups that we are presented with from the outset:

  • all
  • ungrouped

The inventory file that we have been using is in /home/tux/keys, let’s investigate it:

$ cd $HOME/keys
$ ansible-inventory --graph
@all:
|--@ungrouped:
| |--192.168.122.4
| |--192.168.122.5
$ ansible all --list-hosts
hosts (2):
192.168.122.4
192.168.122.5
$ ansible ungrouped --list-hosts
hosts (2):
192.168.122.4
192.168.122.5

NOTE: The group ungrouped lists host that do not appear in any groups other than the default all and ungrouped*groups. As such, they are ungrouped by the administrator.

Adding Groups to Ansible Inventory

At the lest we might group by the Linux distribution, but we can consider the role of the system, location, production or development etc. I am going to add another system to my managed nodes, again a brand new freshly installed Ubuntu 18.04 server, I have enabled remote SSH *root* login to the Server. We will start by editing the inventory file.

$ cd $HOME/keys
$ vim inventory
192.168.122.[4:6]
[rhel8]
192.168.122.[4:5]
[ubuntu1804]
192.168.122.6
[peterborough]
192.168.122.[4:6]
[development]
192.168.122.[4:6]

Groups are defined in the INI format using the square brackets. Host can be, and usually are in multiple groups. It now become easy to target just the rhel8 systems or the ubuntu1804. Lets graph the inventory again.

$ cd $HOME/keys
$ ansible-inventory --graph
@all:
|--@development:
| |--192.168.122.4
| |--192.168.122.5
| |--192.168.122.6
|--@peterborough:
| |--192.168.122.4
| |--192.168.122.5
| |--192.168.122.6
|--@rhel8:
| |--192.168.122.4
| |--192.168.122.5
|--@ubuntu1804:
| |--192.168.122.6
|--@ungrouped:

Targeting Groups with ansible-playbook

As we now have added each host to at least one administrator defined group, we now no longer have any hosts listed as ungrouped. We have implemented groups based on geography, distribution and status. With this in place we can use the new ubuntu1804 group to target just the new host, who as yet, does not have the key installed.

$ ansible-playbook -l ubuntu1804 -k keys.yml
SSH password:
PLAY [Add Keys to hosts] **********************************************************************************************
TASK [Gathering Facts] ************************************************************************************************
ok: [192.168.122.6]
TASK [Install Key] ****************************************************************************************************
changed: [192.168.122.6]
PLAY RECAP ************************************************************************************************************
192.168.122.6 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

NOTE: The option -l is used to drill down into the inventory for the patter that we specify. We use the group name here. It is normally the play within the Playbook that is used to target the hosts. We used all in the play but using the option -l means that we do not have to edit the Playbook

From the output of ansible-playbook*we can see that it was only the 192.168.122.6 host that was targeted.

=== Nested Groups

Groups can also contain groups but we need to ass a special keyword to the parent group name so that it is understood that we are adding groups and not hosts as members. We will modify the inventory to include the UK as a country to parent the city of Peterborough. We will also create ubuntu and redhat as parent groups for the distros.

$ cd $HOME/keys
$ vim inventory
192.168.122.[4:6]
[rhel8]
192.168.122.[4:5]
[ubuntu1804]
192.168.122.6
[peterborough]
192.168.122.[4:6]
[development]
192.168.122.[4:6]
[uk:children]
peterborough
[rhel:children]
rhel8
[ubuntu:children]
ubuntu1804

$ ansible-inventory --graph
@all:
|--@development:
| |--192.168.122.4
| |--192.168.122.5
| |--192.168.122.6
|--@rhel:
| |--@rhel8:
| | |--192.168.122.4
| | |--192.168.122.5
|--@ubuntu:
| |--@ubuntu1804:
| | |--192.168.122.6
|--@uk:
| |--@peterborough:
| | |--192.168.122.4
| | |--192.168.122.5
| | |--192.168.122.6
|--@ungrouped:

Creating an inventory with the thought of of expanding at a later time is always useful. So even though we only have one location adding in a country group allows for ease of growth later. The same with the rhel group which can cater for rhel7, rhel8 and CentOS derivatives we may have. The video follows: