Connect to Ansible Managed Hosts With an SSL Key

This blog post will show you how to connect to Ansible managed hosts using an SSL key and authenticate without using a password.

Create Inventory

In the previous article, I have shown you how to Install Ansible On Ubuntu Server 20.04; we will take the next step and add a managed machine for Ansible to manage.

To add managed machines to Ansible, we need to create an inventory file or use the default one installed with Ansible located in \etc\ansible and called hosts.

In my case, I have created a new host file called hosts using the following command on my Ubuntu host.

Note: You can name it with any name you like.

touch hosts

In the host file, I added a Linux managed machine, as shown below. I am using SSL keys to log in to the machines (To log in to your machine with SSH, see the following post How to Use SSH Keys To Login To a Linux Host.)

The first section (servers) lists the managed host IP address. The second section (server: vars) add variables that belong to the server. Since I am not using a username and password to log in to the machine, I am adding variables for the SSL key file and the user name.

#hosts file
[servers]
 192.168.100.201

[servers:vars]
 ansible_private_key_file=/home/user/.ssh/id_rsa
 ansible_user=myuser

Connect

To connect to my managed machine using Ansible, I will run the following command. Note that hosts is the name of the host file I created above.

sudo ansible servers -m ping -i hosts

The output that you should see is shown below:

192.168.100.201 | SUCCESS => {
     "ansible_facts": {
         "discovered_interpreter_python": "/usr/bin/python3"
     },
     "changed": false,
     "ping": "pong"

Processing…
Success! You're on the list.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.