Ansible how to build an inventory the right way

Ansible is a well known tool which allows you to perform tasks on remote servers, building your hosts inventory the right is very important…

Ansible how to build an inventory the right way
Photo by Athul Cyriac Ajay on Unsplash

Ansible is a well known tool which allows you to perform tasks on remote servers, building your hosts inventory the right is very important especially when you have to handle many servers and multiple server groups, lets see how!

Inventory location

By default inventory file is at /etc/ansible/hosts but you might also define the inventory file to be used using the -i parameter

ansible-playbook -i /path/to/custom/inventory playbook.yml

Inventory formats

Ansible supports ini and yaml type formats for inventory, in this article i will use ini since is more readable in my opinion.

Inventory Examples

simple group

This is the simplest ansible inventory example

[groupname] 
host1 
host2 
host3

This inventory declares a group named “groupname” and three hosts named host1, host2 and host3. Assuming that you have 100 servers ranging from host1 to host100 you can do the following trick to make the hosts declaration easier!

group range

[groupaname] 
host[1:100]

The above example delclares a group named “groupname” with 100 hosts with the name host1 to host100, easy right? ;)

define username and key

Most likely using Ansible in a corporate environment means that you will use specific usernames and keys to connect to the hosts, to define hosts with usernames and keys use the following

[groupname] 
host[1:100] ansible_ssh_user=username ansible_ssh_private_key=./private_key.pem
  • ansible_ssh_user: Defines the username to be used to connect to the host
  • ansible_ssh_private_key: Defines the key to be used

Note that you can pass passwords as well but its not a good practice you have plain text passwords in a file.

group range trick

What if hosts 5 to 10 use another username and key than the rest of the hosts? You can use the following

[groupname] 
host[1:4] ansible_ssh_user=username1 ansible_ssh_private_key=./private_key1.pem 
host[5:10] ansible_ssh_user=username2 ansible_ssh_private_key=./private_key2.pem 
host[11:100] ansible_ssh_user=username1 ansible_ssh_private_key=./private_key1.pem

Or you can use this format, but personally i prefer the first way since is very well defined and less error prone.

[groupname] 
host[1:100] ansible_ssh_user=username1 ansible_ssh_private_key=./private_key1.pem 
host[5:10] ansible_ssh_user=username2 ansible_ssh_private_key=./private_key2.pem

groups of groups

Assume the following scenario! you have three groups, webeu,webus and and webas and you need to perform operations in all three servers groups, to make this easier you create a group of groups! lets call it webserversall

[webserversall:children] 
webeu 
webus 
webas

Conclussion

Ansible is powerful! knowing how to use inventory efficient can make your ansible operations run more flexible and smoother! i hope you enjoyed this article!