How to install Ansible on Ubuntu 20.04

How to install Ansible on Ubuntu 20.04

As an administrator or operations team, you need to choose the configuration management system, in this article you will learn how to install Ansible on Ubuntu 20.04. these systems are designed to streamline the process of controlling large numbers of servers. They also allow you to control many different systems in an automated way from one central location. Between the popular configuration management tools, such as Chef and Puppet, Ansible by offering an architecture, is a great choice that doesn’t require special software to be installed on nodes, using SSH to execute the automation tasks and YAML files to define provisioning detail.



The tutorial may be more useful if you know:


Recommended Article: Tutorial Configure MongoDB Remote Access on Ubuntu 20.04

How to install Ansible on Ubuntu 20.04

1- Installing Ansible

To install Ansible, first, refresh your system’s package index

sudo apt update  

After the update, install the Ansible software

sudo apt install ansible  

To confirm the installation, press Y if it prompted. To administer your hosts, the Ansible control node has all of the required software now.


2- Set up the inventory File

You would be able to manage information about the hosts with Ansible which is available on the inventory file. Also, you can organize the hosts into groups and subgroups. The inventory file has the ability to include one to several hundred servers and set variables that will be valid only for specific hosts or groups if it is used for playbooks and templates. some variables as  ansible_python_interpreter may affect the way a playbook is run.


Open the /etc/ansible/hosts file to edit the contents of your default Ansible inventory, by your text editor, on your Ansible control node:

The example below defines a group named [servers] with three different servers in it, each identified by a custom alias: server1server2, and servers. The default inventory file provided by the Ansible installation contains a number of examples that you can use as references for setting up your inventory. When you are running the command, make sure of replacing the highlighted IPs with the IP addresses of your Ansible hosts.

[servers]  server1 ansible_host=  server2 ansible_host=  server3 ansible_host=    [all:vars]  ansible_python_interpreter=/usr/bin/python3    

Save and close the file by pressing CTRL+X then Y and ENTER to confirm your changes. If you want to check your inventory, run the following command.

ansible-inventory --list -y  

The output should be as below.

all:    children:      servers:        hosts:          server1:            ansible_host:            ansible_python_interpreter: /usr/bin/python3          server2:            ansible_host:            ansible_python_interpreter: /usr/bin/python3          server3:            ansible_host:            ansible_python_interpreter: /usr/bin/python3      ungrouped: {}


Buy Linux Virtual Private Server


3- Testing Connection

It is time to check if Ansible is able to connect to these servers and run commands via SSH. as the Ubuntu root account is typically the only account available by default on newly created servers, you use it in this process. But if your Ansible hosts already have a regular sudo user created, you are encouraged to use that account instead. To specify the remote system user use the -u argument. Ansible will try to connect as your current system user on the control node, when not provided.

From your local machine or Ansible control node, you can run:

ansible all -m ping -u root  

The above command will use Ansible’s built-in ping module to run a connectivity test on all nodes from your default inventory, connecting as root. The ping module will test:

1. Are hosts able to run Ansible modules using Python or not.

2. Do you have valid SSH credentials or not.

3. Are hosts accessible or not.

Then, the output should display as below:

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

In case you are connecting to these servers via SSH for the first time, you should confirm the authenticity of the hosts you’re connecting to via Ansible. Type yes and then ENTER to confirm when you are asked. When you get a pong reply back from a host, you can run Ansible commands and playbooks on that server.


 4- Running Ad-Hoc Commands

When your ansible control node is able to communicate with your hosts, start running ad-hoc commands and playbooks on your servers. Let’s see an example that you can check disk usage on all servers with:

ansible all -a "df -h" -u root
server1 | CHANGED | rc=0 >>  Filesystem      Size  Used Avail Use% Mounted on  udev            3.9G     0  3.9G   0% /dev  tmpfs           798M  624K  798M   1% /run  /dev/vda1       159G  2.3G  157G   2% /  tmpfs           3.9G     0  3.9G   0% /dev/shm  tmpfs           5.0M     0  5.0M   0% /run/lock  tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup  /dev/vda15      105M  3.6M  101M   4% /boot/efi  tmpfs           768M     0  768M   0% /run/user/0    server2 | CHANGED | rc=0 >>  Filesystem      Size  Used Avail Use% Mounted on  udev            2.0G     0  2.0G   0% /dev  tmpfs           395M  608K  394M   1% /run  /dev/vda1        88G  2.2G   86G   3% /  tmpfs           2.0G     0  2.0G   0% /dev/shm  tmpfs           5.0M     0  5.0M   0% /run/lock  tmpfs           1.0G     0  1.0G   0% /sys/fs/cgroup  /dev/vda15      115M  3.6M  111M   4% /boot/efi  tmpfs           375M     0  375M   0% /run/user/0    ...

You can replace your preferred command with the highlighted command df -h. To test the connection, you can execute Ansible modules via ad-hoc commands. For example, you can use the apt module to install the latest version vim on all the servers in your inventory:

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

While running Ansible commands, you also can target individual hosts, as well as groups and subgroups. Check the uptime of hosts in the servers group.

ansible servers -a "uptime" -u root  

Specify multiple hosts by separating them with colons:

ansible server1:server2 -m ping -u root  


Dear user, we wish this tutorial how to install Ansible on Ubuntu 20.04 would be helpful for you, to ask any question or review the conversation of our users about this article, please visit Ask page. Also to improve your knowledge, there are so many useful tutorials ready for Eldernode training.


Also, see

Initial server set up on Ubuntu 20.04 LTS

Tutorial connect to ssh on linux

We Are Waiting for your valuable comments and you can be sure that it will be answered in the shortest possible time.

Leave Your Comment

Your email address will not be published.

We are by your side every step of the way

Think about developing your online business; We will protect it compassionately

We are by your side every step of the way


7 days a week, 24 hours a day