How to Install Ansible on Ubuntu Server 22.04

Image: Song_about_summer/Adobe Stock

Ansible makes it much easier for busy administrators to manage a large collection of servers. Instead of having to access each server remotely to manage a task, you can take care of much of it from a single entry point. One of the reasons I prefer Ansible over similar tools is that Ansible doesn’t require you to install clients on remote nodes. Instead, Ansible uses SSH to run all the tasks, and the YAML files contain the definitions of the tasks to run.

SEE: 40+ open source and Linux terms you need to know (TechRepublic Premium)

In other words, Ansible creates a task that could be incredibly difficult, simple enough for any administrator, regardless of skill level, to tackle.

I want to walk you through the steps of setting up and running Ansible on Ubuntu Server 22.04 (Jammy Jellyfish). You will come away surprised at how easy it can be.

What you will need to install Ansible

To get Ansible up and running, you will need at least two hosts: one to act as the controller and one host used to test the configuration. I will demonstrate with two Ubuntu Server 22.04 instances. You will also need a user with sudo privileges and an SSH key created on the controller.

How to install Ansible

Since Ansible is in the standard repositories, installation is as simple as logging into your controller node and issuing the command:

sudo apt-get install ansible -y

The installation will support a large number of dependencies and will take between 2 and 10 minutes.

We will also need to install a second piece of software, called SSHpass, which is a non-interactive password provider, otherwise you will have problems with SSH authentication. Install SSHPass with:

sudo apt-get install sshpass -y

How to create an SSH key

If you haven’t already, create an SSH key on the controller with the command:

ssh-keygen

Once the key is complete, you will need to copy it to your remote host with the command:

ssh-copy-id NODE

Where NODE is the IP address of your remote hosts.

How to create an inventory file

On your controller, you will need to create an inventory file, which contains all the necessary details of your remote hosts. First, create a directory to host the files with:

sudo mkdir /etc/ansible

Create the hosts file with:

sudo nano /etc/ansible/hosts

We will list our remote host under [servers] with an IP address of 192.168.1.66. This file will look like this:

[servers]
192.168.1.66

[all:vars]
ansible_python_interpreter=/usr/bin/python3

Save and close the file.

How to test if Ansible is working

To test if Ansible is working correctly, issue the command (from the controller):

ansible all -m ping

You should be prompted for your remote user password, and after successfully pinging the remote host, Ansible will send you a report:

192.168.1.66 | SUCCESS => {
"changed": false,
"ping": "pong"
}

It means it works.

Let’s test our configuration.

How to run an ad-hoc command to test

Let’s run the ls command on our remote host. To do this, issue the command:

ansible all -a "ls -l"

You should see a listing of the user’s root directory associated with the SSH key you sent to the host, indicating that all is well.

How to create and run a playbook

At the heart of Ansible is the playbook. You create these playbooks that map out the states you want your remote hosts to be in. For example, you can create a playbook that copies a file, adds a new user, and updates all apt packages on your remote host. Let’s just do that.

Let’s start by creating a directory to host our playbooks with:

mkdir ~/playbooks

Access this directory with:

cd ~/playbooks

Create the new playbook with the command:

nano test.yaml

In this file, paste the following:

---

- name: Test Playbook
hosts: all
tasks:

- name: Copy file hosts with permissions
ansible.builtin.copy:
src: /etc/ansible/hosts
dest: /tmp/hosts_backup
mode: '0644'

- name: Add the user 'olivia'
ansible.builtin.user:
name: olivia
become: yes
become_method: sudo

- name: Upgrade all apt packages
apt:
force_apt_get: yes
upgrade: dist
become: yes

Save and close the file.

A quick preview of the playbook looks like this:

  • We run the playbook on all hosts listed in our hosts file.
  • We copy the hosts file to the remote.
  • We add the user “olivia” to the host. One thing to keep in mind with this task is that it only creates the user, not the password.
  • We run apt upgrade on the host.

Run the test playbook with:

ansible-playbook test.yaml  --user=USER --extra-vars ansible_sudo_pass="PASSWORD"

Where USER is the remote user and PASSWORD is that user’s password.

Since we are running an apt upgrade on the system, this may take some time. When complete, your remote host will not only have a new user, but all of its software will be upgraded.

And it’s that easy to install and use Ansible on Ubuntu Server 22.04. We’ll come back to this topic later to create more playbooks to see how you can get the most out of Ansible.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech tips for professionals from Jack Wallen.