How to Install Ansible on Ubuntu 20.04

Install Ansible on Ubuntu 20.04

In this article, we will have explained the necessary steps to install and configure Ansible on Ubuntu 20.04 LTS. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

Ansible is a free and open-source Configuration and automation tool for UNIX-like operating systems. It is written in python and similar to Chef or Puppet but there is one difference and the advantage of Ansible is that we don’t need to install any agent on the nodes. It uses SSH for making communication to its nodes.

Install Ansible on Ubuntu 20.04

Step 1. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.

sudo apt update
sudo apt upgrade

Step 2. Install Ansible on Ubuntu system.

You can simply install ansible by executing the command below:

sudo apt install ansible

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

$ ansible --version

ansible 2.9.6
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.8.2 (default, Agustus 31 2020, 16:13:26) [GCC 9.3.0]

Step 3. Setup Ansible Inventory.

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:

sudo nano /etc/ansible/hosts file
[google_cloud]
gcp_instance_1 ansible_host=EXTERNAL_IP

[google_cloud:vars]
ansible_ssh_user=username
ansible_ssh_private_key_file=path_to_private-key

[aws]
aws_instance_1 ansible_host=EXTERNAL_IP

[aws:vars]
ansible_ssh_user=username
ansible_ssh_private_key_file=path_to_private-key-or-pem-key

[all:vars]
ansible_python_interpreter=/usr/bin/python3

Once done, you can check the inventory using the following command:

$ ansible-inventory --list

all:
  children:
    google_cloud:
      hosts:
        gcp_instance_1:
          ansible_host: EXTERNAL_IP
          ansible_python_interpreter: /usr/bin/python3
          ansible_ssh_user=username
          ansible_ssh_private_key_file=path_to_private-key
    aws:
      hosts:
        aws_instance_1:
          ansible_host: EXTERNAL_IP
          ansible_python_interpreter: /usr/bin/python3
          ansible_ssh_user=username
          ansible_ssh_private_key_file=path_to_private-key-or-pem-key
  ungrouped: {}

Step 4. Test Ansible Connection.

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

ansible all -m ping -u root

You will get an output similar to the one below:

gcp_instance_1 | SUCCESS => {
"changed": false,
"ping": "pong"
}

That’s all you need to do to install Ansible on Ubuntu 20.04 Focal Fossa. I hope you find this quick tip helpful. If you have questions or suggestions, feel free to leave a comment below.