Dec 11, 2023 6 min read

How to Install and Configure Ansible on Ubuntu 20.04

Install the Ansible on Ubuntu 20.04 with our step-by-step tutorial. Ansible is an automation tool that simplifies IT infrastructure management.

Install and Configure Ansible on Ubuntu 20.04
Install and Configure Ansible on Ubuntu 20.04
Table of Contents

Introduction

Before we begin talking about how to install Ansible on Ubuntu 20.04, let's briefly understand - what is Ansible?

Ansible is a powerful open-source automation tool that simplifies IT infrastructure management. It enables users to automate tasks such as software provisioning, configuration management, and application deployment across multiple systems. With its agentless architecture, Ansible eliminates the need for additional software on managed hosts, making it easy to set up and use.

It uses a simple and human-readable language, YAML, for defining automation tasks, making it accessible to both beginners and experienced users. Ansible's flexibility, scalability, and extensive library of pre-built modules make it a popular choice for automating IT operations and accelerating development processes.

In this tutorial, you will install Ansible on Ubuntu 20.04. We will also answer few FAQs based on Ansible Installation.

Advantages of Ansible

  1. Simplicity: Ansible's easy-to-understand YAML syntax makes automation accessible to all.
  2. Agentless: No agents or extra software required on managed systems, simplifying setup and reducing overhead.
  3. Versatility: Automate various tasks, from configuration management to app deployment, across diverse environments.
  4. Scalability: Efficiently manage numerous servers at once, ideal for large-scale infrastructures.
  5. Extensibility: A vast collection of modules and plugins extend Ansible's capabilities to suit unique needs.

Steps 1 - Installing Ansible

  1. To let Ansible manage your server infrastructure, you need to install Ansible software, which will serve as the control node.
  2. From the control node, you can include the official project’s PPA in your system’s list of sources using the following command:
sudo apt-add-repository ppa:ansible/ansible

3. After that, press ENTER to accept the PPA addition.

4. Then refresh the system’s package index so it is aware of all packages available in the newly included PPA:

sudo apt update

5. After this update, install the Ansible software using the following command:

sudo apt install ansible

Now, the control node has all the software necessary to administer the hosts.

Step 2 - Setting Up the Inventory File

It contains all information about the hosts you will be managing with Ansible. It is possible to include multiple servers in your inventory file, and hosts can be organized further into groups as well as subgroups.

The inventory file is useful in setting up the variables that are valid only for specific hosts or groups so that they can be used within playbooks and templates. Some variables can also affect the way in which a playbook is running, like the ansible_python_interpreter.

  1. To edit the contents of the default Ansible inventory, open /etc/ansible/hosts file using your text editor on your Ansible Control Node:
sudo nano /etc/ansible/hosts
💡
Some Ansible installations will not create any default inventory file. The file may not exist in your system. You will then create a new file at /etc/ansible/hosts or can provide a custom inventory path using the -i parameter, when running the commands and playbooks. 

2. The default inventory file contains a number of examples, which can be helpful for you as a reference for setting up the inventory. The following example defines a group named, [servers] with 3 different servers in it and each one of them is identifiable by a custom alias like server1, server2, and server3. You will need to replace these IPs with the IP addresses of your Ansible hosts.

[servers]
server1 ansible_host=203.0.113.111
server2 ansible_host=203.0.113.112
server3 ansible_host=203.0.113.113

[all:vars]
ansible_python_interpreter=/usr/bin/python3
  • The all:vars sub-group sets the ansible_python_interpreter host parameter that will be valid for all the hosts included in this particular inventory. This parameter will make sure that the remote server uses /usr/bin/python3 Python 3 executable instead of /usr/bin/python, that is not present on the recent Ubuntu versions.
  • After finishing, save and close the file by pressing CTRL+X, Y and then ENTER to confirm the changes.
  • In order to check your inventory, you should run:
ansible-inventory --list -y

You will see the output similar to this:

Output

