How to Install Ansible on Ubuntu 22.04 | Step-by-Step

November 9th, 2023
How to Install Ansible on Ubuntu 22.04 | Step-by-Step

Ansible is an invaluable tool for system administrators and DevOps experts who seek to automate their IT workflows effectively. Ansible's capability excels whether you're handling automation for a lone server or a sprawling network. This article offers detailed, step-by-step guidance on how to install Ansible on Ubuntu 22.04, accessible even for those new to automation tools.

Using Ubuntu 20.04? Check out our previous guide on how to install and configure Ansible on Ubuntu 20.04 .

What is Ansible?

Developed by Red Hat, Ansible is an open-source tool renowned for its simplicity and flexibility in IT automation. Unlike other management tools, it doesn’t require custom agents on nodes and uses SSH for secure connections, making it lightweight and easy to adopt. Ansible stands out as an exceptional automation tool, streamlining complex software setups, managing tasks with ease, and facilitating efficient application deployments.

What is ansible used for?

Ansible makes configuration management, application deployment, and orchestrating complex workflows easier. Its declarative language lets you describe your infrastructure in code, which can be version-controlled and shared among team members. Ansible's capability to handle multiple tasks simultaneously and its modular design makes it a go-to choice for scalable and efficient automation.

Prerequisites

  • To show how to install and set Ansible in this guide, we use three computers with Ubuntu 22.04. One is the control machine, and the other two are managed hosts.
  • On the control machine, we install Ansible. This machine uses SSH to manage the hosts.
  • An Ansible host is a computer that the control machine controls using Ansible. This means any computer that the control machine can automate tasks on.
  • You need sudo rights to run commands in the terminal.

Run your deployments in a scalable and cost-effective open cloud infrastructure. Cherry Servers' secure virtual private servers offer automatic scaling, flexible pricing, and 24/7 technical support. Our pre-defined Ansible module helps you automate the installation and configuration.

How to install Ansible on Ubuntu 22.04: 6 Steps

Installing Ansible on Ubuntu is a straightforward process, especially when compared to other complex IT systems. Below, we will show how to install Ansible on Ubuntu 22.04 in six steps. The Ansible installation mainly involves setting up the control machine and establishing reliable communication channels with the managed hosts.

The key steps to install Ansible on Ubuntu 22.04 include the following:

  • Verifying sudo privileges and updating system packages
  • Adding Ansible PPA
  • Installing Ansible and verifying the installation
  • Setting up the host machine
  • Setting up SSH keys in host machines

Step 1: Verify sudo privileges and update system packages

Open the Ansible control machine terminal and run any sudo command, like sudo apt update, to see if you encounter any errors. If you don't see any errors, you have the necessary sudo privileges. Moreover, run the following command to check the Ubuntu version of your control machine.

lsb_release -a

check ubuntu version

It is always a good practice to update the existing packages and repositories before starting with the Ansible installation. It ensures the packages have their latest versions, installs security patches, and resolves dependencies. It also helps maintain consistency with the new software installation.

Open the terminal of the Ansible control machine and type sudo apt update command. It will fetch the latest updates, as shown in the following screenshot.

fetch the latest updates

Next, execute the following command to upgrade all the installed packages on your system to their latest versions based on the information obtained from the previous command.

sudo apt upgrade -y

upgrade the installed packages

Step 2: Add Ansible PPA (Personal Package Archive)

The Ansible Personal Package Archive (PPA) repository allows developers to update Ansible with the latest versions. Ansible PPA shares the latest software, especially when the official Ubuntu sources do not have the latest packages.

The Ansible PPA provides an easy way to install the latest stable Ansible versions on the Ubuntu operating system. It allows them to always have the most up-to-date releases without relying solely on the official channels, which may be a bit behind. Following is the command used to add the package archive.

sudo apt-add-repository ppa: ansible/ansible

add the package archive

While fetching the package, it will request authorization to proceed. Once you press enter, it will start adding the PPA.

request authorization

Next, execute the sudo apt update command again to update the system package index. This command is important for the Ubuntu system to identify the newly added PPA.

identify the newly added PPA

Step 3: Install Ansible and verify the installation

With the Ansible PPA added you can now continue to install Ansible on Ubuntu. Use the following command to initiate the installation. This command fetches and installs the latest stable version of Ansible from the PPA.

sudo apt install ansible

fetches and installs the latest stable version of Ansible from the PPA

This installation will also request authorization to proceed. Type Y and press Enter to continue with the Ansible installation.

request authorization to install ansible

You can also use the following combined command to install Ansible without interruption.

sudo add-apt-repository --yes --update ppa:ansible/ansible

Verify the installation using the following command.

ansible --version

check ansible version PPA

