Google Cloud Compute Engine

How to Install and Setup Ansible on Ubuntu 20.04

Disclosure: This post may contain affiliate links, which means we may receive a commission if you click a link and purchase something that we recommended.

Pinterest LinkedIn Tumblr

Install and Setup Ansible on Ubuntu 20.04. Ansible is a configuration management tool which offers an architecture that doesn’t require special software to be installed on nodes, using SSH to execute the automation tasks and YAML files to define provisioning details.

In this guide you are going to learn how to install and configure Ansible on a Ubuntu 20.04 server.

This setup is tested on Google Cloud Platform, so it will work fine on any cloud service providers or VPS or any dedicated servers.


Root access to your server or user access with sudo privileges

Step 1: Initial server setup

Start by updating the packages to the latest version available.

sudo apt update
sudo apt upgrade

Step 2: Install Ansible

Once all packages are updated you can proceed to install Ansible.

sudo apt install ansible

Once the installation is completed you can check the Ansible version installed using the following command.

ansible --version

You will get an output similar to the one below.

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, Jul 16 2020, 14:00:26) [GCC 9.3.0]

Step 3: Setup Inventory File

The inventory file contains all information about the hosts that you will manage with Ansible.

To configure inventory file you can open the /etc/ansible/hosts file and adjust the configurations.

sudo nano /etc/ansible/hosts file

Here you can configure multiple groups and their own variables.

Provided a sample configuration of remote server connection details.

gcp_instance_1 ansible_host=EXTERNAL_IP


aws_instance_1 ansible_host=EXTERNAL_IP



Here we have created two groups google_cloud and aws and there own corresponding variables with SSH username and the private keys.

You can check the inventory using the following command.

ansible-inventory --list -y

You will receive and output similar to the one below with the respective variables in the corresponding group.

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

You need to add the public key to the remote host and configure the private key to make a successful connection.

Once you have configured all required details you can proceed to test the connection.

Step 4: Test Connection

Now Ansible should be able to connect to the servers listed in the inventory file using SSH.

To check connection on all servers you can use the following command.

ansible all -m ping

To check connection on a specific group you can use this command

ansible google_cloud -m ping

The ping module will test

  • if hosts are accessible;
  • if you have valid SSH credentials;
  • if hosts are able to run Ansible modules using Python.

You will get an output similar to the one below.

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

If the configuration is not made properly you will get this message.

gcp_instance_1 | UNREACHABLE! => {
  "changed": false,
  "msg": "Failed to connect to the host via ssh: [email protected].238.130.101: Permission denied (publickey).",
  "unreachable": true

If you have any connection problems you need to provide proper SSH credentials and make sure you can connect using that credentials.


Now you have learned how to install and setup Ansible and connect to remote servers.

Thanks for your time. If you face any problem or any feedback, please leave a comment below.

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.