Install Ansible on Ubuntu Server to automate Linux server deployments

How many Linux machines do you manage on your network or cloud-hosted platform? These days, that number is probably growing pretty quickly, especially considering that businesses depend not only on Linux for regular services, but also containerized and cloud-native deployments.

So yes, these Linux machines are probably growing exponentially every week or every month. This means that you have more and more machines to manage, which can be time consuming. Since your day is already busy, you don’t need to log into each machine and run commands manually.

With that in mind, what do you do? One solution is to turn to a centralized configuration management tool, like Red Hat’s Ansible. One of the best things about Ansible is that it uses SSH and YAML files to handle the heavy lifting remotely. This means you don’t have to worry about installing agents on the servers you need to manage, because everything is managed through the controller.

I’ll walk you through installing and configuring Ansible on Ubuntu Server, then show you how to use the platform to run your first Ansible playbook.

What you will need

I will demonstrate this setup entirely with Ubuntu Server installations, specifically Ubuntu Server 22.04. You can use Ansible with other operating systems, but since Ubuntu is my benchmark, that’s what I choose to use. Additionally, Ansible is incredibly easy to install on Ubuntu.

That said, are you ready to get to work? I thought so.

Installing Ansible

Connect to your Ubuntu Server 22.04 instance and install Ansible with the command:

sudo apt-get install ansible -y

The above command will also retrieve all the dependencies needed to get ansible up and running. There is, however, another piece of software that we will be installing, which is sshpass. SSHpass is a non-interactive ssh password provider, so you can configure your remote server inventory with passwords for easier use of Ansible.

To install sshpass, run the command:

sudo apt-get install sshpass -y

And that’s all the software you need to install.

Creating your inventory file

I will show you how to create an inventory with a single server. You can add as many servers as you need to this file, just be sure to divide them into categories (such as web, dev, database, etc.), so you have more control over the configuration.

First, create a new directory to host the inventory file with the command:

sudo mkdir /etc/ansible

Next, create your inventory file with:

sudo nano /etc/ansible/hosts

This is where things take a turn for the specific. Ansible requires you to configure your hosts in a very particular way. I will create an entry called testServer for a machine at IP address, the user socket and an ssh password of [email protected] This inventory file would look like this:

The bottom section defines variables for all servers and tells Ansible to use Python3 instead of the default Python. Be sure to insert your user’s sudo password there.


Let’s test our inventory. To do this, issue the command:

ansible all -m ping

The output of the above command should look like this:

The SUCCESS output is what you are looking for. You see that… you are golden.

Create and run a playbook

Now let’s create our first Ansible playbook. We are going to create a simple playbook that will install a full LAMP stack on our Ubuntu servers. It is very important that you indent this playbook perfectly, because it is a YAML file and it will fail if the indentation is not correct.

Create the new playbook with the command:

nano lampstack.yaml

In this file, paste the following:

Save and close the file. No, you’ll want to change jack above to a user with sudo privileges on your server.

Once your playbook has been created, you can now run it with the command:

ansible-playbook lampstack.yaml  --user=jack --extra-vars ansible_sudo_pass="[email protected]"

Again, you’ll want to switch jacks and [email protected] with a user and password that actually works on your server.

As the playbook runs, you should see output like this:

The playbook should run (this will probably take a while), and when it’s done, you can point a web browser to the server’s IP address in your inventory to see the Apache homepage on the remote server.

And that’s all there is to installing Ansible and using it to manage remote servers. For more information on Ansible Playbooks, be sure to check out the official documentation.

Feature image:

Comments are closed.