How to install and use Ansible on Debian 10

When we talk about configuration management tools, the most common name we hear is Ansible. It is a cross-platform tool designed to handle system configurations when working with Linux, macOS and Windows operating systems. Today we will try to shed some light on the Ansible installation procedure on Debian 10.

Installing Ansible on Debian 10

To install Ansible on Debian 10, you need to follow these three easy steps:

Step # 1: Upgrade your Debian 10 system:

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

sudo apt update

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

sudo apt update

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

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

sudo apt install ansible

Install Ansible

While running this command, you will see a message asking if you want to continue with the installation on your terminal. You must enter “Y” for the installation process to proceed smoothly, as shown in the following image:

Installing Ansible

When Ansible is successfully installed on your Debian 10 system, the terminal will display several messages on it, similar to the one shown in the image below:

Installing Python Libraries

Step # 3: Confirm the installation of Ansible on your Debian 10 system:

Installing Ansible on a Debian 10 system is so easy that it will be completed in the above two steps. However, you can still check if it was installed successfully on your Debian 10 system or not. This can be done by checking its version with the following command:

ansible --version

Check Ansible version

Our Debian 10 system has Ansible 2.7.7 installed, which is also highlighted in the image shown below:

Ansible 2.7.7

Edit the Ansible hosts file / etc / ansible / hosts to add the system 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 need to set up an SSH key for the client node, since Ansible uses the SSH protocol to send commands to the client system.

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

ssh-keygen

Exit:

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....................... root[email protected]
The key's randomart image is:
+---[RSA 2048]----+
| . . . |
+----[SHA256]-----+

The next step is to copy the newly generated key to another system. Run this command:

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

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

Exit:

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/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’s time for the first test, login to another computer using this SSH command:

ssh [email protected]

Now the login should work without asking for a password.

Test Ansible

The installation is complete, now we can start testing Ansible.

Run this command to test the connection:

ansible -m ping TestClient

Exit:

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

If you have defined more than one client, you can check all connections with the following command:

ansible -m ping all

Now it’s time to run the command on the remote system and get the result. In this example, I will use the df command.

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

Exit:

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 hard disk usage on the remote system. You can run any similar Linux shell command using ansible, as well as create complete scripts for configuring and maintaining servers.

Removing Ansible

To uninstall Ansible from your Debian 10 system, you first need to uninstall it as well as its config files with the following command:

sudo apt-get purge ansible

Clear Ansible

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

sudo apt-get autoremove

Remove unused Ansible dependencies

Output

This article shows you how to install Ansible on a Debian 10 system and how you can use it to remotely manage systems over a secure SSH connection.

How to install and use Ansible on Debian 10

Sidebar