How to Install Ansible on Ubuntu 20.04

What is Ansible?

Ansible is an open-source tool for server provisioning, configuration management, and automatic application deployment.

It’s a simple tool that makes your life as system administration easier, improves the scalability, consistency, and reliability of your IT system environment.

Ansible is written in Python, can run on Unix-like operating systems such as Ubuntu, CentOS, and RHEL.

Ansible is an agentless configuration management tool. It runs through SSH service and can manage and provision both Unix-like systems and Microsoft Windows.

Why using Ansible?

Nowadays, automatic deployment is everything in IT industries. And ansible comes helps you to manage your complex IT infrastructures. It is powerful and easy to set up and use.

Ansible was flexible because it can deploy your application on the complex environment with no time and do not need extra software for its setup.

What we will do?

For this tutorial, we will show you how to set up Ansible on Ubuntu 20.04 Server.

We will install Ansible, setup inventory configuration, setup SSH authentication, and test the connection between the Ansible server and clients.

Step 1 – Install Ansible

First, we will install the Ansible to the Ubuntu 20.04 Server. by default, the Ubuntu FocalFossa provides the latest Ansible version 2.9 it’s repository.

Show available ansible package on the Ubuntu system using the apt command below.

apt show ansible

Below is the result you will get.

Install Ansible on Ubuntu 20.04

As can be seen, the Ubuntu FocalFossa repository provides an Ansible 2.9.

Next, install the Ansible using the apt following command.

sudo apt install ansible

Once the installation is finished, check the Ansible version using the following command.

ansible --version

And will get the result as below.

Ansible Check version

As a result, you’ve successfully installed the latest version of Ansible 2.9 on Ubuntu 20.04 server.

Step 2 – Setup Ansible Inventory

Ansible inventory is a configuration file that defines hosts and groups of hosts which managed by Ansible. It helps you to manage hosts for multiple environments such as development, staging, and production.

By default, the Ansible has an inventory configuration file on the ‘/etc/ansible’ directory. Also, you can create a custom inventory file for your environment, and call it with the ‘-i’ option on the ansible command.

In this step, we will edit the default inventory configuration and add server hosts into it.

Now go to the ‘/etc/ansible’ directory and edit the inventory configuration ‘hosts’ using vim editor.

cd /etc/ansible/
vim hosts

– For Single Node Server

To manage single node server, you can just add all server IP address to the ‘/etc/ansible/hosts’ configuration file.

Below is an example of the Ansible inventory configuration for single server.

– For Group of Server

To manage multiple servers using Ansible, you can create a group which have contain multiple server nodes with different IP addresses.

This is allows you to deploy your application or service to multiple servers with the single Ansible command.

Below is an example of Ansible inventory configuration for group named ‘dbserver’ with managed hosts ‘node01’ and ‘node02’.

The name ‘node01’ is resolved to server with IP address ‘’, and the ‘node02’ is resolved to server with IP address ‘’. Both names resolved by the ‘ansible_host’ option.

[dbservers]ansible-inventory --list
node01 ansible_host=
node02 ansible_host=

Next, for managed server with Ubuntu 18.04 or above, add the following configuration to specify with the Python version 3. And for server with Ubuntu 20.04, continue without the following config.


Save and close.

Next, check all hosts on the ansible inventory using the ‘ansible-inventory’ command below.

ansible-inventory --list

And you will get the result as below.

    "_meta": {
        "hostvars": {
            "node01": {
                "ansible_host": ""
            "node02": {
                "ansible_host": ""
    "all": {
        "children": [
    "dbserver": {
        "hosts": [
    "ungrouped": {
        "hosts": [

As a result, you will get detail about Ansible hosts from inventory configuration.

Additionally, you can check the simple and readable list of managed hosts using the ‘–graph’ option as below.

ansible-inventory --graph

Below is the result you will get.

|[email protected]:
| |--node01
| |--node02
|[email protected]:
| |--
| |--

As can be seen, you’ve a group ‘dbserver’ and two IP addresses which is ungrouped.

As a result, you’ve successfully set up a simple Ansible inventory for single server node and group of server nodes.

Step 3 – Setup SSH Authentication for Ansible

The Ansible uses the SSH service for managing hosts. And you can the SSH service with password-based authentication or using the key-based authentication.

For this guide, we will use the SSH with key-based authentication.

In this step, we will generate the SSH key on the Ansible node and upload the public key to all hosts.

Generate the SSH key on the Ansible node using the command below.


As a result, the SSH public and private key have been generated to the “~/.ssh/” directory.

Generate SSH key

Next, we will upload the public key to all servers as a ‘root’ user and using the ‘ssh-copy-id’ command as below.

ssh-copy-id [email protected]
ssh-copy-id [email protected]

Upload SSH Public Key using ssh-copy-id

Once the public key uploaded to all hosts, you can connect to each host with the user ‘root’ thorugh SSH without any password.

ssh [email protected]
ssh [email protected]

As a result, you’ve successfully set up SSH using key-based authentication.

Step 4 – Test Ansible Connection

After configuring the SSH key-based authentication, we will test our configuration from using the ansible command.

We will test to ping all hosts and make sure all hosts replying with the pong message.

Test using the ansible command below

ansible all -m ping -u root

And you will get the result as below.

Ansible Ping Ubuntu 20.04 hosts

As a result, you get the ‘pong’ message from all hosts. And you’ve successfully setup Ansible hosts using the key-based authentication.

Step 5 – Ansible Ad-Hoc Command

In this step, we will test the ansible setup using an ‘Ad-Hoc’ command.

You can use the Ansible ‘ad-hoc’ command for checking the host directly using the one line Ansible commandYou can use to check all hosts managed by Ansible, also check only a single-host server.

– Using Ad-Hoc Command for all hosts

To run ansible command for all hosts, you can use the ‘all’ option as below.

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

Below is the result you will get.

Ansible Ad-hoc command

As a result, you get the hostname and detail disk usage of all servers on the Inventory configuration.

– Using Ad-Hoc command for the specific host

For a specific host, you can use the name of the host at the Ansible inventory configuration.

ansible node01 -a "hostname -f" -u root
ansible node02 -a "df -h" -u root

As a result, you will get the hostname of ‘node01’ server and get the details disk of the ‘node02’ server.

Ansible Ad-hoc command for specific hosts

Finally, you’ve successfully installed the Ansible on Ubuntu 20.04 Server.

Write A Comment