Ansible Inventories

Let's fist create a file with the name ansible.cfg,

[defaults]
inventory = hosts

Next a file with the name hosts.ini,

[all]
centos1

In here, the ansible.cfg file is refering to an invenstory file with the name "hosts" and the hosts.ini file consist a host with the name "centos1". (In here it is assumed that a host with the name centos1 is reachable at this point)

Now first, I will remove the known hosts file from the ssh directory.

$ rm -rf /home/ansible/.ssh/known_hosts

Next when I try to ping to my host, it should asks me to verify the fingerprint, and I'm not going to accept it.

$ ansible all -m ping
The authenticity of host 'centos1 (172.18.0.8)' can't be established.
ECDSA key fingerprint is SHA256:gdRFM1dy+ntzCjU1mJi5oBS1k5enVlS/bPz6Wms59Ck.
Are you sure you want to continue connecting (yes/no/[fingerprint])? ^C [ERROR]: User interrupted execution

To get around with this, I'm going to ping the hosts again with a varible set up.

$ ANSIBLE_HOST_KEY_CHECKING=False ansible all -m ping
centos1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

The variable ANSIBLE_HOST_KEY_CHECKING instructs ansible to ignore to fingerprint verification when it is set to False. But sometimes it could be repetitive to specify this variable when executing commands. This can also be configured in the ansible.cfg file.

To make sure host's are not there, I'm going to remove the known hosts file,

Then I'm going to ping our hosts again,

Now let me slightly modify our invenstory file hosts.ini like below,

Though we have grouped these under their os type, we can still use the all key word which will take all hosts into consideration.

We can specify only a one type of os types like below,

This even works with other types of commands such as listing,

Well, this works with regular expressions as well (any number or charters and digits),

Sometimes it is essential that we run our commands as the root user. In order to perform that, we can specify the ansible_user parameter in our inventory file.

To verify this, I'm going to use a different module called command with the parameter id like below,

Output of the above command shows us that connections to the centos has made as root while for ubuntu hosts it still uses the ansible user.

This brings us to another interesting scenario. What if we want to connect to our hosts as a normal user, but needs to perform some tasks with escalated user? in that case we can use ansible_become_true parameter set to true and ansible_become_pass set to the password. For example, let's modify our inventory file like below,

Next we can execute the same command to see the user being used,

Ansible by default assumes the ssh port is 22. But there could be situations where the ssh port is different. In that case we can specify the ssh port in one of two ways mentioned in below,

or

And if we ping again, we might be able to connect to the hosts again,

We can also specify a control gorup where it will not use and ssh connection.

It is also possible to specify ranges in the inventory files.

Let's check all the hosts to verify this,

But still our inventory file has duplicates. For example we specify the ansible user for all groups. This can be addressed using group vars. These group vards will be fed into each record during the execution.

Let's ping again and see if this works,

We can futher group things using a parent:child relationship like below,

We can now call everything falling under linux group and see if it still working,

Now let's motify the inventory like below,

You can notice that I've specified variables section for all where I define the ssh port as 1234. This indeed is a wrong port. However, these variables have a precedence effect. Since I have specified the correct port for centos1 host along with it, it will work fine, but others might fail.

It is also possible to write the inventory files in YAML format. But in that case we have to explicitly specify the inveontory file in the ansible.cfg file,

Then we can declare our inventory file in YAML format like below,

Similarly it is also possible to specify the the inventory file in JSON format,

When using a differnt inventory file formats or multiple file, you can specify the inventory using the i flag in the command line,

Last updated

Was this helpful?