Site icon UnixArena

Ansible – How to Setup Inventory for Easy Operations ?

Ansible Inventory

Ansible Inventory


In Ansible, Setting up inventory is one of the most important tasks.  Ansible can work with multiple servers at the same time but the challenge is how to classify the servers. While setting up Ansible environment, you need to classify the hosts as much a possible using the ansible inventory. For an example, By looking at the inventory, you should be able to list the specific application environment hosts, datacenter, location etc.  Let’s start building Ansible inventory file.

 Please note that /etc/ansible/hosts is different from OS /etc/hosts.

 

Server Inventory: 

Datacenter: DC-MTL

Datacenter: DC-TOR

Region: QUEBEC
Country: CANADA

 

Adding Servers to the inventory

1.Login to Ansible server as a user.  In this tutorial, we are using “sysadmin” user on all the client nodes.

[sysadmin@ansible-server ~]$ id
uid=1000(sysadmin) gid=1000(sysadmin) groups=1000(sysadmin)
[sysadmin@ansible-server ~]$ cd /etc/ansible/
[sysadmin@ansible-server ansible]$ ls -lrt
total 24
drwxr-xr-x. 2 root root     6 Jan 29 12:15 roles
-rw-r--r--. 1 root root  1016 Jan 29 12:15 hosts
-rw-r--r--. 1 root root 19179 Jan 29 12:15 ansible.cfg
[sysadmin@ansible-server ansible]$

 

2. To edit the “hosts” file, you need an root access. use “sudo” to get the elevated access.

[sysadmin@ansible-server ansible]$ sudo vi hosts

 

Add the following lines to the /etc/ansible/hosts file and save it.

[ABC-APP1-QUT]
uaans

Here “ABC-APP1-QUT” is group name and “uaans” is a server which is part of that.

 

3. List all the hosts which are added into the hosts file.

[sysadmin@ansible-server ansible]$ ansible all --list-hosts
  hosts (1):
    uaans
[sysadmin@ansible-server ansible]$

 

You could also list using the group parameter like below when you have more hosts. It will help to filter the hosts in Group wise and push the configuration.

[sysadmin@ansible-server ansible]$ ansible ABC-APP1-QUT --list-hosts
  hosts (1):
    uaans
[sysadmin@ansible-server ansible]$

 

Let’s ping all the hosts which are in the ansible inventroy. (we just have one host!)

[sysadmin@ansible-server ansible]$ ansible all -m ping
uaans | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
[sysadmin@ansible-server ansible]$

Ansible reads the inventory file and sends the ping module. The above output shows, nothing has changed but got the “pong” response.
4. Let’s send a specific command to the newly created group. If you have 100 hosts in that group, Ansible will get the results by logging in to each server.

[sysadmin@ansible-server ansible]$ ansible ABC-APP1-QUT -a 'uptime'
uaans | SUCCESS | rc=0 >>
 08:07:43 up 1 day, 16:24,  2 users,  load average: 0.00, 0.01, 0.05

[sysadmin@ansible-server ansible]$

Here, ansible server is logged in to the remote server and got the required output.

 

5. Let’s add all other remaining servers from our servers list. Add the following lines in /etc/ansible/hosts file.

[ABC-APP1-QUT]
uaans

[ABC-DB1-QUT]
ana-1

[ABC-APP1-PRD]
uaans69

[ABC-DB1-PRD]
ana-2

Look at each line closely. I have classified the hosts using the App name as “ABC” and landscape (PRD or QUT).

 

List all the hosts now.

[sysadmin@ansible-server ansible]$ ansible all --list-hosts
  hosts (4):
    uaans69
    uaans
    ana-2
    ana-1
[sysadmin@ansible-server ansible]$

 

6. Let’s find out the load average on group “ABC-DB1-PRD”.

[sysadmin@ansible-server ansible]$ ansible ABC-DB1-PRD -a 'uptime'
ana-2 | SUCCESS | rc=0 >>
 08:33:12 up 10:00,  2 users,  load average: 0.00, 0.01, 0.05

[sysadmin@ansible-server ansible]$

 

Now you could have a doubt that Why can’t we directly use the hostname to get the desired command output? Off-course, you can do that but group meant for a different purpose.

[sysadmin@ansible-server ansible]$ ansible ana-2  -a 'uptime'
ana-2 | SUCCESS | rc=0 >>
 08:37:42 up 10:05,  2 users,  load average: 0.00, 0.01, 0.05

[sysadmin@ansible-server ansible]$

 

7. Let’s classify the hosts further using the landscape. Here, we are creating the master group by specifying the children. Add the following lines in the ansible host file.

[ABC-QUT:children]
ABC-APP1-QUT
ABC-DB1-QUT

[ABC-PRD:children]
ABC-APP1-PRD
ABC-DB1-PRD

 

List the hosts from ABC-QUT.

[sysadmin@ansible-server ansible]$ ansible ABC-QUT --list-hosts
  hosts (2):
    uaans
    ana-1
[sysadmin@ansible-server ansible]$ 

 

List the hosts from ABC-PRD

[sysadmin@ansible-server ansible]$ ansible ABC-PRD --list-hosts
  hosts (2):
    uaans69
    ana-2
[sysadmin@ansible-server ansible]$

We have got the desired output. Using the same method, Let me classify more.

 

7. Let’s classify the hosts further using the datacenter, region, and country. Same hosts can be part of multiple groups. Here, I have added the same set of hosts using datacenter classification. I might use children relationship but there is no hard code rule to run a specific application on the same datacenter. Same application could run on different datacenters as well. So we are just adding the hosts according to the hosted data center.

[DC-MTL]
uaans
ana-1

[DC-TOR]
uaans69
ana-2

[QUEBEC:children]
DC-MTL
DC-TOR

[CANADA:children]
QUEBEC

 

List the servers which are running in country “CANADA”.

[sysadmin@ansible-server ansible]$ ansible CANADA --list-hosts
  hosts (4):
    uaans
    ana-1
    uaans69
    ana-2
[sysadmin@ansible-server ansible]$

 

List all the hosts. Even though we have added the same set of servers in multiple groups, it just removes the duplicate and provides the actual number of hosts.

[sysadmin@ansible-server ansible]$ ansible all --list-hosts
  hosts (4):
    uaans
    ana-1
    uaans69
    ana-2
[sysadmin@ansible-server ansible]$

In this tutorial, we haven’t discussed the hostname range, IP range and set up the variables in inventory file. We shall discuss those things in later part of the tutorial.

Exit mobile version