Ansible Inventory

What is Inventory file?

Ansible reads information about which machines you want to manage from your inventory. Ansible has a default inventory file, but you can create your own and define which servers you want Ansible to manage.

Ansible default inventory file

The default location for inventory is a file called /etc/ansible/hosts:
1
[[email protected] ~]# cat /etc/ansible/hosts
2
# This is the default ansible 'hosts' file.
3
#
4
# It should live in /etc/ansible/hosts
5
#
6
# - Comments begin with the '#' character
7
# - Blank lines are ignored
8
# - Groups of hosts are delimited by [header] elements
9
# - You can enter hostnames or ip addresses
10
# - A hostname/ip can be a member of multiple groups
11
12
# Ex 1: Ungrouped hosts, specify before any group headers.
13
14
## green.example.com
15
## blue.example.com
16
## 192.168.100.1
17
## 192.168.100.10
18
19
# Ex 2: A collection of hosts belonging to the 'webservers' group
20
21
## [webservers]
22
## alpha.example.org
23
## beta.example.org
24
## 192.168.1.100
25
## 192.168.1.110
26
27
# If you have multiple hosts following a pattern you can specify
28
# them like this:
29
30
## www[001:006].example.com
31
32
# Ex 3: A collection of database servers in the 'dbservers' group
33
34
## [dbservers]
35
##
36
## db01.intranet.mydomain.net
37
## db02.intranet.mydomain.net
38
## 10.25.1.56
39
## 10.25.1.57
40
41
# Here's another example of host ranges, this time there are no
42
# leading 0s:
43
44
## db-[99:101]-node.example.com
45
Copied!
If you don't create inventory file, Ansible uses this default inventory file.

Sample Inventory files

The inventory file is an ini like format.It is simply a number of servers, listed one after other:
1
#Sample Inventory File
2
server1.company.com
3
192.168.10.2
Copied!
you can also group different servers together by define it like this:
1
#Grouping servers
2
server1.company.com
3
192.16.10.2
4
5
[mail]
6
192.168.10.3
7
server4.company.com
8
9
[db]
10
server5.company.com
11
server6.company.com
Copied!
You can also have a group of groups:
1
#Group of groups:
2
server1.company.com
3
192.168.10.2
4
5
[mail]
6
192.168.10.3
7
server4.company.com
8
9
[db]
10
server5.company.com
11
server6.company.com
12
13
[all_servers:children]
14
mail
15
db
Copied!
in this sample we have created a group called all_servers. Some other examples:
Example
Description
web[1-3].example.com
If you have a lot of hosts with a similar pattern
server1.example.com:5555
If you have hosts that run on non-standard SSH ports
Also If you like to refer to these servers in Ansible using an alias, it is possible:
1
#Using alias
2
web1 ansible_host=server1.company.com
3
db1 ansible_host=server2.company.com
4
mail1 ansible_host=192.168.10.3
5
web2 ansible_host=server4.company.com
Copied!
ansible_host is an inventory parameter, used to specify the FQDN or IP Address of a server. There are other inventory parameters too.

Inventory Parameters

Here, lets take a look at most useful Ansible Inventory parameters and examples:
Example
Description
ansible_host=1.2.3.4
name of the host to connect to, if different from the alias you wich to give to it
ansible_port=5555
which port to connect to (default 22/tcp)
ansible_connection=ssh
defines how ansible get connected to the target[shh / winrm / localhost ]
ansible_user=Linda
defines the user used to make remote connection, if no user is specified current user will be used
ansible_ssh_pass=***
define ssh password for linux
1
#Sample Inventory parameters
2
web1 ansible_host=server1.company.com ansible_connection=ssh ansible_user=root
3
db1 ansible_host=server2.company.com ansible_connection=winrm ansible_user=admin
4
mail1 ansible_host=192.168.10.3 ansible_connection=ssh [email protected]
5
web2 ansible_host=server4.company.com ansible_connection=winrm
6
7
localhost ansible_connection=localhost
Copied!
Note that storing password in plain text format is not a good idea, lookup Ansible Vault to securely store your password in an encrypted format. We will talk about it later.
In production environment using password to establish connectivity between systems is not recommended, it better to use SSH Keys instead.

Demo - Inventory files

