How to Establish Passwordless SSH Connection Between Ansible Server and Hosts

Share via:

Dear Readers,

In this article, we will see Establish Passwordless SSH Connection Between Ansible Server and Hosts.

Steps to Follow

Test Environment Setup in Ansible Server “Ansible”

  1. Create a new user “kt-ansible” and set a password for the user.
  2. Add user “kt-ansible” to the sudo users list.
  3. Establish an ssh connection.
  4. Restart the sshd service.

Test Environment Setup in Host 1 “Ansible-Node-1”

  1. Create a new user “kt-ansible” and set a password for the user.
  2. Add user “kt-ansible” to the sudo users list.
  3. Establish an ssh connection.
  4. Restart the sshd service.

Test Environment Setup in Host 1 “Ansible-Node-2”

  1. Create a new user “kt-ansible” and set a password for the user.
  2. Add user “kt-ansible” to the sudo users list.
  3. Establish an ssh connection.
  4. Restart the sshd service.

Test Environment Setup in Ansible Server “Ansible”

  1. Connect Chef Server to Host 1 “Ansible-Node-1”.
  2. Connect Chef Server to Host 2 “Ansible-Node-2”.

Test Environment Setup in Ansible Server “Ansible”

1. Create a new user “kt-ansible” and set a password for the user

Create a new user “kt-ansible”

Set password for the user “kt-ansible”

2. Add user “kt-ansible” to the sudo users list   

Open “visudo” file and add your user “kt-ansible ALL=(ALL)” under ” root NOPASSWD:ALL ALL=(ALL) ALL”
After adding user adds to the “visudo” file then onwards user”kt-ansible” get sudo privileges.

Note
:wq! —– to quit.

3. Establish an ssh connection

Open ssh configuration file “sshd_config” and comment and uncomment some parameters.

4. Restart the sshd service

Test Environment Setup in Node 1 “Ansible-Node-1”

1 Create a new user “kt-ansible” and set a password for the user

Create a new user “kt-ansible”

Set password for the user “kt-ansible”

2. Add user “kt-ansible” to the sudo users list

Open “visudo” file and add your user “kt-ansible ALL=(ALL)” under ” root NOPASSWD:ALL ALL=(ALL) ALL”
After adding user adds to the “visudo” file then onwards user”kt-ansible” get sudo privileges.

Note
:wq! —– to quit.

3. Establish an ssh connection

open ssh configuration file “sshd_config” and comment and uncomment some parameters.

4. Restart the sshd service

Test Environment Setup in Node 2 “Ansible-Node-2”

1. Create a new user “kt-ansible” and set a password for the user

Create a new user “kt-ansible”

Set password for the user “kt-ansible”

2. Add user “kt-ansible” to the sudo users list

Open “visudo” file and add your user “kt-ansible ALL=(ALL)” under ” root NOPASSWD:ALL ALL=(ALL) ALL”
After adding user adds to the “visudo” file then onwards user”kt-ansible” get sudo privileges.

Note
:wq! —– to quit.

3. Establish an ssh connection

open ssh configuration file “sshd_config” and comment and uncomment some parameters.

4. Restart the sshd service

Test Environment Setup in Ansible Server “Ansible”  

1. Connect Ansible Server to Host 1 “Ansible-Node-1”

Connecting to node 1 with a password and without password
Switch to user “kt-ansible” and update server packages

Connect to Node 1 “Ansible-Node-1” with password

using below command for connecting Ansible Server to Node 1

Connect to Node 1 “Ansible-Node-1” without password

For connecting to the host 1 without a password we need to follow some steps
Generate Keys
Copy keys to node 1

Generate Keys

Verify keys
we can see the keys inside the .ssh directory.

Copy keys to Host 1

When you run below command it will copy the content of public key inside the .ssh directory and go inside .ssh directory of the node 1 create a file “Authorized keys” and paste content of public key.
we can see the keys has been copied to node 1 successfully.

Verify keys in Host 1 “Ansible-Node-1”
We can see the file “authorized keys” inside the .ssh directory.

Using below command for connecting to node 1 without password

2. Connect Chef Server to Host 2 “Ansible-Node-2”

Here also, we need to generate keys and copy keys to node 2.
We have already generated the keys, so we have to copy those keys to node

Copy the keys to node 2

Verify keys in node 1 “Ansible-Node-2”
We can see the file “authorized keys” inside the .ssh directory.

Using below command for connecting to node 1 without password

The connection has been established from ansible to servers to 2 nodes.
so, we verify the nodes and groups being inside the chef server.

Verify all nodes and all groups (be inside ansible server)
all — all nodes and all groups of inventory.
we can see the 2 hosts IP Addresses, those we have configured in /etc/ansible/hosts.

Verify all nodes inside the group “Ktexperts-Nodes-Group”

Verify the first node inside the group ” Ktexperts-Nodes-Group”

Verify the last node inside the group ” Ktexperts-Nodes-Group”

Verify first 2 nodes inside the group ” Ktexperts-Nodes-Group”

Thank you for giving your valuable time to read the above information.
Follow us on 
Website  www.ktexperts.com
Facebook Page KTexperts
Linkedin Page : KT EXPERTS

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

Add Comment