How to Install Ansible on Ubuntu 20.04

Ansible is an Infrastructure as Code tool that allows its users to control many servers from a centralized location. The benefit of using Ansible is that it uses SSH along with YAML files for configuration without any need to require other configurations. In this tutorial, I will describe how to install and configure Ansible on an Ubuntu 20.04 system.

Installation of Ansible on Ubuntu

To install Ansible, use the official Ubuntu 20.04 repository in your system. Open up the terminal window using the Ctl+Alt+T shortcut or use Applications-> Terminal. After this, update the repository using:

$ sudo apt update

Update packages

The apt package repository cache will then be updated. Now, install Ansible using:

$ sudo apt install ansible

Apt install ansible

The system will prompt after a while, press Y from the keyboard, and then hit the enter key to continue.

Install Ansible

Ansible will then be installed. Let’s verify this step by using:

$ ansible –version

Check ansible version

The version installed will be displayed like this:

Which ansible version is installed?

Generate an SSH Key

Now, you need to generate an SSH key on your system where Ansible is being installed. To generate the key, append the command:

$ ssh-keygen

Run SSH Keygen

Once entered in the terminal window, press the enter key.

Generate SSH key

Again press <enter>

Set a passphrase

Now, again you need to hit the <enter> key from your keyboard

Repeat passphrase

As soon as you press enter, an output quite similar to this one will be displayed. It will have an SSH key that will be used in the next half of the tutorial.

SSH key generated

Configuration of Ubuntu hosts to automate Ansible

To automate more than one host, you need to repeat the same process for each of the hosts respectively. All Ubuntu hosts (Ansible) which are to be configured must have the SSH package installed. Now, we will update the apt package using:

$ sudo apt update

Install update on the hosts

The updates will begin quite similar to the one displayed below:

Packages installed

Next step is to install OpenSSH server using:

$ sudo apt install openssh-server -y

Install OpenSSH server

Once done, then you need to check the status of sshd service. Use the following command to check this:

$ sudo systemctl status sshd

Check ssh status

The output will be displayed as soon as you press <enter>. The statuses enabled and active (running) will

SSH Server status

Once you have checked that sshd command is running fine and enabled, then you can proceed. If not enabled then start it manually by using:

$ sudo systemctl start sshd

Start sshd

Now, let’s configure the firewall to allow SSH access by using:

$ sudo ufw allow ssh

Open SSH port in the firewall

An output similar to the one displayed below will appear:

Enable port in IPv4 and IPv6

The next step is to add an ansible user and then allow password-less access. We will now create an ansible user by using:

$ sudo adduser ansible

Add ansible user

Provide the password for the ansible user.

Ansible user added

After that fill in the relevant information against all available fields.

Set a password

To configure the password-less sudo access type the following in the terminal window for your ansible user:

$ echo "ansible ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/ansible

Ansible sudo configuration

Let us check out the IP address of the Ansible host by using:

$ hostname -I


The host will appear in the output.

Show hostname

Now, you know the hostname so, we will be copying the SSH public key to the Ansible host like this:

$ ssh-copy-id [email protected]

Copy ssh key

Copying ssh key from management node

Now, type Yes to proceed.

Proceed ssh key copying

The key will be copied to the host.

To disable password-based login use the command:

$ sudo usermod -L ansible

Disable password based logins

Here we are able to access the Ansible host without any password and it is ready for automation.

password login disabled

Testing Ansible

To test Ansible after the installation and configuration, users need to create a new project directory. Use the mkdir command to do so:

$ mkdir ~/ansible-demo

Create directory

Once, you have created new directory, you need to access it using:

$ cd ~/ansible-demo/

enter folder

After that, create a new host file in the same project directory using:

$nano hosts

Create hosts file

Ansible will be using the hosts in this file to SSH. Once you have opened the nano editor, type the IP address of your host, you want to ping.

Test file

Let’s try to ping all hosts using Ansible by using:

$ ansible all -i ./hosts -u ansible -m ping

Ping ansible hosts

You will see a success like this one displayed below:

Ansible test result

Uninstallation of Ansible

To uninstall Ansible, use the following command in the terminal window:

$ sudo apt remove ansible

Remove ansible

Type Y to proceed with the uninstallation process.

Removing ansible

This way users can easily remove Ansible from their system.


In this article, we saw the Ansible installation process on an Ubuntu 20.04 system and how to configure and test Ansible.