How to Install Ansible on Ubuntu 22.04? (Step by Step Guide)

Ansible is essential for system administrators and DevOps professionals who want to automate their IT workflows effectively. Whether you're overseeing a single server or a vast network, Ansible's robust capabilities can significantly boost efficiency. Ansible is an open-source automation tool, developed by Red Hat, known for its simplicity and versatility. 

Unlike other management tools, Ansible doesn’t need custom agents on nodes and uses SSH for secure connections, making it lightweight and easy to deploy. Ansible streamlines configuration management, application deployment, and the orchestration of complex workflows. Its declarative language lets you describe your infrastructure in code, which can be version-controlled and shared among team members. Ansible's ability to handle multiple tasks simultaneously and its modular design makes it a preferred choice for scalable and efficient automation.

This guide will show you how to install Ansible on Ubuntu 22.04, making it easy to follow even for beginners. You'll learn all the necessary steps to get started with this powerful automation tool on Ubuntu 22.04.

Prerequisites

To demonstrate how to install Ansible on Ubuntu 22.04, we'll use three different machines:

  1. One control machine and two managed hosts. Ansible will be installed on the control machine. This machine will use the SSH to manage the hosts. 
  2. An Ansible host is any computer that the control machine can automate tasks on. Ensure you have sudo privileges to execute commands in the terminal.

How to Install Ansible on Ubuntu? (Installation Steps)

Installing Ansible on Ubuntu is quite simple, especially compared to other complex IT systems. 

This process mainly involves setting up the control machine and ensuring reliable communication with the managed hosts. Follow the below steps to learn how to install Ansible on Ubuntu 22.04:

Step 1: Configure Ansible Control Node

Open the terminal on your Ansible control machine and run a sudo command, such as sudo apt update, to verify if you have the required sudo privileges. If no errors appear, you have the necessary permissions. Additionally, use the following command to check the Ubuntu version on your control machine:

$ lsb_release -a

how to install ansible on ubuntu 22.04? (step by step guide)

The Ansible control node is the system used to connect to and manage Ansible host servers. Follow the steps below to set up the control node on your main server:

Create an administrator-level user for the control node using the ‘adduser’ command:

$ sudo adduser [username]

how to install ansible on ubuntu 22.04? (step by step guide)

You will be prompted to set a strong password for the account. Optionally, you can provide additional details about the user by answering questions during the creation process. Press Enter to skip any questions you don't want to answer. Use the following command to grant superuser privileges (sudo access) to the account:

$ sudo usermod -aG sudo [username]

Being a member of the sudo group allows the user to use the sudo command for administrative tasks.

Step 2: Generate SSH Key Pair

To generate an SSH key pair for the Ansible user on the control node, enter the following command in the command line of the Ansible control node:

$ ssh-keygen

how to install ansible on ubuntu 22.04? (step by step guide)

When prompted, you can optionally provide a passphrase for added security. Pressing Enter allows you to skip passphrase creation.

The system will generate a public/private key pair and display a random art image as confirmation.

Step 3: Set up SSH on the Ansible Host Machine

An Ansible host is a remote server managed by the Ansible control node. Follow these steps for each new Ansible host:

Use the ssh-copy-id command on the control node to copy the public key to the host:

$ ssh-copy-id [username]@[remote-host]

Replace [username] with an existing administrative user on the host system and [remote-host] with the domain or IP address of the remote host. For example, to copy the key to the user ansible on a host with IP address 192.168.0.81, you would type:

$ ssh-copy-id ansible@host-ip

how to install ansible on ubuntu 22.04? (step by step guide)

When prompted, type yes and press Enter to confirm connecting to an authenticated host. Enter the password for the remote host account when prompted. This command uploads the public key to the remote host account.

Step 4: Install Ansible on Ubuntu

After configuring the node machine, we will see how to install Ansible on Ubuntu. Install the Ansible package on the control node system using the APT package manager:

Make sure the package index is up to date using this command:

$ sudo apt update

Install Ansible on Ubuntu with the following command:

$ sudo apt install ansible -y

how to install ansible on ubuntu 22.04? (step by step guide)

Step 5: Verify the Installation

To confirm that Ansible is installed correctly on your Ubuntu system, use the following command:

$ ansible --version

how to install ansible on ubuntu 22.04? (step by step guide)

The output will show details such as the Ansible version number, the location of the configuration file, the path to the executable, and other relevant information.

Step 6: Setup Ansible Inventory File

The Ansible inventory file serves as a repository of information about all the hosts managed by Ansible. It is automatically generated upon installing Ansible. 

This file allows hosts to be organized into groups based on their roles or functions, such as web servers, database servers, or development servers, and can also categorize them by operating system. This organizational structure enhances the efficiency of managing hosts.

In the inventory file, hosts can be listed by their hostname or IP address, typically with examples provided to assist in specifying these details.

To edit the inventory file, you can use either of the following commands:

$ vi /etc/ansible/hosts

or

$ sudo nano /etc/ansible/hosts

Additionally, if you prefer to create a custom inventory file, you can do so in any location. When executing Ansible playbooks or commands, use the -i parameter to specify the custom inventory file.

For this task, add the following private IP addresses to your host machines under the 'servers' category in the inventory file, and then save the changes:

[servers]
Server IP-address
[server-ip or hostname]

Save your changes to the inventory file and exit the editor.

Use the following command to display the items in the inventory:

$ ansible-inventory -i ansible/anventory --list 

how to install ansible on ubuntu 22.04? (step by step guide)

This command will output a list of hosts from the inventory.

Step 7: Test Host Machine Connection

Now that you've configured access from the Ansible control node to the host machines, it's time to verify the connections. Use the following command to test access:

ansible all -m ping -u root

If the connection is successful, you'll see a "SUCCESS" message. Initially, you may be prompted for authorization. Subsequent tests will connect directly to the host servers. You can now utilize the control node to manage and automate tasks across your host machines.

Conclusion

In this tutorial, we covered how to install Ansible on Ubuntu 22.04. We demonstrated how to configure Ansible by updating the control machine, configuring the host machine, and setting up SSH keys for effective management by the Ansible control node over SSH. 

Ansible offers a wide range of pre-built playbooks, modules, and plugins, each with its capabilities and limitations. For more detailed documentation, visit the official Ansible website. Automate your infrastructure deployment and server configuration efficiently with the BlueVPS Ansible module, designed for a minimal learning curve.

Blog