all:
  children:
    servers:
      hosts:
        server1:
          ansible_host: 203.0.113.111
          ansible_python_interpreter: /usr/bin/python3
        server2:
          ansible_host: 203.0.113.112
          ansible_python_interpreter: /usr/bin/python3
        server3:
          ansible_host: 203.0.113.113
          ansible_python_interpreter: /usr/bin/python3
    ungrouped: {}
    ```

Now that the configuration of the inventory file is done, you have everything to test the connection to your Ansible hosts.

Step 3 - Testing Connection

  • Once the inventory file set up is done, now you have to check if Ansible is able to connect to the servers and can run commands via SSH.

    You will need the Ubuntu root account, as it is the only account available by default on newly created servers. If Ansible hosts already have a regular sudo user, you can use that account instead.
  • You will next use the -u argument to mention the remote system user. If not mentioned, Ansible will try to connect as the current system user on the control node.
  • From the local machine or Ansible control node, you will run the following command:
ansible all -m ping -u root

The command will use Ansible’s built-in ping module to run a connectivity test on all nodes from default inventory, it will connect as root. The ping module will test the following:

  • if hosts are accessible properly.
  • you have a valid SSH credential.
  • the hosts are able to run the Ansible modules through Python.

You will get a similar output as follows:

Output 

server1 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
server2 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
server3 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
  • If you are connecting to servers via SSH, you will get a prompt to confirm the authenticity of the hosts you are getting connected to. When you get a prompt, type yes and then press ENTER to confirm.
  • After you get a "pong" reply back from the host. It means you are ready to run Ansible commands and playbooks too on that server.

Step 4 - Running the Ad-Hoc Commands

  • Once you receive the confirmation that the Ansible control node can now communicate with the hosts, it is a good time to start running ad-hoc and playbooks on your servers.
  • Now, any command that can be normally executed with SSH can be run with Ansible on the servers mentioned in your inventory file. For clarity take an example like, you can check disk usage on all servers with the following command:
ansible all -a "df -h" -u root

You will see the following output:

Output 

server1 | CHANGED | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
udev            3.9G     0  3.9G   0% /dev
tmpfs           798M  624K  798M   1% /run
/dev/vda1       155G  2.3G  153G   2% /
tmpfs           3.9G     0  3.9G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/vda15      105M  3.6M  101M   4% /boot/efi
tmpfs           798M     0  798M   0% /run/user/0

server2 | CHANGED | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
udev            2.0G     0  2.0G   0% /dev
tmpfs           395M  608K  394M   1% /run
/dev/vda1        78G  2.2G   76G   3% /
tmpfs           2.0G     0  2.0G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/vda15      105M  3.6M  101M   4% /boot/efi
tmpfs           395M     0  395M   0% /run/user/0

...
  • The command df -h can be replaced by any command of your choice.
  • You can also execute the Ansible modules using ad-hoc commands, which was similar to what we have done before with the ping module for testing the connection. Here's how apt module can be used to install the latest version of vim on all servers in your inventory:
ansible all -m apt -a "name=vim state=latest" -u root
  • You will be able to target the individual hosts. Also, the groups as well as subgroups, when running the Ansible commands. So, this is how you will check the uptime of every host in the servers group, by:
ansible servers -a "uptime" -u root
  • You can even specify multiple hosts by separating them with colons:
ansible server1:server2 -m ping -u root

FAQs to Install and Configure Ansible on Ubuntu 20.04

Can I install a specific Ansible version?

By default, Ubuntu 20.04 repositories offer the latest stable version. To install a specific version, use sudo apt install ansible=version_number.

How do I check Ansible's installed version?

Run ansible --version in the terminal, and it will display the installed Ansible version.

Is SSH necessary for Ansible to work on Ubuntu?

Yes, Ansible uses SSH to connect to remote hosts. Ensure SSH is installed and properly configured on both the Ansible control node and managed hosts.

How do I configure Ansible to work with specific hosts?

Edit the /etc/ansible/hosts file or create a new one to define host groups and their IP addresses or domain names.

What if I want to use a different SSH key for authentication?

In the Ansible hosts file, specify the ansible_ssh_private_key_file variable to point to the desired SSH private key path.

Can I uninstall Ansible from Ubuntu 20.04 if needed?

Yes, you can uninstall Ansible using sudo apt remove ansible. Additionally, use sudo apt autoremove to remove its dependencies no longer needed.

Is it necessary to configure SSH key-based authentication for Ansible on Ubuntu 20.04?

By default, Ansible relies on SSH key-based authentication for communicating with remote hosts. It is recommended to set up SSH keys and ensure that the control node has appropriate SSH access to the managed resources

Conclusion

We hope this detailed tutorial helped you to install and configure Ansible on Ubuntu 20.04.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.