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.
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.
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.
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,
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,
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.
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.
$ ansible all -m ping -o
centos2 | UNREACHABLE!: Failed to connect to the host via ssh: ssh: connect to host centos2 port 1234: Connection refused
centos3 | UNREACHABLE!: Failed to connect to the host via ssh: ssh: connect to host centos3 port 1234: Connection refused
ubuntu1 | UNREACHABLE!: Failed to connect to the host via ssh: ssh: connect to host ubuntu1 port 1234: Connection refused
ubuntu2 | UNREACHABLE!: Failed to connect to the host via ssh: ssh: connect to host ubuntu2 port 1234: Connection refused
ubuntu3 | UNREACHABLE!: Failed to connect to the host via ssh: ssh: connect to host ubuntu3 port 1234: Connection refused
ubuntu-c | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": false,"ping": "pong"}
centos1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": false,"ping": "pong"}
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,