After completing this section, you should be able to:
Create an Ansible host inventory.
Inspect the contents of the inventory using the ansible-inventory program.
The Ansible host inventory is the source of truth for Ansible managed resources on the network. It includes all managed hosts: routers, switches, firewalls, load balancers, and other network appliances; servers, workstations, desktops, or mobile devices such as tablets or phones.
You can use the inventory to assign managed hosts to groups. And custom variables for hosts and groups can be defined by way of the inventory.
Hosts can be in multiple groups. Groups can be defined to describe many different ways in which it makes sense to map Ansible ad hoc commands or plays to hosts. This makes it possible to apply actions to multiple hosts without having to explicitly specify each one.
Would you like to apply a change to all network devices located in a particular virtual private cloud?
If they all belong to a group named vpc01_net, use that group name in the ad hoc command or playbook.
Ansible uses command options, the configuration file, or the file system to determine which inventory file to use.
It checks these in this order and uses the first file it finds.
Command option, such as ansible-playbook -i
filepath
The ansible.cfg configuration file:
[defaults] inventory = /some/other/filepath
The default locations: /etc/ansible/hosts
The inventory file can be in one of many formats, depending on the inventory plug-ins you have. The most common formats are INI and YAML.
Table 1.2.
| INI Format | YAML Format |
|---|---|
[spines] spine01 spine02 [leafs] leaf01 leaf02 |
all:
children:
spines:
hosts:
spine01
spine02
leafs:
hosts:
leaf01
leaf02
|
Ranges, which match all the values from START to END inclusively, can be used in inventory files to define hosts:
[START:END]!
192.168.[4:7].[0:255] matches all IPv4 addresses in the 192.168.4.0/22 network (192.168.4.0 through 192.168.7.255)
srvr[01:20].example.com matches all hosts named srvr01.example.com through srvr20.example.com.
If leading zeros are included in numeric ranges, they are used in the pattern.
This does not match srvr1.example.com but does match srvr07.example.com.
[a:c].dns.example.com matches hosts named a.dns.example.com, b.dns.example.com, and c.dns.example.com.
2001:db8::[a:f] matches all IPv6 addresses from 2001:db8::a through 2001:db8::f.
The children keyword can be used to create nested groups.
Table 1.3.
| INI Format | YAML Format |
|---|---|
[spines]
spine[01:02]
[leafs]
leaf[01:02]
|
all:
children:
spines:
hosts:
spine[01:02]
leafs:
hosts:
leaf[01:02]
|
There are two special host groups: all and ungrouped.
Ansible automatically puts hosts that do not have a group into the ungrouped group.
Every host in the inventory automatically belongs to the special group named all.
Use the ansible-inventory command to inspect the inventory.
Here are some useful options:
[user@host ~]$ansible-inventory --list[user@host ~]$ansible-inventory --graph[user@host ~]$ansible-inventory --graphgroup[user@host ~]$ansible-inventory --hosthost
The ansible command accepts host patterns.
The --list-hosts option provides a quick way to show how a host pattern resolves to a host or list of hosts.
[user@host ~]$ansible -i inventory --list-hosts spine*hosts (2): spine01 spine02