1. Home
  2. Linux
  3. Set up ansible on ubuntu server

How to set up Ansible on Ubuntu server

Ansible is a configuration management and app deployment tool for Linux and other Unix-like operating systems. Ansible is primarily used to manage and deploy software on many Linux servers at once. The software is maintained by RedHat and is a favorite in the Linux IT community. In this guide, we will show you how to install Ansible on Ubuntu server, and get it working.

Upgrade Ubuntu Server

The Ansible application works best on a recent version of Ubuntu Server. Before following this guide to learn how to install and set up the Ansible tool, Ubuntu Server must be upgraded. Ideally, to version 18.04 LTS or newer.

Upgrading Ubuntu Server is a complicated process compared to Ubuntu Desktop, as there is no GUI to do the heavy lifting. Back up all critical data to an external drive, or network share, or cloud service. Then, follow this guide to learn how to upgrade your Ubuntu Server.

Alternatively, if upgrading is too time-consuming, try downloading the new version of Ubuntu Server here, and install it before continuing.

Install Ansible on Ubuntu server

On Ubuntu, the Ansible software is incredibly easy to install, thanks to the developers of the software providing a Personal Package Archive (PPA) that has all of the necessary dependencies and packages ready to go. To start the installation, log into Ubuntu Server, then follow the step-by-step instructions below to install Ansible.

Step 1: On Ubuntu Server, you may not have PPA support enabled. The reason PPA support may be disabled is that Ubuntu Server does not include the “software-properties-common” package by default, which enables the PPA feature.

To install “software-properties-common” on your Ubuntu server, use the Apt command below in a terminal shell prompt or SSH session.

sudo apt install software-properties-common

Step 2: After installing the “software-properties-common” package on your Ubuntu Server, it is time to add the official Ansible PPA to the system. Using the add-apt-repository command, add the PPA.

sudo apt-add-repository ppa:ansible/ansible

After adding the PPA to Ubuntu Server, you will see a prompt on-screen. This prompt outlines what the PPA is, as well as some other information about the software on it. Press the Enter key to continue through the prompt and add the PPA.

Step 3: Once the PPA is added to Ubuntu Server, it is time to run the update command. It will refresh Ubuntu’s software sources, and allow the Ansible PPA to be accessible.

sudo apt update

Step 4: Following the update, it is time to install any pending software patches that Ubuntu Server may have. To do this, use the upgrade command.

sudo apt upgrade -y

Step 5: Now that your system is upgraded, it is time to install Ansible on Ubuntu Server with the Apt command below.

sudo apt install ansible ssh-pass

Configuring Ansible

Ansible needs to be configured before it can be used to manage other servers. The first thing you must do is enable SSH on your Ubuntu server running Ansible, as the software uses the SSH protocol to communicate.

To enable an SSH server on Ubuntu, head over to our guide on the subject. It explains in-depth how to set up SSH and how to use it.

Note: want extra security on your Ubuntu server running Ansible? Try enabling SSH key access by following our guide on the subject.

Ansible hosts

After enabling the SSH server on Ubuntu, it is time to set up the hosts file for Ansible, as this is the way the software keeps track of the servers it manages. Using the command below, open up the Ansible “hosts” file for editing purposes.

sudo nano -w /etc/ansible/hosts/

Inside of the Nano text editor, you will see a lot of text with # symbols in front of them. These are configuration examples. Look through them, as they contain helpful examples of how you can tweak and customize your Ansible setup.

After taking a look at the examples in the “hosts” file, use the Down arrow button to move to the bottom of the file. Then, write out “[servers]” in the Nano text editor.

[servers]

Followed by the “servers” line, write out the IP addresses to the remote servers that you will be managing with the Ansible software on Ubuntu. For example:

[servers]
server1 ansible_host=123.4.567.1 ansible_user=remote-username
server2 ansible_host=123.4.567.2 ansible_user=remote-username
server3 ansible_host=123.4.567.3 ansible_user=remote-username
server4 ansible_host=123.4.567.4 ansible_user=remote-username

After you’ve set up the hosts on the system, press Ctrl + O to save the edits in Nano. Then, close the Nano text editor with Ctrl + X.

Testing Ansible

To test Ansible to ensure that it is working, you can run commands to it. For example, to test that the networking is working for all remote servers, run the following command from Ubuntu.

ansible -m ping server1 --ask-pass

Or, ping the entire group:

ansible -m ping servers --ask-pass

Assuming the ping command is successful, Ansible can access your servers and works correctly.

Running other commands

To run any command on servers managed with Ansible, follow the command syntax below. However, be sure to replace EXAMPLE-COMMAND with the actual command you wish to run.

ansible -m shell -a 'EXAMPLE-COMMAND' servers --ask-pass

Or, a specific server:

ansible -m shell -a 'EXAMPLE-COMMAND' server1 --ask-pass