Pages

Wednesday, February 3, 2016

Ansbile Inventory file


An inventory file is very important file that Ansible uses to find information about the remote machines that the tasks need to run. It is written in the INI format and tells anisble whether the remote machine or host provided by the user is genuine or not.

Ansible can run its tasks against multiple hosts in parallel. To do this, you can directly pass the list of hosts to Ansible using an inventory file. For such parallel execution, Ansible allows you to group your hosts in the inventory file; the file passes the group name to Ansible. Ansible will search for that group in the inventory file and run its tasks against all the hosts listed in that group.

The inventory file is passed to Ansible using the -I or –inventory-file option followed by path of the file. If we don’t pass the inventory file a default one is taken from the variable host_file set in Ansible.cfg file which is set to /etc/Ansible/hosts.

The basic inventory file looks as,
[root@vx111a inventory]# cat hosts
172.16.202.96 ansible_ssh_user=root
172.16.202.97 ansible_ssh_user=root

The Inventory file can contain either Host name or the IP address. In the above example we have defined 2 IP address. If we want ansbile to run the tasks on all machine we need to pass the “all” parameter while executing Ansible.

Groups in Inventory
The inventory file allows the hosts to be defined in groups so that we can run the Ansible command on that specific group. Groups can be defined as,

[root@vx111a inventory]# cat hosts
[dev]
172.16.202.96 ansible_ssh_user=root

[test]
172.16.202.97 ansible_ssh_user=root

In the above inventory file, we have defined 2 groups’ dev and test. We can use this grouping when we want to run our taks on specific group as

Ansible –I hosts all –l dev –m “ping”

In the above command we have used the Ansible command to run on the specific group “dev”.

Groups of Groups
Ansible allows us to create groups with in groups as,
[root@vx111a inventory]# cat hosts
[dev]
172.16.202.96 ansible_ssh_user=root

[test]
172.16.202.97 ansible_ssh_user=root

[testing:children]
dev
test

Now we have create a group testing:children with 2 groups dev and test. We can run the Ansible command on that group as

[root@vx111a inventory]# ansible testing -i hosts -m ping
172.16.202.96 | success >> {
    "changed": false,
    "ping": "pong"
}

172.16.202.97 | success >> {
    "changed": false,
    "ping": "pong"

}

No comments :

Post a Comment