Ansible_Inventory_KeyLess_Auth
After Ansible installation, you need to follow some basic steps.
- Create inventory file.
- Make keyless authentication with Node[s].
Create an inventory file.
The Ansible inventory file defines the hosts and groups of hosts upon which commands, modules, and tasks in a playbook operate. The file can be in one of many formats depending on your Ansible environment and plugins. The default location for the inventory file is /etc/ansible/hosts .
For example in our environment we have these servers.
mail.feenixdv.com
foo.feenixdv.com
bar.feenixdv.com
one.feenixdv.com
two.feenixdv.com
three.feenixdv.com
For example, these servers categorized into mail, web and DB. Here in inventory we can group these servers list like.
[mail]
mail.feenixdv.com
[web]
foo.feenixdv.com
bar.feenixdv.com
[db]
one.feenixdv.com
two.feenixdv.com
three.feenixdv.com
We can also create cross grouping. Like
[webmail]
mail.feenixdv.com
foo.feenixdv.com
bar.feenixdv.com
If you are adding a lot of hosts following similar patterns,
[web]
www[01:50].feenixdv.com
You can also define alphabetic ranges:
[db]
db-[a:f].feenixdv.com
You can also select the connection type and user on a per host basis:
[targets]
localhost ansible_connection=local
other1.feenixdv.com ansible_connection=ssh ansible_user=mpdehaan
other2.feenixdv.com ansible_connection=ssh ansible_user=mdehaan
For broad details, follow Ansible official site https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html
For key less authentication, first need to generate key then copy key file to node.
On Ansible server:-
[root@feenixdv ~]# ssh-keygen
[root@feenixdv ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.40.XX
Follow the screen instruction. Mostly you need to press “Enter” except root password.
Here “192.168.40.XX” is IP address of node1. Change IP address during your practices.
Now check keyless with below command.
[root@feenixdv ~]# ssh root@192.168.40.XXX
Now you can check ping from Ansible by using ping module.
In my host (inventory) below server IP are present.
[root@feenixdv ansible]# pwd
/etc/ansible
[root@feenixdv ansible]# tail -n 24 hosts |head -n 5
[test]
192.168.40.146
192.168.40.148
[root@feenixdv ansible]# ansible test -m ping
192.168.40.146 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.40.148 | SUCCESS => {
"changed": false,
"ping": "pong"
}