Introduction

Info
Ansible is a powerful automation tool that simplifies the automation of complex tasks. This guide will walk you through the installation of Ansible on Ubuntu 20.04, 22.04, and 24.04, and demonstrate how to create and run a demo playbook to change the server hostname.

Prerequisites

Warning
Ensure you meet the following prerequisites before proceeding:
  • Ubuntu 20.04, 22.04, or 24.04 installed
  • Regular user with sudo rights
  • 2 CPUs / vCPUs
  • 2 GB RAM or more
  • 20 GB Hard drive
  • Internet Connection (In case you don’t have a local configured apt repository server)

Installation Steps

Step 1: Update the System

Note
Login to your Ubuntu system and run the following apt commands to install all available updates:
bash
$ sudo apt update
$ sudo apt upgrade -y

Once all the updates are installed, reboot the system:

bash
$ sudo reboot

Step 2: Add Ansible PPA Repository

Tip
Ansible package and its dependencies are available in the default package repositories but there are chances that you will not get the latest version of Ansible. So, in order to install the latest Ansible version, add its PPA repository. Run the following commands:
bash
$ sudo apt install -y software-properties-common
$ sudo add-apt-repository --yes --update ppa:ansible/ansible

Now, update the repository package index by running the following apt command:

bash
$ sudo apt update

Step 3: Install Ansible

Success
Now, we are ready to install the latest version of Ansible on Ubuntu 20.04, 22.04, or 24.04, run the following apt command:
bash
$ sudo apt install -y ansible

Step 4: Verify Installation

Once the installation is complete, you can verify that Ansible is installed correctly by checking its version:

bash
$ ansible --version

You should see the installed version of Ansible displayed on your terminal.

Configure SSH Keys

Note
Configure password-less SSH authentication for the sysops user. Generate the SSH keys for the sysops user from the control node and share it among managed hosts. Run the ssh-keygen command:
bash
$ ssh-keygen -t rsa

Hit enter when prompting for the input.

Note: Add managed host entries in /etc/hosts file on the control node. This is only required when you don’t have a local DNS server configured.

bash
192.168.1.200 node1.example.com
192.168.1.77  node2.example.com

To share the SSH keys between the control node and managed hosts, run the ssh-copy-id command:

bash
$ ssh-copy-id node1.example.com
$ ssh-copy-id node2.example.com

Create Ansible Configuration and Inventory Files

Tip
It is always recommended to have separate ansible.cfg and inventory files for each project. For the demonstration purpose, I am going to use demo as the project name. So, create the project folder first by running mkdir command:
bash
$ mkdir demo && cd demo

Next, generate the ansible.cfg file using the following command:

bash
$ ansible-config init --disabled | tee ansible.cfg

Now, edit the ~/demo/ansible.cfg file, set the following parameters:

bash
$ vi ~/demo/ansible.cfg

Under the default section:

ini
inventory = /home/sysops/demo/inventory
remote_user = sysops
host_key_checking = False

Under the privilege_escalation section:

ini
become=True
become_method=sudo
become_user=root
become_ask_pass=False

Save and close the file.

Let’s create the inventory file that we have defined in ~/demo/ansible.cfg file:

bash
$ vi ~/demo/inventory
[dev]
node2.example.com

[prod]
node1.example.com

Save and exit the file.

Re-run ansible –version command to verify that the new config file is reflected:

bash
$ cd demo/
$ ansible --version

Great, Ansible is now reading our project’s configuration file. Let’s verify the managed nodes connectivity using ansible ad-hoc command:

bash
$ ansible all -m ping

Note: Make sure to run the Ansible command from the demo folder.

Test Ansible Installation

Create a Demo Playbook

Example
In order to test Ansible installation and configuration, let’s create a sample playbook named change_hostname.yaml under the demo folder. This playbook will change the hostname on managed nodes.
bash
$ vi change_hostname.yaml
yaml
---
- name: Playbook to Change Server Hostname
  hosts:
    - dev
    - prod
  tasks:
  - name: Change hostname to new_hostname
    hostname:
      name: "new_hostname"

Save and close the file.

Run the Playbook

Next, run the playbook using the ansible-playbook command:

bash
$ ansible-playbook change_hostname.yaml

Output:

bash
PLAY [Playbook to Change Server Hostname] *************************************

TASK [Change hostname to new_hostname] ****************************************
changed: [node2.example.com]
changed: [node1.example.com]

PLAY RECAP *******************************************************************
node1.example.com          : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node2.example.com          : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Output above confirms that the playbook has been executed successfully. To verify the result, run the following ad-hoc commands:

bash
$ ansible dev -m shell -a 'hostname'
$ ansible prod -m shell -a 'hostname'

Conclusion

Quote
That’s all from this post. I believe you have found it informative and useful.