Ansible is a free and easy to use automation tool for managing multiple remote hosts from the single machine. It provides you with an easy to configure for a large number of remote hosts. For example, you are performing the same tasks over multiple machines, Ansible provides you the option to automate these tasks.

Advertisement

Ansible is a better alternatives of the other popular infrastructure automation tools available like Chef and Puppet. You don’t need to install any client software on nodes to manage through Ansible server. It uses SSH connection to execute tasks on hosts.

This tutorial will help you to install and configure Ansible on Debian 10 Linux systems.

Prerequisites

For this tutorial, I have the following hosts:

  • One Control Node – To build your infra with Ansible, you need a control node where Ansible server will run. This is known as Ansible control node.
  • One or more Hosts – We have three hosts running with different-2 operating systems.
    • Two hosts with Ubuntu 20.04 LTS
    • One host with CentOS 8

Step 1 – Configure SSH Keys

Ansible control node uses ssh connection to connect hosts. You can configure Ansible to connect hosts using the password or key-based ssh access. In this tutorial, we will use both (password and key based ssh) types to connect hosts from control node.

Login to Ansible control node and generate ssh key pair (if not generated):

ssh-keygen -t rsa 

Just press “Enter” to all the input asked by the command.

Then copy the public key to the remote hosts, you need to connect via key-based:

ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.1.101 
ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.0.1.102 

Step 2 – Installing Ansible on Debian

The Ansible debian packages are available under the official Apt repository. You just need to add the PPA to your system. Use the following command to add Ansible debian repository to your system:

sudo apt-add-repository ppa:ansible/ansible 

Software Updater utility will update the packages cache on your system. So you have to run the following command to install or update Ansible on your Debian system

sudo apt update 
sudo apt install ansible 

Press ‘Y’ for all the installation confirmation to complete install process. Next, you need to configure Ansible server

Step 3 – Create Inventory File

You have installed Ansible server on your control node.

Now, you need to add the hosts to the Ansible server. Ansible allows to manage hosts in the form on hosts and groups. The Groups are used for performing one task on all remote hosts defined under it.

A single host can be added to multiple groups. Edit Ansible hosts configuration file. For example:

sudo nano /etc/ansible/hosts 

Add your hosts and organize them with groups:

[webservers]
web-host1
web-host2

[dbservers]
db-host1

[ubuntu]
web-host1
db-host1

[centos]
web-host2

The below image will help you to understand group and hosts under a group.

Per Host Configuration

You can create configuration files for individual hosts. All the hosts configuration file resides under /etc/ansible/host_vars directory with the same as hostname.

sudo mkdir /etc/ansible/host_vars/ 
sudo vi /etc/ansible/host_vars/web-host1 
  • 1’st Host – /etc/ansible/host_vars/web-host1
    ansible_ssh_host: 10.0.1.101
    ansible_ssh_port: 22
    ansible_ssh_user: root
    
  • 2’nd Host – /etc/ansible/host_vars/web-host2
    ansible_ssh_host: 10.0.1.102
    ansible_ssh_port: 22
    ansible_ssh_user: root
    
  • 3’rd Host – /etc/ansible/host_vars/db-host1
    ansible_ssh_host: 10.0.1.103
    ansible_ssh_port: 22
    ansible_ssh_user: root
    
  • In case you don’t have used Step 1 for the ssh connection for this host. You can also add one of the below methods to web-hosts1 configuration file for the authentication.

ansible_ssh_pass: secret_password
ansible_ssh_private_key_file: /home/rahul/.ssh/aws.pem

Group Vars Configuration

You can configure common variable settings of a Group under group configurations. The group file name must be same as the group name (eg: webservers) under group_vars directory.

sudo mkdir /etc/ansible/group_vars 
sudo vi /etc/ansible/group_vars/webservers 

Add the common variables to this file used by all the hosts added under this group.

ansible_ssh_port: 22
ansible_ssh_user: root

Step 4 – Testing Ansible Connection

Your Ansible is ready to use. To test all nodes connectivity use ping module. Login to your Ansible server and execute following command:

ansible -m ping all 

You can also test connectivity for the specific host or groups.

ansible -m ping web-host1         ## Specific host 
ansible -m ping webservers        ## Specific group 

You can also run any Linux command using the Ansible shell module. For example, execute the below command to test the free memory on web-host1.

ansible -m shell -a 'free -m' web-host1 

You can also perform the same task for a group. Just use group name instead of hostname.

Conclusion

In this tutorial, you have learned to install and configure Ansible on Debian 10 Linux system.

Share.

2 Comments

  1. Hi,
    Mr.Rahul
    Thanks a lot for such a nice tech info website , i would like to learn how to install |Ansible dashboard on Debian 10.
    i will be Thank full for your great help.

    Regards,
    waheed

Leave A Reply