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): 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:
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: 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 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: Add your hosts and organize them with groups: 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.
1’st Host – /etc/ansible/host_vars/web-host1ansible_ssh_host: 10.0.1.101 ansible_ssh_port: 22 ansible_ssh_user: root 2’nd Host – /etc/ansible/host_vars/web-host2ansible_ssh_host: 10.0.1.102 ansible_ssh_port: 22 ansible_ssh_user: root 3’rd Host – /etc/ansible/host_vars/db-host1ansible_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.
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. Add the common variables to this file used by all the hosts added under this group.
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: You can also test connectivity for the specific host or groups. 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.
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.