Google Cloud Compute Engine

How to Install Ansible on Ubuntu 22.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 Ansible on Ubuntu 22.04 and configure it to connect to remote servers to automate several tasks. 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 Ansible on a Ubuntu 22.04 server and configure it to connect with remote servers on Google cloud and AWS instances.


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.10.8
  config file = None
  configured module search path = ['/home/pappinvijak/.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.10.4 (main, Apr  2 2022, 09:04:19) [GCC 11.2.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

Here you can configure multiple groups and their own variables.

Provided a sample configuration of remote server connection details to connect to Google cloud and AWS instances.

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]: 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 Ansible on Ubuntu 22.04 and configure it to connect to remote servers on Google cloud and AWS.

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.