Debian

How to Install and Configure Ansible on Debian 12

Ansible

Ansible is a Python-based application that provides the facility to run similar tasks on multiple systems through one primary system. This removes the hassle of configuring each system manually as the user can just write down the task to be performed by the systems in a playbook. Ansible is an open-source software that can be used in app development, system integration, or for carrying out routine work when it comes to network management. On Debian 12 Ansible can be installed in two ways yet to function properly it is to be configured properly.

Outline:

How to Install Ansible in Debian 12

Ansible usually uses the secure shell for interacting remotely with the systems and sends instructions in the form of modules. Using these modules the systems perform the desired tasks or get into the desired state, to install Ansible on Debian 12 there are two ways:

Method 1: Through the Advance Packaging Tool

One of the easiest ways to install any package or application on Debian 12 is by using its default package manager tool so in the case of Ansible execute:

sudo apt install ansible -y

After successfully installing this automation application, then verify its version and for that:

ansible --version

Method 2: Through Pipx

Since Ansible is a Python-based tool it can be installed through pipx which is a package manager for installing non-Debian Python command-line applications. So, to install Ansible on Debian through pipx it needs to be installed first and for that run:

sudo apt install pipx -y

After successful installation of pipx verify its version by executing:

pipx --version

There are two types of installation for Ansible one is minimal which means that it has all the basic features whereas the other type has all the options starting from the basic level to advanced. To install the advanced level Ansible with full options execute the:

pipx install --include-deps ansible

You might get a warning note once upon the successful installation of Ansible on Debian 12 stating that this application cannot be accessible globally.

This is because the path of the environmental variable doesn’t include the directory in which you have installed Ansible, so to fix this issue of accessibility execute:

pipx ensurepath

Once you are done with the path correction, a system reboot is required to make effect the changes. Keeping the application updated keeps them functioning smoothly so in case there is a new update for Ansible then just execute:

pipx upgrade --include-injected ansible

If you have trouble with using the currently installed version of Ansible and want to install the basic setup of Ansible with a specific version then run:

pipx install ansible-core==2.12

Note: You might be wondering since Ansible is based on Python language why it cannot be installed through the pip command. The answer to that is no, and this is because in Debian 12 there is a new feature that prevents pip from installing any applications in the Python environment because it causes issues with the Debian package manager, which can lead to system stability issues. This issue can be resolved by creating a virtual environment as it isolates the Python projects resulting in no conflict with the Debian package managing tool.

Configuring Ansible on Debian 12

Once you have successfully installed Ansible on Debian 12 there are still some configurations required to make it work and here are some steps for it:

Step 1: Install SSH

As mentioned above Ansible uses SSH to access the systems remotely so on Debian 12 the OpenSSH server should be installed first and for that execute:

sudo apt install openssh-server -y

Once the secure shell is installed, check if the service is active and running using the systemctl command like this:

sudo systemctl status sshd

If in any case the SSH service is not running or active then start and enable the service by using the below commands:

sudo systemctl start sshd

sudo systemctl enable sshd

Step 2: Create an Ansible User

Now to run the commands and tasks on remote hosts through Ansible on Debian 12 a new separate Ansible user should created and for that run:

sudo adduser anisble

Now to allow the ansible user to have administrator permissions while executing the commands without providing the password allow the password exemption by editing the sudoers file. This is required because the system itself executes the commands and it might need administrator permission sometimes:

echo "ansible ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/ansible

Step 3: Generate and Copy the SSH Key

To make the secure shell more protected from any malware or hacks it requires a key that makes the connection between the host and the remote system successful. This key has two parts one is the public key which is shared with the remote system and the other is the private key which is saved in the host system. So, the Ansible user can use its private key to connect to the remote system, to generate the ssh key execute:

ssh-keygen

When generating the SSH key the system will ask about the password, and the save directory so simply press enter to move forward with the process. Once the key is generated, save the public part of the key to the host address by stating its IP address:

ssh-copy-id ansible@<host-address>

Step 3: Add Hosts

Now you can create a file named hosts and save their IP addresses in it, here for illustration I have two dummy IP addresses:

sudo nano hosts

Step 4: Test the Configuration

Once you have saved the IP addresses of the hosts, use the ping command along with executing the host file to see if the connection is established successfully.

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

Here I have only one host, so only one IP address is displayed, with a success message and if the status is not successful it means that the connection is not established successfully:

If you are new to using Ansible then to get familiar with the commands used for operating Ansible consult its help by executing:

ansible --help

Removing Ansible From Debian 12

If you plan to remove Ansible from Debian 12 that was installed through the advanced packaging tool then execute the:

sudo apt remove --autoremove anisble -y

If you plan to remove Ansible from Debian 12 that was installed through pipx then in that case, just run:

pipx uninstall anisble

The ansible-core is the base-level setup of Ansible so if you want to remove it from Debian 12 then execute:

pipx uninstall anisble-core

Conclusion

To install Ansible on Debian 12 there are two ways one is by using the advanced packaging tool and the other is by using the pipx tool which is a replacement for pip command. To make Ansible function smoothly on Debian it is required to be configured which includes creating an Ansible user having sudo privileges with password exemption and an SSH key to establish a remote connection.

 

Similar Posts