Network Topology

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Here, ansible-pc is a Debian 10 machine where we will install Ansible.

The servers 6f7c2 and 6b219 are Debian 10 machines which we will configure for Ansible automation. I will simply call these servers Ansible hosts for the purpose of this article.

We can use Ansible from ansible-pc to automate different tasks in the 6f7c2 and 6b219 Debian servers.

Installing Ansible

In this section, I will show you how to install Ansible on ansible-pc.

You can install Ansible on Debian 10 from the official package repository of Debian.

First, update the APT package repository cache with the following command:

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Now, install Ansible with the following command:

$ sudo apt install ansible

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

To confirm the installation, press Y and then press .

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Ansible should be installed.

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Now, run the following command to check if Ansible is working correctly.

As you can see, the ansible command is available and is working correctly. Ansible 2.7.7 is the latest version of Ansible available in the Debian package repository at the time this article was written.

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Generating SSH Key

On the Debian 10 machine (ansible-pc) where you have installed Ansible, you must first generate an SSH key.

To generate an SSH key, run the following command:

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Now, press .

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Press .

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Press .

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

An SSH key should be generated.

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Configuring Debian Hosts for Ansible Automation

In this section, I will show you how to configure a Debian host for Ansible automation. If you have multiple hosts which you want to automate using Ansible, then repeat the same process for each of the hosts.

The hosts you would like to configure for Ansible automation must have the SSH server package pre-installed.

First, update the APT package repository cache with the following command:

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Then, install the OpenSSH server with the following command:

$ sudo apt install openssh-server -y

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

In my case, the OpenSSH server package is already installed. If it is not installed in your case, then it should be installed prior to this step.

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Now, check if the sshd service is running via the following command:

$ sudo systemctl status sshd

As you can see, the sshd service is active (running) and enabled (will automatically start on system boot).

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

If the sshd service is not active (running), start it manually with the following command:

$ sudo systemctl start sshd

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

If the sshd service is not enabled (not added to the system startup) in your case, add it to the system startup manually with the following command:

$ sudo systemctl enable sshd

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Now, create an ansible user and allow password-less sudo access to the ansible user.

To create an ansible user, run the following command:

$ sudo adduser –shell /bin/bash –gecos “” ansible

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Type in a password for the ansible user and press .

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Retype the password and press .

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

An ansible user should be created.

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Now, to allow password-less sudo access to the ansible user, edit the /etc/sudoers file with the following command:

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Now, add the following line to the /etc/sudoers file.

ansible ALL=(ALL) NOPASSWD:ALL

Then, save the file by pressing X followed by Y, and then press .

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Now, find the IP address of the Ansible host 6f7c2 with the following command:

Here, the IP address in my case is 192.168.20.167. It will be different for you. So, make sure to replace this address with your own form now on.

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Copying SSH Public Key to the Ansible Host

From the computer where you have installed Ansible (ansible-pc), copy the SSH public key to the Ansible host 6f7c2 as follows:

$ ssh-copy-id ansible@192.168.20.167

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Type in yes and press .

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Next, type in the password for the ansible user and press .

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

The public SSH key should be copied to Ansible host 6f7c2.

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

You should be able to SSH into the Ansible host 6f7c2 as the user ansible without any password, as you can see from the screenshot below:

$ ssh ansible@192.168.20.167

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

You should also be able to run sudo commands without being prompted for any password.

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Finally, close the SSH session as follows:

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Securing Ansible Hosts

As the ansible user can run any sudo command without being prompted for a password, we have configured the SSH key based login for the Ansible hosts. But, you can still SSH into the Ansible hosts as ansible user using the password of the ansible user. So, this is not very secure.

To improve security, run the following command on the Ansible hosts to disable password-based login for the ansible user:

$ sudo usermod -L ansible

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

If you later decide to enable password-based login for the ansible user, run the following command on the Ansible host:

$ sudo usermod -U ansible

Testing Ansible

Create a new project directory ~/project/ in the Debian machine where you have installed Ansible (ansible-pc) using the following code:

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Navigate to the ~/project/ directory using the following code:

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Create a new hosts file in the project directory as follows:

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Now, list the IP addresses or DNS names of the Ansible hosts (6f7c2 and 6b219 in my case) in the hosts file:

192.168.20.167


192.168.20.168

Once you are done, save the file by pressing X followed by Y and then hit .

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

To test, try to ping all the hosts using Ansible with the following code:

$ ansible -i ./hosts all -u ansible -m ping

NOTE: Here, the -u option is used to specify the username (ansible in this case) which Ansible will use to SSH into the hosts.

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

As you can see, Ansible can access all the hosts. So, the hosts are ready for Ansible automation.

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

So, that is how you install Ansible on Debian 10 and configure Debian hosts for Ansible automation. Thank you for reading this article.

About the author

How to Install Ansible on Debian 10 and Configure Debian Hosts for Ansible Automation Ansible Debian

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.