Lets make a test project with a custom inventory file.
1
[[email protected] ~]$ mkdir demo-inventory
2
[[email protected] ~]$ cd demo-inventory/
3
[[email protected] demo-inventory]$ ll
4
total 0
5
[[email protected] demo-inventory]$ vim inventory.txt
6
[[email protected] demo-inventory]$ cat inventory.txt
7
ubuntu
Copied!
The list of machines in the inventory can be found out through the ansible --list-hosts all command :
1
[[email protected] demo-inventory]$ ansible --list-hosts all -i inventory.txt
2
hosts (1):
3
ubuntu
Copied!
We can specify a different inventory file at the command line using the -i <path> option.
And now, First Ansible Task! You can ping all of your inventory machines using the following command:
1
[[email protected] demo-inventory]$ ansible ubuntu -m ping -i inventory.txt
2
ubuntu | SUCCESS => {
3
"ansible_facts": {
4
"discovered_interpreter_python": "/usr/bin/python3"
5
},
6
"changed": false,
7
"ping": "pong"
8
}
Copied!
so that confirms that our ansible controller can successfully communicate or connect to the target machines. lets update inventory.txt file by adding second target:
1
[[email protected] demo-inventory]$ cat inventory.txt
2
ubuntu
3
centos
Copied!
and lets see the results:
1
[[email protected] demo-inventory]$ ansible all -m ping -i inventory.txt
2
centos | SUCCESS => {
3
"ansible_facts": {
4
"discovered_interpreter_python": "/usr/bin/python"
5
},
6
"changed": false,
7
"ping": "pong"
8
}
9
ubuntu | SUCCESS => {
10
"ansible_facts": {
11
"discovered_interpreter_python": "/usr/bin/python3"
12
},
13
"changed": false,
14
"ping": "pong"
15
}
Copied!
There is a group that Ansible creates by default and that's called the all group. The all group is a built-in group that Ansible creates and it has all the servers in our inventory file part of that group.
If there is a problem with python on one of your target nodes, you can send a raw module (we will talk about it later):
1
[[email protected] demo-inventory]$ ansible -m raw -a "/usr/bin/uptime" -i inventory.txt all
2
centos | CHANGED | rc=0 >>
3
10:12:35 up 23:33, 2 users, load average: 0.00, 0.01, 0.05
4
Shared connection to centos closed.
5
6
ubuntu | CHANGED | rc=0 >>
7
22:42:35 up 19:20, 2 users, load average: 0.00, 0.00, 0.00
8
Shared connection to ubuntu closed.
Copied!
And if you like to see which python version has been installed on remote machines use shell module(we will talk about it later):
1
[[email protected] demo-inventory]$ ansible -m shell -a "python -V" -i inventory.txt all
2
centos | CHANGED | rc=0 >>
3
Python 2.7.5
4
[DEPRECATION WARNING]: Distribution Ubuntu 18.04 on host ubuntu should use
5
/usr/bin/python3, but is using /usr/bin/python for backward compatibility with
6
prior Ansible releases. A future Ansible release will default to using the
7
discovered platform python for this host. See https://docs.ansible.com/ansible/
8
2.9/reference_appendices/interpreter_discovery.html for more information. This
9
feature will be removed in version 2.12. Deprecation warnings can be disabled
10
by setting deprecation_warnings=False in ansible.cfg.
11
ubuntu | CHANGED | rc=0 >>
12
Python 2.7.17
Copied!
Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg
Now that you know about inventory files let put our targets nodes information on /etc/ansible/hosts :
1
[[email protected] ~]# tail -n7 /etc/ansible/hosts
2
3
ubuntu
4
centos
5
6
[lab]
7
ubuntu
8
centos
Copied!
this way we won't need to specify inventory file while running a command.

Dynamic inventories

Most infrastructure can be managed with a custom inventory file, but there are many situations where more control is needed. Ansible will accept any kind of executable file as an inventory file, as long as you can pass it to Ansible as JSON.
You could create an executable binary, a script, or anything else that can be run and will output JSON to stdout, and Ansible will call it with the argument --list when you run, as an example, ansible all -i my-inventory-script -m ping.
You can always check ansible github web page (https://github.com/ansible/ansible/tree/devel/examples) and other sources for examples, but that's more advanced topic.
that's all.
.
.
.
.
Last modified 2mo ago