Whenever we talk about configuration management tools, the name that we hear most often is Ansible. It is a cross-platform tool that is designed to handle system configurations while working with Linux, macOS, and Windows operating systems. Today, we will try to throw light on the procedure of installing Ansible on Debian 10.

Installing Ansible on Debian 10

For installing Ansible on Debian 10, you need to perform the following three simple steps:

Step # 1: Update your Debian 10 System:

Before installing Ansible on Debian 10, you have to update it with the command shown below:

sudo apt update

How to Install and Use Ansible on Debian 10 Debian linux

After updating your Debian 10 system, you will see something like shown in the following image on your terminal:

How to Install and Use Ansible on Debian 10 Debian linux

Step # 2: Install Ansible on your Debian 10 System:

After the system update finishes, you can install Ansible on Debian 10 with the command shown below:

sudo apt install ansible

How to Install and Use Ansible on Debian 10 Debian linux

During the time this command will execute, you will see a message asking you if you want to continue with the installation or not on your terminal. You have to type “Y” so that the installation process keeps running smoothly as highlighted in the following image:

How to Install and Use Ansible on Debian 10 Debian linux

When Ansible will be successfully installed on your Debian 10 system, the terminal will produce some messages on it similar to the ones shown in the image below:

How to Install and Use Ansible on Debian 10 Debian linux

Step # 3: Confirm the Installation of Ansible on your Debian 10 System:

The installation of Ansible on a Debian 10 system is so simple that it will complete within the above two steps. However, you can still verify if it has been successfully installed on your Debian 10 system or not. This can be done by checking its version with the following command:

ansible --version

How to Install and Use Ansible on Debian 10 Debian linux

The version of Ansible that is installed on our Debian 10 system is 2.7.7 which is also highlighted in the image shown below:

How to Install and Use Ansible on Debian 10 Debian linux

Edit the Ansible hosts file /etc/ansible/hosts to add the system that we want to manage with Ansible.

sudo nano /etc/ansible/hosts

Add the following:

[TestClient]
node1 ansible_ssh_host=192.168.0.12
And save the file.

Using Ansible

First, we have to configure an SSH Key for client node as Ansible is using the SSH protocol to transfer commands to the client system.

Use this command to generate a SSH key for key-based authentication:

ssh-keygen

Output:

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:gTHiTCK....................... [email protected]
The key's randomart image is:
 ---[RSA 2048]---- 
| . . . |
 ----[SHA256]----- 

The next step is to coyp the newly generated key to the other system. Run this command:

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

The IP 192.168.0.2 in above command needs to be replaced with the IP address of the system that you want to manage with Ansible.

Output:

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "https://vitux.com/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:

Number of key(s) added: 1

Now it is time for a first test, log into the other machine by using this SSH command:

ssh [email protected]

The login should work without requiring a password now.

Test Ansible

The installation part is finished, now we can start to test Ansible

Run this command to test the connection:

ansible -m ping TestClient

Output:

node1 | SUCCESS => {
"changed": false, 
"ping": "pong"
}

In case you defined more than one client, you can test all connections with the following command:

ansible -m ping all

Now it’s time to run a command on the remote system and fetch the result. For this example, I’ll use the df command.

ansible -m shell -a 'df -h' TestClient

Output:

node1 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
udev 957M 0 957M 0% /dev
tmpfs 195M 21M 175M 11% /run
/dev/sda1 38G 11G 25G 31% /
tmpfs 974M 0 974M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 974M 0 974M 0% /sys/fs/cgroup
/dev/sda15 121M 130K 120M 1% /boot/efi
tmpfs 195M 0 195M 0% /run/user/0

The result shows the harddisk usage on the remote system. You can execute any Linux shell command like this by using ansible and also create compley scipts to setup and maintain servers.

Removing Ansible

For removing Ansible from your Debian 10 system, you first need to uninstall it and also its configuration files with the following command:

sudo apt-get purge ansible

How to Install and Use Ansible on Debian 10 Debian linux

After executing this command successfully, you can also get rid of all the irrelevant packages and dependencies by executing the command shown below:

sudo apt-get autoremove

How to Install and Use Ansible on Debian 10 Debian linux

Conclusion

This article shows how to install Ansible on a Debian 10 system and how you can use it to manager systems remotely.