How to Install Ansible on Ubuntu

June 5, 2024

Introduction

Ansible is an agentless Infrastructure as Code tool that allows controlling and monitoring multiple servers from a central location. Its key advantage over similar platforms is that it uses SSH and YAML configuration files and does not require additional specialized software to manage hosts.

This tutorial explains how to install and configure Ansible on Ubuntu.

How to install Ansible on Ubuntu.

Prerequisites

How Do I Install Ansible on Ubuntu?

Installing Ansible on Ubuntu requires setting up an Ansible control node and connecting it to one or more Ansible hosts. The following steps describe how to perform the necessary configuration and test the new Ansible installation.

Step 1: Configure Ansible Control Node

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

1. Create an administrator-level user for the control node. Use the adduser command:

sudo adduser [username]

2. When prompted, define a strong account password.

Adding a user with the adduser command.

Optionally, provide more details about the user by answering questions. Press Enter to skip a question.

3. Use the following usermod command to assign superuser privileges to the account:

sudo usermod -aG sudo [username]

A membership in the sudo group allows the user to utilize the sudo command to perform administrative tasks.

4. Switch to the newly created user on the control node:

sudo su [username]

Note: The Ansible control node can be a dedicated server, a local machine, or a virtual machine running Ubuntu.

Step 2: Set up an SSH Key pair

The Ansible control node uses SSH to connect to hosts. Generate an SSH key pair for the Ansible user by executing the following steps:

1. Enter the command below using the Ansible control node command line:

ssh-keygen

Note: If an SSH key pair with the same name already exists, SSH displays a warning asking the user decide whether to overwrite it. Overwriting makes the previous SSH key pair unusable, so ensure the old keys are no longer needed before confirming.

2. When prompted, provide a passphrase. While adding a strong passphrase is recommended, pressing Enter allows the user to skip the passphrase creation.

The system generates the public/private key pair and prints the randomart image.

Generating a public/private key with the ssh-keygen command.

Step 3: Configure an Ansible Host

Ansible hosts are remote servers managed by the Ansible control node. Each host must have the control node's SSH public key in the authorized keys directory. Apply the steps below for each new Ansible host:

1. Use the following ssh-copy-id command on the control node to copy the public key to a host:

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

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

ssh-copy-id ansible@192.168.0.81

2. Type yes and hit Enter when asked whether to continue connecting to an authenticated host.

3. Enter the remote host account password.

Copying a public SSH key to remote hosts with the ssh-copy-id command.

The utility uploads the public key to the remote host account.

Step 4: Install Ansible

Use the APT package manager to install the Ansible package on the control node system:

1. Ensure the package index is up to date:

sudo apt update

2. Install Ansible on Ubuntu with the following command:

sudo apt install ansible -y

Step 5: Verify the Installation

Check that Ansible was successfully installed on your Ubuntu system using the ansible command:

ansible --version

The output displays the Ansible version number, the location of the configuration file, the path to the executable, and other information.

Test Ansible installation on Ubuntu.

Step 6: Set up the Inventory File

Once Ansible is installed on the control node, set up an inventory file to allow Ansible to communicate with remote hosts. The inventory file contains all the information about the remote hosts managed through the Ansible control node.

Note: For an in-depth overview of creating files on remote hosts, refer to our article How to Create a File In Ansible.

Follow the steps below to create an inventory file on the control node:

1. Create the ansible subdirectory in the etc directory:

sudo mkdir -p /etc/ansible

2. Use a text editor such as Nano to create a file named hosts:

sudo nano /etc/ansible/hosts

3. Add remote hosts that the control node will manage. Use the following format:

[category]
[server-ip-or-hostname]

The [category] line allows for the creation of custom categories to organize available hosts. The following example adds a remote host using its local IP address 192.168.0.81 and sorts it into the servers category:

Editing the default Ansible hosts file.

4. Save the file and exit.

5. Enter the command below to check the items in the inventory:

ansible-inventory --list -y

The output lists the hosts:

Checking the Ansible inventory list with the ansible-inventory command.

Step 7: Test the Connection

To ensure the Ansible control node can connect to the remote hosts and run commands, use the following ansible command to ping the hosts from the control node:

ansible all -m ping

Note: When a user connects to the remote hosts for the first time, Ansible asks for confirmation that the hosts are authentic. To confirm the authenticity, enter yes when prompted.

The output confirms the successful connection.

Pinging remote hosts in Ansible.

The Ansible control node is now set up to control the connected remote hosts.

Conclusion

After following the steps in this guide, you have successfully installed Ansible on Ubuntu and can execute commands and playbooks on remote hosts. The guide provided instructions for setting up the Ansible control node and connecting it with the hosts via SSH.

Get started with Ansible playbooks by reading Ansible Playbook: Create and Configure Playbooks.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How to Connect to a Server Using the BMC Remote Console Feature
August 28, 2020

This guide shows you how to use the Remote Console functionality in the Bare Metal Cloud Portal...
Read more
How to Install phoenixNAP BMC Ansible Module
July 24, 2020

Learn how to successfully install the Bare Metal Cloud Ansible module on your control machine. Take a look at...
Read more
What is Bare Metal Cloud
May 20, 2020

This article provides answers to everything you wanted to know about Bare Metal Cloud and how it compares to...
Read more
How to Set Up Passwordless SSH Login
October 3, 2024

Speed up connecting to remote servers by enabling passwordless SSH login via public key authentication...
Read more