In this article, you will learn how to install and configure Ansible on CentOS 8.
Ansible is an open-source software package. The main work of Ansible is to automate work like configuration management, application deployment, inter-service orchestration, cloud provisioning, and many other IT works.
This is a simple tool. But, you can find it very useful to automate the complex multi-tier IT application environments. Ansible has been written using Python programming language. Therefore, It follows a straightforward procedure. Also, It does not require any additional software to work on the server or client. It manages nodes using SSH.
You can read more about How Ansible works in an overview.
Let’s install and configure ansible
Step 1: Installation of EPEL repository
Ansible is not available in the default repository. So, to install it, we have to enable the EPEL repository. Firstly, we are going to install epel-release. Use the below command to install it.
sudo dnf install epel-release
It may ask for your confirmation. So, press y to accept and hit the enter key.
Step 2: Installing Ansible on CentOS 8 (control node)
As we have completed the installation of the epel repository. So, let’s install Ansible. Use the below command for this installation.
sudo dnf install ansible
It may ask for your confirmation. So, press y to accept and hit the enter key.
Once, the installation is completed, then use the below command to verify the version.
ansible --version
And, you will get the output similar to the below picture

Step 3: Set up for Ansible managed node
There must be python installed on the managed node. The version supported by Ansible on the managed node is Python 2 (version 2.7 or later) and Python 3 (version 3.5 or later).
So, you can use the below command to install python.
To install Python on CentOS 8, the command will be
sudo dnf install platform-python
To install Python on CentOS 7, the command will be
sudo yum install python
It may ask for your confirmation. So, press y to accept and hit the enter key.
Step 4: Installing SELinux dependencies on Ansible managed node
You need to install the SELinux dependencies package, In case, you have enabled selinux on your managed node. So, this affects the option like copy/file/template-related functions in Ansible. To fix it, use the below command to install this package.
Use this command to install on CentOS 8 managed node
sudo dnf install python3-libselinux
To install on CentOS 7 managed node
sudo yum install libselinux-python
It may ask for your confirmation. So, press y to accept and hit the enter key.
Step 5: Setting up ssh authentication
Ansible uses the OpenSSH for remote communication. Ansible supports password and passwordless authentication to execute the commands on managed nodes.
Setting up passwordless login on CentOS 8
Here, we are going to set up passwordless login on CentOS 8.
So, we are going to generate the key using the below command.
ssh-keygen -t rsa -P ""
Here, I’m using -P, so, it will not ask for the passphrase.
Now, we are going to copy the public key to the remote system. Use the below command for that.
ssh-copy-id hasan@192.168.43.10
Setting up password login on CentOS 8
You can also use password-based authentication. For this, you must have installed a sshpass software package. You can find the below command useful for this sshpass software package installation.
sudo dnf install sshpass
Or
sudo yum install sshpass
It may ask for your confirmation. So, press y to accept and hit the enter key.
Step 6: Creating Ansible inventory
There is a file with the name of the hosts, this file contains all the remote host’s information for making a connection using ssh for Linux-based hosts and Winrm for Windows hosts. This file location is /etc/ansible/hosts.
So, let’s edit the inventory file i.e host file. Although I’m using vim editors you can use them as per your choice of editors.
sudo vim /etc/ansible/hosts
In this file, you can create a group of hosts. For example, I’m creating a group of hosts with the name of the ‘webservers’ and the IP or hostname of the managed hosts in the below format. Here, I have two managed nodes.
[webservers] 192.168.43.10 192.168.43.197
Step 7: Checking the Ansible connectivity
(Optional Step)
For example, we are going to ping the managed host from the control node.
So, you can use the below command
ansible -m ping webservers
Here, -m to load module, and ‘webservers‘ are the name of the group. You can also type replace websters with all. You will get the output like in the below picture.

Secondly, If you are using passwordless authentication. And the remote user is different from a logged-in user on the Ansible server. Then the command will be as below.
ansible -m ping -u sahil webservers
Here, the group name is webservers.
Thirdly, if you are using passwordless authentication for all the servers. And, the remote user is the same as the user logged on the Ansible server. In this case, the command will be as below.
ansible -m ping all
Lastly, If you are using password authentication and the remote user is different from the logged-in user on the Ansible server then the command will be as below.
ansible -m ping webservers -u sahil --ask-pass
Here, using the –ask-pass argument, it will ask for password authentication. Usr name is sahil.
Step 8: Execute the command on nodes
(Optional Step)
In this step, we are going to use the command module along with an ansible command to get the remote host node information.
Firstly, we are going to check the hostname of the remote nodes
ansible -m command -a "hostname" webservers
Secondly, you can check uptime
ansible -m command -a "uptime" webservers
In the same way, you can execute any command through ansible using the command argument.
For the third example, I am going to redirect the output of the ansible command in the file name ansible_redirection
ansible -m command -a "ls -l /etc" webservers > /tmp/ansible_redirection
Step 9: Making configuration changes on nodes
(Optional Step)
You can use the lineinfile module to perform this task along with the ansible command. Using this command we are going to change the name server in resolv.conf file. Use the below command for that.
ansible -m lineinfile -a "path=/etc/resolv.conf line=nameserver 1.1.1.1" webservers
You can validate using the below command.
ansible -m command -a "cat /etc/resolv.conf | grep -i nameserver" webservers
Conclusion
Some detailed information about Ansible at a glance. Inventory locations:
Default: /etc/ansible/hosts
Specified by CLI: ansible -i file_name
Configuration file: /etc/ansible/ansible.cfg
In this article, we learn to install and configure Ansible on CentOS 8. This is a basic tutorial. You can read about the YAML file in deep. In conclusion, you can install Ansible now. Therefore, Keep practicing. So, I hope, you understand, but if you have any questions, you can ask in the comment section.
You can read about A bash script file to print welcome to the Linux
install and configure ansible | install and configure ansible | install and configure ansible | install and configure ansible | install and configure ansible | install and configure ansible | install and configure ansible