Install Ansible on Linux

How to Install Ansible on Linux

With the advancement of technology today, various configuration management systems have been developed to facilitate the control and management of multiple hosting servers simultaneously. By using configuration management systems, you can automatically control different systems.

Ansible is an open-source automation tool for configuration management in cloud resources. As a system administrator or developer, you can use this tool to automate processes such as application deployment, software installation and expansion, infrastructure provisioning, service management, the configuration of single or multiple nodes from scratch, patching, and many other similar processes; Therefor most of your tasks in controlling and managing configuration changes are done automatically.

Ansible has gained popularity among configuration management tools due to its excellent features, which include the following:

  • Ansible is Agentless and does not rely on Agent and special software to install on a node or system.
  • It is written based on Python programming language.
  • Ansible coding in setting up provisioning details based on YAML files and language.
  • It uses SSH protocol to make configuration changes in connecting to the remote server and communicating and managing systems; So it is a very safe method.

Now that you are familiar with the advantages and privileged features of Ansible, it is time to use Ansible features on Linux VPS because this efficient and popular tool is a product of RedHat company, and the advanced Linux operating system supports this tool well.

As a result, one of the prerequisites for using Ansible is having a Linux VPS. Ansible can be installed and used on Red Hat Enterprise Linux, CentOS, Fedora, Debian, and Ubuntu operating systems. In this article, we will teach you how to install Ansible on Ubuntu and CentOS servers, which are the most popular Linux distributions, so you can follow the end of our article.

Prerequisites for installing Ansible on Linux

Ansible requires a control node to install, which is created by connecting each machine to the remote machine through port 22 (SSH). As a result, you can consider your Linux VPS or a local machine to host and run Ansible. In this tutorial, we have assigned Linux VPS with Ubuntu or CentOS operating system as the Ansible control node. To install Ansible, other factors that must be considered are as follows:

  • User account with sudo privileges
  • ufw firewall configuration
  • External access to the management panel of the non-root user
  • Having the SSH key pair in connection with the desired user
  • Installing Python, which is necessary to manage Linux by Ansible.

How to install Ansible on Ubuntu

If you are interested in using the Ansible configuration management tool, you must install it on the machine you plan to designate as an Ansible control node.

First, you must connect to the Ansible controller that you intend to create (Ubuntu 20.04 server) through SSH and access its command line.

1. Since Ansible requires Python to manage and perform server infrastructure control tasks, your Linux system must support Python programming language. Python is usually installed as the default package in Linux distributions, but if you find that the Ubuntu operating system does not support Python, first install the Python package on your Linux operating system by running the following command:

sudo apt-get install python3

2. Then add the project’s official PPA (Ansible package repository) to the Ubuntu server resources that you have considered as the control node by entering the following command:

sudo apt-add-repository ppa:ansible/ansible

After executing the previous command, accept the PPA installation confirmation request by pressing Enter.

3. Update the Ubuntu System Package Repository to apply the changes you made in the previous step to the Ubuntu System Package Repository:

sudo apt update

4. Now the Ubuntu system is ready to download and install the Ansible tool, for this purpose you must run the following command:

sudo apt install ansible

5. You can make sure that the Ansible installation process is correct by running the following command:

ansible -v

In the output, if Ansible is installed correctly, the version of Ansible that you installed should be displayed.

Therefore you equipped your Ubuntu server with Ansible to manage different hosting servers.

How to install Ansible on CentOS

Follow the steps below to install Ansible on the CentOS7 server:

1. First, connect to the CentOS server from your client through the SSH protocol.

2. If the Python language package is not installed by default on the CentOS server, install it by running the following command:

sudo yum install python3 -y

3. Then, if the CentOS system does not support the EPEL repository, you must install the Extra Packages for Enterprise Linux (EPEL) repository, which includes the Ansible package:

sudo yum install epel-release

4. Update the package list of the CentOS system using the following command so that the default CentOS repository becomes aware of the addition of the EPEL repository:

sudo yum update -y

5. After making sure to install the EPEL repository and update the default CentOS repository, now you can install Ansible on CentOS7:

sudo yum install ansible -y

6. Run the following command to check the success of Ansible installation on CentOS7:

ansible --version


ansible -v

The display of the Ansible version in the output of the previous commands indicates the success of the Ansible installation process on CentOS.

In the next steps, we will explain how to configure Ansible to add your hosts to the control node’s inventory file.

How to configure Ansible controller hosts

For Ansible to perform its management tasks efficiently and communicate with other nodes, it needs to set up an Inventory File that stores information such as IP addresses and names of hosts that you want to manage with Ansible. The contents of the Inventory File can store multiple servers and their information in the form of sets or subsets to make it easier to organize information related to multiple servers.

Another advantage of Inventory File is that you can set variables that are allowed to be used in playbooks and templates of certain hosts.

Of course, creating and editing an Inventory File is not the only way to connect to managed nodes, but it is the most efficient method because you will need the managed server’s IP or hostname to execute commands.

