Install and Configure Ansible on Ubuntu 20.04 in 4 Simple Steps

Try this guide with our instant dedicated server for as low as 40 Euros

install ansible on ubuntu

Configuration management systems simplify the management of multiple servers for DevOps teams and administrators. These platforms allow sysadmins and authorized users to automate the administration and control of multiple devices from a single, central place.

You can find many popular configuration management tools for Linux systems that bring efficiency into multi-server setup management. 

However, the main challenge in working with tools such as Chef and Puppet is that they are more complicated than most users want or require. 

Here, Ansible is a great substitute, providing architectures that use YAML files to define provisioning information and SSH to conduct automation operations without requiring specific software installed on nodes. Ansible simplifies server management by automating repetitive tasks like software installation and user authorization. As a result, it offers invaluable support for scaling server infrastructure.

In this simple tutorial, we’ll show you how to install Ansible on an Ubuntu 20.04 server. 

Table Of Contents

  1. How to Install Ansible on Ubuntu
    1. The Prerequisites
    2. Step #1: Set Up the Host, SSH Key Pair, and Ansible Control Node
    3. Step #2: Install Ansible
    4. Step #3: Set Up the Inventory File
    5. Step #4: Test the Connection
  2. Conclusion
  3. FAQs

How to Install Ansible on Ubuntu

Let’s run through the process of installing Ansible on an Ubuntu machine, starting with a quick look at the prerequisites for the process.

The Prerequisites

Before diving in, ensure you have the following:

  • A system running Ubuntu 20.04
  • A user account with root or sudo privileges

Step #1: Set Up the Host, SSH Key Pair, and Ansible Control Node

The first step of setting up Ansible on any system is setting up the Ansible control node. This is a critical component in managing your server infrastructure.

Follow these steps to set up the control node and an Ansible host.

Set up the Control Node

Start by logging in as the root user. 

Now, we need to create a new non-root user and grant it administrative privileges. For this, we will use the adduser command to create the user account:

# adduser [username]

Replace [username] with a username of your choice.

Next, choose a secure account password (if preferred, reply to all inquiries about the user). Press Enter to skip any fields you do not want to answer.

adduser rs01

The new account is ready. You can now assign administrative access to the account.

Here, use the sudo command to assign the account sudo privileges. The syntax of the command in this context will be as follows:

# usermod -aG sudo [username]

Once the command is executed, the new user account can perform administrative operations. You can now log in with the credentials of this account to carry out the next steps. 

Configure a Pair of SSH Keys

Open a terminal window as the newly created user.

Generate an SSH key pair using the ssh-keygen command.

# ssh-keygen

This command generates a public key and private key, and asks you for a file to save the key:

ssh-keygen

If you already have an SSH key pair, the system displays the output below. 

rsa already exists

Select whether or not to overwrite the existing SSH key pair. 

Note: Overwriting an SSH key pair will make it ineffective. Make sure you do not need the old keys anymore before confirming, as this is an irreversible process.

Finally, you will be prompted to enter a passphrase. 

A strong passphrase is crucial to protect the SSH access. Therefore, we strongly advise choosing a robust passphrase to enhance security and mitigate potential future risks.

While entering the passphrase, the process does not display characters on the screen for security reasons. Type your desired passphrase and press Enter twice.

enter passphrase

The output in the terminal would resemble the following:

enter same passphrase again

Set Up an Ansible Host

Ansible hosts are remote servers monitored and controlled by the Ansible control node. Each host keeps the control node’s SSH key in the system user’s keys directory (the path is usually /[username]/.ssh/authorized_keys).

You need to upload the SSH public key you created earlier by using the ssh-copy-id command:

# ssh-copy-id username@remote_host

Here, replace username with the actual username on the remote server. Replace remote_host with the IP address or hostname of the remote server you want to manage with Ansible.

When you run the command for the first time, the output will be similar to the following: 

ssh-copy-id username

Note that the output displays the date you established your first connection to the new distant host.

Next, type Yes and press Enter to proceed.

The utility now searches for the public key generated in the previous step. Once it finds it, enter the remote host account password.

ssh-copy-id username 2

Press Enter to confirm the password.

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

Step #2: Install Ansible 

Before installing Ansible, ensure that the package index of your system is up-to-date. We recommend running the following command to update the package index:

# sudo apt update

Next, install Ansible on Ubuntu with the following apt install command:

# sudo apt install ansible

sudo apt install ansible

During the installation process, you will be prompted to press Y to continue, with the rest of the installation process being automated.

Once installed, the Ansible control node can control the remote host.

Step #3: Set Up the Inventory File

