An Introduction to Ansible Inventory Files

Generally you will be running the Ansible task on a large number of systems. So you need to store the system details in a file. This file is called Inventory file in Ansible. The default location of inventory file is /etc/ansible/hosts. In other words, Inventory managed the hostname in which we are going to deploy the Ansible script. Also If you want to use any other inventory file without changing the default inventory file you need to specify it during execution time -i “path of inventory file”

Ansible Inventory

See Also:

Basic Inventory File:

# cat /etc/ansible/hosts
[Ansible-Inventory]
192.168.1.100
192.168.1.110
tech1.techoism.com
tech2.techoism.com

Also, If you want to change the default location of inventory file then you can modify it using Ansible configuration file.

# vim /etc/ansible/ansible.cfg

Old Entries:

inventory = /etc/ansible/hosts

New Entries:

inventory = "Path of New Inventory File"

In inventory file you can also define multiple groups with a set of multiple system. Here we can also define single server in multiple groups. For eg:

[WebServer]
apache1.techoism.com
apache2.techoism.com
apache3.techoism.com

[DataBase]
mysql1.techoism.com
mysql2.techoism.com
apache3.techoism.com

Note: In above example apache3.techoism.com server is define in both the groups.

Group Variables:
It is also possible to make groups of groups using :children suffix in Ansible inventory file.

[PHP:children]
WebServer
DataBase

Additional Variables in Inventory File:
You can also set value for many of the variables in Ansible inventory, so you can skip those variables in Ansible playbook.

[MySQL_servers]
mysql1.techoism.com ansible_user=svcans ansible_port=2223 ansible_ssh_pass="redhat"
mysql2.techoism.com ansible_connection=local ansible_connection=ssh

For more details, you can check ansible inventory documentation.

Enjoy it!

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.