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:
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:
you can also group different servers together by define it like this:
You can also have a group of groups:
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:
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 |
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.
The list of machines in the inventory can be found out through the ansible --list-hosts all
command :
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:
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:
and lets see the results:
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):
And if you like to see which python version has been installed on remote machines use shell module(we will talk about it later):
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
:
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.
.
.
.
https://docs.ansible.com/ansible/2.7/user_guide/intro_inventory.html
https://linuxhint.com/ansible-tutorial-beginners/
https://www.jeffgeerling.com/blog/creating-custom-dynamic-inventories-ansible
.
Last updated