Therefore, in the configuration of Ansible controller hosts, you must first access the default Ansible inventory content of your system and edit it. For this purpose, access the /etc/ansible/hosts file on the Ansible control node through the nano editor (any editor of your choice):

sudo nano /etc/ansible/hosts

By default, Ansible creates the inventory file in the, etc/ansible/hosts directory, but you can change the default location of the inventory file according to your needs. For this purpose, when running Ansible commands, add the -i parameter to the path of your custom inventory file.

You can use our example as a guide for your system inventory settings, in this tutorial we have assigned the Ubuntu server as an Ansible controller host. So pay attention to the following example:

server1 ansible_host=
server2 ansible_host=
server3 ansible_host=

In the example you see, 3 servers with the names server1, server2, and server3 have formed a group. Of course, to use the above example, you have to put your Ansible IP host. We have provided the previous example for a better understanding.

By running the previous example, for existing hosts, the ansible_python_interpreter host parameter will be valid and the remote Ubuntu server will be set to use /usr/bin/python3 Python 3.

After the desired settings, save the /etc/ansible/hosts file by pressing CTRL+X, then Y, and Enter. Then exit the file.

Run the following command to check your inventory:

ansible-inventory --list -y


ansible_python_interpreter: /usr/bin/python3
ansible_python_interpreter: /usr/bin/python3
ansible_python_interpreter: /usr/bin/python3
ungrouped: {}

The output you will get will be similar to our example output but your server’s infrastructure may be different than ours.

Testing Ansible connection to managed nodes

This part of the article is presented to check the Ansible connection to the servers that you have set in the inventory file. To run Ansible connection check commands, it is better to have root user permission because this user account has the necessary permission by default in newly created servers. Of course, it should be noted that if the Ansible hosts have created a normal user with sudo privileges, you should use that user account.

You can use the-uargument to find out the remote server user.

Therefore, to check the connection from the Client side or the Ansible control node, run the following command:

ansible all -m ping -u root


server1 | SUCCESS => {
"changed": false,
"ping": "pong"
server2 | SUCCESS => {
"changed": false,
"ping": "pong"
server3 | SUCCESS => {
"changed": false,
"ping": "pong"

If you are connecting to your default servers through SSH for the first time, respond to the confirmation request about the correctness of the hosts you are accessing through Ansible by entering Yes and then pressing the Enter button.

receiving the Pong message from the server indicates successful Ansible connections and now you can run Ansible commands.

By executing this command, Ansible’s internal ping module is called to test connections on all available nodes. The ping module checks the availability of hosts, the validity of SSH credentials, and the execution of Ansible modules based on Python.

How to use Ansible through Ad-Hoc commands

After starting Ansible on the remote server and making sure that the Ansible control node is connected to the hosts you specified in the inventory file, you can make the process of controlling and managing the hosts you specified more efficient and easier by executing Ad-Hoc commands. Therefore, learning how to use Ad-Hoc one-line commands is very useful in improving the management of your hosts.

One of the advantages of Ansible is that by executing the command on the remote server, it is possible to execute the same command on the servers specified in the inventory file which makes it easier to control your other servers

If you want to check the disk space occupied by your servers, you can run the following command in the Ansible control node:

ansible all -a "df -h" -u root

In the previous command, you can replace the “df -h” parameter with other parameters for different purposes. You can also call Ansible modules using ad-hoc commands. For example, by running the following command, you can install the latest version of vim on your specified servers in the inventory file:

ansible all -m apt -a "name=vim state=latest" -u root

You can also check each of the hosts separately for better management of your hosts. For example, by running Ansible commands, control the desired host or subgroup and monitor the hosts’ uptime:

ansible servers -a "uptime" -u root

You may want to check 2 servers among several servers. For this purpose, you can separate them in the running of the commands as in the example we provide:

ansible server1:server2 -m ping -u root


Any system you can connect to via SSH can be controlled with Ansible.

To install Ansible packages on the Ubuntu server, you must use the apt or apt-get command, the Ansible installation command will be as follows:

sudo apt install ansible

Since Ansible has prioritized ease of use in its design, it has used the YAML format to make reading and writing easier for users. Also, expressing Ansible playbooks are written based on YAML syntax.


Now that you are familiar with the Ansible tool and realize that it is very simple to install it on a Linux operating system, we are sure that you are more interested in using the features of the Ansible configuration management tool; because it is one of the useful tools for Linux VPS administrators to control their multiple servers.

In this article, we taught how to install Ansible on Ubuntu and CentOS servers, which are popular Linux operating systems, and how to set up an inventory file to manage your servers. We also tested connecting the Ansible host node to the servers you want to manage. In addition, in this article, you learned how you can manage and control your infrastructure through an Ansible central controller machine. We hope that by reading this article, you can enjoy the capabilities of the Ansible tool on Ubuntu and CentOS servers.

Leave a Reply

Your email address will not be published. Required fields are marked.