How to Set Up Passwordless Authentication in Ansible

How to Set Up Passwordless Authentication in Ansible

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:

  1. Control Node: The machine where Ansible is installed and commands are executed.

  2. 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.

  1. On the Managed Node: Open the authorized_keys file:

     vim /home/ubuntu/.ssh/authorized_keys
    
  2. On the Control Node: Display the contents of the public key:

     cat /home/ubuntu/.ssh/id_ed25519.pub
    
  3. 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.

  4. 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.