Adding Remote Hosts in Ansible Inventory with Authenticate Using 3 Different Method - Ansible Tutorial

Adding Remote Hosts in Ansible Inventory with Authenticate Using 3 Different Method - Ansible Tutorial


tags: devops tool ansible,devops,tools,ansible,tutorial,2019,ansible add remote hosts in inventory,add remote hosts,ansible remote host authentication method,automation tools,ansible_ssh_pass,ansible authentication using ssh key,ansible authentication with password,linuxtopic,linux topic,ansible hosts,ansible video turorial,ansible tower,ansible documentation,ansible family,ansble open source,
Ansible

OS - CentOS 7
Kernel - 3.10.0-693.17.1.el7.x86_64
IP - 172.17.20.100
Hostname - linuxtopic.com

Remote Nodes:

Node 1 :
UefiPXE  = 172.17.20.98
SSH Port = 22022
User = root

Node 2 : 
Ubuntu Server = 192.168.3.104 
SSH Port =  22
User = lokesh

Add remote hosts in inventory 

1) Password Authentication 
2) Preparing SSH Keys 
3) ssh-copy-id  


Video Tutorial in Hindi




1) Add remote hosts in inventory using Password  authentication 
vi /etc/ansible/hosts
[remote]
# Add host with password
192.168.3.104 ansible_ssh_user=lokesh ansible_ssh_pass=server32



To Ping Remote hosts group
ansible remote -m ping
ansible all -m ping


To check all configure hosts 
ansible --list-hosts all
To check groups hosts 
ansible --list-hosts remote


2) Authentication ansible hosts using ssh key 

Prepare ssh key in remote hosts, for example we have Ubuntu 16 with details:

Remote Host: 192.168.3.104
User: lokesh

To Install Openssh-server in Remote host 
apt install openssh-server


To generate ssh key using ssh-keygen 
ssh-keygenEnter passphrase ( emply for no passphrase) :Enter same passphrase again :


To verify ssh key file
cd .ssh/
cat id_rsa.pub 
To create authorized_keys file and copy public key 
cat id_rsa.pub >> /home/lokesh/.ssh/authorized_keys
cat id_rsa

Note: we require this private key ( id_rsa ) to authenticate with ansible 

Login in Ansible Server  and  create a key file with any suitable name and paste remote hosts Private key ( id_rsa )
vi /etc/ansible/remote-104.key
-----BEGIN RSA PRIVATE KEY-----

MIIEpAIBAAKCAQEArqw6AWoVPkebYQvlejJQPhJc/7tMVIrqnnhNtG2TbJ3DokaX

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EsbHF/b76G1+0LkH5S

vh+qlfAcNXVGatbrWMa41NSVGRviELo50MzXLUpRpvcBZ+hnMxtYvLqbEfnlews5

M8zObYO08fdPjGa3K2t1AQKBgQC9kuayPSxBRFbsSPFWA+dSvLovuJUKcumyE+3k

fZzpr6Vwb6mEj0A1bDz+A3y8UiPmYkkCRDQ754HT1hhGcqU1CNGGxybG6NL3dbv3

6wXRUjAbD1yO7uEZgtPSOqnw3+NemL/UF51i4buF5gy38506VOzpEtG1+fBSSsdT

DBGAaQKBgQCJN2kerW9+ecp08Bh79WwVdf8lyMjBwqx32thFu7f8c4heD4JZ/qJ/

WskapUzorqSNZpqr2ET/SdP0oZu0Z4ImH3MWxNVQeJd2TvpWfl45w/Ps+bVxwjju

3yGL1Gy7iku+7nocaNOG32ROB3Tu1l9NSPvmWmtRX6T0zV4x7NfIIw==

-----END RSA PRIVATE KEY-----


To set permission 
chmod 600 /etc/ansible/remote-104.key
To login  on remote host by ssh key
ssh -p22 -i /etc/ansible/remote-104.key [email protected]


Add remote host in inventory file using key file path
vi /etc/ansible/hosts
[localhost]
127.0.0.1

[remote]
192.168.3.104 ansible_ssh_user=lokesh ansible_ssh_private_key_file=/etc/ansible/remote-104.key



Verify hosts connectivity using ping module
ansible remote -m ping
ansible all -m ping



3) ssh-copy-id 

To copy ssh key using ssh-copy-id command
ssh-copy-id -p22 [email protected]
Type current password of lokesh user, it will auto copy the ssh key



Here: ssh-copy-id is command, -p22 is ssh port, lokesh is ssh user and 192.168.3.104 remote host 

Host 2: 
ssh-copy-id -p22022 [email protected]


Add Remote hosts in Inventory file
vi /etc/ansible/hosts
[localhost]
127.0.0.1

[remote]
192.168.3.104 ansible_ssh_user=lokesh ansible_become_pass=server32
172.17.20.98 ansible_port=22022



In inventory we use ansible_become_pass=server32, means we give sudo privileges 

To verify all hosts using ping ad-hoc command
ansible all -m ping


To check kernel version of all hosts using shell module 
ansible all -m shell -a "uname -a"





Previous
Next Post »