How to Install Ansible on Ubuntu

Ansible is an open source command line tool that is built for the purpose of configuration management and task automation on multiple systems. Further, it doesn’t require any hefty installations, which makes it a choice for many users as it simplifies the management process and reduces overhead. It is mostly used for Linux system like Ubuntu and there are several ways to install it like using Ubuntu default app installer or using Pip package installer.

4 Ways to Install Ansible on Ubuntu

Ansible uses the YAML files for declaration, which makes it easier to write and understand playbooks, even for non-technical users. Further, it comes with modules for common tasks like file transfers, service management, and package installation. To install Ansible on Ubuntu, there are four ways which will be discussed along with some steps for configuring it.

1: Through Ansible PPA Repository

The first and the official method for installing Ansible on Ubuntu is using its personal package archive repository. The PPA repositories are primarily used for Ubuntu and other Debian-based systems, especially for those applications which are not present in their own repositories. To add Ansible PPA repository, use the following command:

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

Adding Ansible PPA repository on Ubuntu

After adding the Ansible PPA repository, you first need to reload the systmd service and then update the apt packages list to apply the changes:

sudo systemctl daemon-reload
sudo apt update

 

Re-loading systemd service and updating apt packages list on Ubuntu after adding Ansible PPA

Now, you are ready to install Ansible and for that run the below command:

sudo apt install ansible -y

Installing Ansible on Ubuntu using its PPA repository

To verify the Ansible installation you can check its version and for that use the below command:

ansible --version

Checking Ansible version on Ubuntu to verify its installation via PPA

2: Through Ansible Repository

Another way to install Ansible is by adding a GPG key in the sources directory that points to the PPA repository for Ansible. Moreover, it doesn’t sign the repositories for any releases, so download the GPG key for Ansible by running the below command:

wget -O- "https://keyserver.ubuntu.com/pks/lookup?fingerprint=on&op=get&search=0x6125E2A8C77F2818FB7BD15B93C4A3FD7BB9C367" | sudo gpg --dearmour -o /usr/share/keyrings/ansible-archive-keyring.gpg

Adding GPG key for Pointing to Ansible PPA repository on Ubuntu

Next, first you need to save the Ubuntu codename based on the version you are using, since here I am using the jammy version, so I have used this name, Next download and save the Ansible PPA pointed by the GPG key you added previously:

UBUNTU_CODENAME=jammy

echo "deb [signed-by=/usr/share/keyrings/ansible-archive-keyring.gpg] http://ppa.launchpad.net/ansible/ansible/ubuntu $UBUNTU_CODENAME main" | sudo tee /etc/apt/sources.list.d/ansible.list

Adding Ansible repository on Ubuntu

Once you have added Ansible repository, update the apt packages to apply the changes in the repository:

sudo apt update

Updating apt packages list after adding Ansible repository on Ubuntu

Now use the apt package manager to install Ansible as in the command below:

sudo apt install ansible -y

Installing Ansible on Ubuntu after adding its repository

Further, to verify Ansible installation, you can list all the apps installed via apt by running the following command:

sudo apt list --installed

Listing apt installed apps on Ubuntu to validate Ansible installartion.

3: Through Pip Package Installer

If you are looking to work in an isolated environment or want to use Ansible in a virtual environment, then you should install it via pip package installer. This method is beneficial for those users who want to test any app or project they are working on which requires certain Ansible version. To install Ansible via pip on Ubuntu you first need to create a virtual environment followed by the creating a dedicated directory:

mkdir ansible_install
sudo python3 -m venv ansible_install

Creating Ansible directory and creating a virtual environment on Ubuntu

Next, use the pip package installer to install Ansible using the below command:

python3 -m pip install ansible

Installing Ansible on Ubuntu using pip package installer

Once the installation is completed, you will see a message for successfully installed Ansible at the end as in the image below:

Successfully installed Ansible-10.3.0 on Ubuntu via pip

Further, if you are looking for a specified version of Ansible, then use the below command syntax:

python3 -m pip install ansible==[desired-version]

Installing desired version of Ansible on Ubuntu via pip pacakge manager

Note: While executing the package installation command for Ansible in virtual environment, you might get the error of permission denied. This is mainly due to the permissions issue for the subdirectories of virtual environment directory. In that case, you need to simply use the following syntax:

sudo chmod 777 [path-to-directory-in-virtual-environment]

4: Through Ubuntu Default Package Installer

The fourth and the last method to install Ansible is using the apt package manager, which is the most straight forward and easy method among all the others. To install Ansible via apt app installer, use the below command:

sudo apt install ansible -y

Installing Ansible on Ubuntu using apt package manager

Now in order to validate the Ansible installation you can either list apt installed apps or check its version using the following command:

ansible --version

Checking Ansible version to validate its installation on Ubuntu

How To Configure Ansible on Ubuntu

Ansible is a primarily a command line tool for automating tasks and accessing other systems via CLI, so to use it either on client or host it needs to be configured. Here are some steps in this regard that you need to follow to configure Ansible on Ubuntu and other Linux systems:

1: Create Ansible Directory

The first step in Ansible is its directory creation, which in some cases may be automatically created depending on the method you choose for its installation. So to create Ansible directory use the mkdir command and then create a host file which again in some cases will be created automatically:

sudo mkdir -p /etc/ansible
sudo nano /etc/ansible/hosts

Creating Ansible directory and hosts file on Ubuntu

2: Add Host IP addresses

Now, to access the systems via Ansible you need to give IP address to Ansible and for that you need to add them in its host file using the below syntax:

[category]
[server-ip-address]

Adding Host IP address that are to accessed from Ubuntu using Ansible

Once you have added the IP addresses, you can verify by listing the Ansible inventory using the below command:

ansible-inventory --list -y

Listing Ansible inventory on Ubuntu to verify the added host's IP addresses

3: Generate and Share SSH Key

To establish a secure connection you need to use the OpenSSH service and for that first you need to generate is SSH key by executing the below command:

ssh-keygen

Generating SSH key on Ubuntu for accessing hosts via Ansible

 

Next, copy the SSH ID and share it with the host you want to access using its IP address as in the syntax below:

ssh-copy-id [user-name]@[IP-address]

Copying the generated SSH key to host system to establish Ansible connection

3: Test Ansible Connection For all Hosts

Once you have performed all the above steps now you can test the connections by pinging all the added hosts in Ansible using the below command:

ansible all -m ping

Testing Ansible connection for hosts on Ubuntu

Conclusion

This guide discussed four ways to install Ansible on Ubuntu which included using the PPA repository, Ansible repository, pip package installer and apt package manager. Further, this guide discussed some necessary steps for configuring Ansible on Ubuntu. Among all the methods, the recommended one is using the apt package manager because using the repositories you will also get the same version other than using the Ansible repository.

 

Index
Scroll to Top