Set up a default inventory file to allow Ansible to connect with remote hosts.

The inventory file contains everything you need to control remote hosts using the Ansible control node. You can store up to several hundred hosts, grouped, and subgrouped, and variables unique to the created groups you had set up. 

You can access the inventory file in your preferred text editor. We will run the following command in the control node terminal to open this file in Nano:

# sudo nano /etc/ansible/hosts

The Ansible inventory file’s default path is /etc/ansible/hosts. However, you can specify a different path by using the -i parameter.

You can now start adding remote hosts that your control node will manage. 

The default Ansible inventory file starts by listing general advice and examples on building a list of remote hosts: 

default ansible host file

Next, scroll down to the bottom of the file.

Add new hosts and group them into categories. While adding hosts and categories, use the following format:

[category name]

server_name ansible_host=[server_ip]

In this example, we categorized a remote host named rsserver, into the [servers] category using its IP address:

rsserver

Save and exit the file when you are done with adding items to Ansible’s inventory. If you open the file in Nano, press Ctrl+X and press Y.

After adding the categories and servers to the inventory file, you should always run the following command to double-check it:

# ansible-inventory --list -y

ansible-inventory --list -y

Step #4: Test the Connection

The final phase of installing Ansible on Ubuntu is verifying that Ansible’s control node can connect to remote systems and execute instructions.

Start by running the following command to check the control node’s connection with the hosts:

# ansible all -m ping

Typically, you will be logged in as the non-root user you created in Step 1. If necessary, you can utilize the -u option with the command to test the connection as the root user. The command syntax would be as follows:

# ansible all -m ping -u root

If this is your first time connecting to the remote hosts, you will be prompted to verify the authenticity of the hosts. Once prompted, type Yes and press Enter to verify the legitimacy.

If each remote host responds with a pong back, you are ready to initiate command execution through the Ansible control node.

Conclusion 

Installing and configuring Ansible on Ubuntu 20.04 is a simple procedure that includes developing playbooks, completing automation tasks, updating the package index, installing Ansible, setting up SSH key authentication, and testing connectivity. Through this guide, users can improve efficiency and scalability throughout their infrastructure by streamlining configuration file management and deployment procedures. 

To efficiently automate tedious processes and manage complicated workflows, partner with RedSwitches.

RedSwitches is a global hosting provider offering Dedicated Servers, Infrastructure As a Service, Managed Solutions, and Smart Servers. Known for its powerful managed dedicated servers, RedSwitches now offers expert assistance with Ansible implementation and ongoing infrastructure management.

FAQs

Q. What is Ansible?

Ansible is an open-source automation tool that makes IT infrastructure management, configuration, and deployment tasks easier with simple, agentless playbook execution.

Q. Why should I use Ansible?

Ansible offers ease of use, scalability, and flexibility, allowing users to efficiently automate tasks across large and diverse infrastructure environments.

Q. Is Ansible suitable for Ubuntu 20.04?

Yes, Ansible is fully compatible with Ubuntu 20.04, and the installation and configuration process outlined in the guide applies specifically to this version.

Q. Does Ansible require programming knowledge to use?

No, Ansible employs human-readable, straightforward YAML-based playbooks. To begin with, you don’t need to be an expert coder, though it helps to have a basic understanding of YAML syntax.

Q. Can I install Ansible on other operating systems?

Indeed, Ansible is cross-platform and compatible with Windows, macOS, and several Linux distributions.

Q. What is an Ansible inventory file?

Ansible inventory files are text files that list the hosts that Ansible will manage. It can be adjusted to define variables and groups and also holds the hostnames or IP addresses of remote hosts.

Q. How can I troubleshoot connectivity issues with Ansible?

Ensure SSH key authentication is rightly configured, verify network connectivity, and check firewall settings on the control node and remote hosts. Reviewing Ansible logs can also provide insights into any errors or issues encountered during execution.

Q. Is Ansible secure to use?

Ansible improves security by utilizing SSH for communication and removing the need for extra agents on managed systems. However, it’s crucial to adhere to recommended practices, including limiting access to Ansible control nodes and maintaining robust SSH key security.

Q. Can Ansible be integrated with other tools and platforms?

Yes, Ansible offers extensive integrations with various tools and platforms, including cloud providers, version control systems, monitoring solutions, and more, allowing for seamless automation and orchestration workflows.

Q. Where can I locate more Ansible learning resources?

The Ansible documentation, community forums, tutorials, and online courses are valuable resources for learning Ansible and mastering its capabilities. Experimenting with playbooks and getting practical experience are also crucial for mastery. 

Try this guide with our instant dedicated server for as low as 40 Euros