Why Password-less Authentication?
In Ansible, password-less authentication simplifies automation by eliminating the need to repeatedly input passwords when managing multiple servers. Once set up, it allows you to:
Automate tasks across servers without manual intervention.
Connect to managed nodes securely using SSH keys.
Save time while working on scalable infrastructure.
Step 1: Prepare Your Instances
You’ll need two instances:
Control Node: The machine where Ansible is installed and commands are executed.
Managed Node: The server(s) you wish to manage with Ansible.
Launch both instances in your terminal and follow these steps on each:
sudo apt update
sudo apt install ansible
ansible --version
This ensures Ansible is installed and ready to use on the control node.
Step 2: Generate SSH Keys on on Both Nodes
On your control node, generate SSH keys using the following command:
ssh-keygen
Simply press Enter to save the keys in the default location. After generating the keys, check the .ssh
directory:
ls /home/ubuntu/.ssh
You should see the following files:
id_ed25519
(private key)id_
ed25519.pub
(public key)authorized_keys
(if it exists)
Step 3: Copy the Public Key to the Managed Node
Now, you need to add the public key from the control node to the authorized_keys
file on the managed node.
On the Managed Node: Open the
authorized_keys
file:vim /home/ubuntu/.ssh/authorized_keys
On the Control Node: Display the contents of the public key:
cat /home/ubuntu/.ssh/id_ed25519.pub
Copy and Paste the Key: Copy the output from the control node’s public key and paste it into the
authorized_keys
file on the managed node.Save the File: Save and exit the file on the managed node.
Step 4: Test Password-less Authentication
On the control node, test the SSH connection to the managed node using its private IP address:
ssh ubuntu@<MANAGED_NODE_PRIVATE_IP>
If everything is configured correctly, you should log in without being prompted for a password. 🎉 Boom! You’ve set up password-less authentication.