It will provide the following information regarding the installation: Ansible configuration, Python version, and collection and module information.

  1. ansible [core 2.15.5] - Currently installed Ansible version;
  2. config file - The Ansible configuration file path;
  3. configured module search path - Directories where Ansible will search for modules;
  4. Ansible Python module location;
  5. ansible collection location - Collections are a distribution format for Ansible content, including playbooks, Ansible roles, modules, and plugins;
  6. executable location: The path to the Ansible executable;
  7. Python version;
  8. jinja version - The version of the Python templating engine. Ansible uses Jinja2 for templating needs, such as generating template configuration files;
  9. libyaml = True - This line indicates the user of the LibYAML library. LibYAML allows faster reading of YAML files. It is better to set it to true since Ansible relies heavily on YAML for tasks such as playbooks and settings.

The machine will act as the “Ansible control node” after completing the above steps.

Step 4: Host machine setup

After setting up the Ansible control node, the next step is to prepare the host machines where Ansible tasks will be executed. You need to add these machines to the control node’s inventory file, which is typically located at /etc/ansible/hosts. The following image shows an example of this file.

host machine setup

The Ansible inventory file contains information about all the hosts that Ansible will manage. It is created by default when you install Ansible. In this file, you have the option to organize hosts into different groups based on their roles or functions, like web servers, database servers, and development servers, or categorize them by operating system. This organizational structure helps in managing the hosts more effectively.

In the inventory file, you can list the hosts either by their hostname or IP address, and the file typically includes examples to guide you in specifying these details.

To edit the inventory file, use one of the following commands.

vi /etc/ansible/hosts
sudo nano /etc/ansible/hosts

Moreover, if you wish to create a custom inventory file, it can be done in any location. Use the '-i' parameter when executing Ansible playbooks or commands.

For this step, assign the following private IP addresses to your host machines under the 'AppServers' group, then save the changes.

[appServers]

172.31.21.153

172.31.19.136

assign ip addresses to host machines

Use the following command to see the servers that have been added to your inventory file.

ansible-inventory --list -y

view the servers that have been added to your inventory file

Discover how Tempesta, an open-source application delivery controller (ADC), leveraged Cherry Servers' bare metal cloud to complete tests and validation of their ADC successfully, benefiting from 99.97% uptime, server customization, and 24/7 technical support.

Step 5: Set up SSH keys in host machines

Before managing the host machine with Ansible, you must properly set up the SSH keys. It will allow the Ansible control node to access the host machines.

First, create an SSH key in the Ansible control node using the following command.

ssh-keygen

You can use the cat id_rsa.pub command to visualize the rsa public key and copy it.

visualize the rsa public key and copy it

Login to the console of the host machines. Then, paste and save the copied RSA public key in the authorized_keys file in the /root/.ssh/ path.

paste and save the copied RSA public key in authorized_keys file

Step 6: Test the host machine access

You've now set up everything to access the host machine from the Ansible control node. It's time to verify if the connections are successful. To test the access, use the following command.

ansible all -m ping -u root

Finally, if the connection is successful, you will see the SUCCESS message as in the following screenshots. The first time you connect, you will be prompted to provide authorization. The subsequent test will directly connect to the host servers. Now, you can use the Control Node to manage the host machine and automate your tasks.

check connection status

Conclusion

To summarize, this tutorial showed how to install Ansible on Ubuntu 22.04, including the configuration. We’ve highlighted the importance of the preliminary updates to the control machine and the correct configuration of the host machine, as well as adding Ansible PPA and SSH key setup so that the Ansible control node can successfully manage these machines through SSH.

Ansible has a myriad of pre-built playbooks, modules, and plugins that you can use each with its capabilities and limitations. You can find in-depth Ansible documentation on the official [Ansible website(https://docs.ansible.com/).

Deploy your infrastructure and manage server configuration automatically with a minimal learning curve utilizing Cherry Servers' Ansible module.

Shanika is a technical consultant and writer with over eight years of experience as a software engineer in the IT sector. Her professional journey started as a software engineer with WSO2. At the same time, she started working as a freelancer on Upwork. She has collaborated with numerous companies throughout her freelance career, including Digication, Splunk, BMC.com, Filestack, APILayer, Flosum, Blazemeter, Sencha, and over twenty others. Having opportunities to work with various companies in different roles has allowed her to amass a wealth of experience. Shanika is an expert in web development, programming, Java, Python, React, Cypress, CI/CD, Docker, and Kubernetes,m. She has significantly contributed to developing products such as IAM solutions, APIs, OCR technologies, test management systems, and front-end frameworks throughout her career. She has also produced blog articles, tutorials, user guides, product documentation, and many other documents, as well as consulting companies to enhance their productivity. Overall, Shanika brings together the experience of a web developer, automation engineer, DevOps developer, software consultant, and technical writer, which is the main reason behind her success as a freelancer. Shanika received her B.Sc. (Hons) in Computer Science from University of Moratuwa, Sri Lanka and resides in Colombo, Sri Lanka.

Start Building Now

Deploy your new Cloud VPS server in 3 minutes starting from $5.83 / month.

We use cookies to ensure seamless user experience for our website. Required cookies - technical, functional and analytical - are set automatically. Please accept the use of targeted cookies to ensure the best marketing experience for your user journey. You may revoke your consent at any time through our Cookie Policy.
build: 0cab4b92.684