update concept, software upgrade icon on virtual screen
Image: Song_about_summer/Adobe Stock

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

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

In other words, Ansible makes a task that could be incredibly challenging, simple enough that any admin regardless of skill level can take care of.

I want to walk you through the steps of getting Ansible up and running on Ubuntu Server 22.04 (Jammy Jellyfish). You’ll walk away from this surprised at how easy it can be.

What you’ll need to install Ansible

In order to get Ansible up and running, you’ll need at least two hosts: One to serve as a controller and one host used to test the setup. I’ll be demonstrating with two Ubuntu Server 22.04 instances. You’ll also need a user with sudo privileges and an SSH key created on the controller.

How to install Ansible

Because Ansible is found in the standard repositories, the installation is as simple as logging in to your controller node and issuing the command:

sudo apt-get install ansible -y

The installation will pick up a large number of dependencies and will take anywhere from 2-10 minutes to complete.

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

sudo apt-get install sshpass -y

How to create an SSH key

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

ssh-keygen

Once the key is complete, you’ll 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’ll need to create an inventory file, which contains all the necessary details of your remote hosts. First, create a directory to house the files with:

sudo mkdir /etc/ansible

Create the hosts file with:

sudo nano /etc/ansible/hosts

We’ll list our remote host under [servers] with an IP address of 192.168.1.66. That 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 properly, issue the command (from the controller):

ansible all -m ping

You should be prompted for your remote user password and, upon successfully pinging the remote host, Ansible will report back:

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

That means it’s working.

Let’s test our setup.

How to run an ad-hoc command for testing

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

ansible all -a "ls -l"

You should be presented with a listing of the root directory of the user associated with the SSH key you sent to the host, indicating all is well.

How to create and run a playbook

At the heart of Ansible is the playbook. You create these playbooks which 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 do just that.

Let’s first create a directory to house our playbooks with:

mkdir ~/playbooks

Change into that directory with:

cd ~/playbooks

Create the new playbook with the command:

nano test.yaml

In that 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 rundown 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 the password for that user.

Because we’re running an apt upgrade on the system, it could take some time to complete. When it finishes, your remote host will not only have a new user but will have all of its software upgraded.

And that’s how easy it is to install and use Ansible on Ubuntu Server 22.04. We’ll return to this subject later to craft 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 advice for business pros from Jack Wallen.