How to Install Ansible on Ubuntu 22.04 | 20.04

In this post, we will cover how to install latest version of Ansible on Ubuntu 22.04 LTS (Jammy Jellyfish) and 20.04 LTS (Focal Fossa).

Ansible is a free and open-source IT Automation and configuration tool. It is available for almost all the Linux distributions and can be used to manage Linux and Windows systems. Now a days, Ansible is also used to manage EC2 instances in AWS, Virtual machines, and containers etc. It does not require any agent on managed hosts, but it only requires ssh connection.

Prerequisites

  • Minimal Installed Ubuntu 22.04 | 20.04
  • Regular user with sudo privileges
  • 2 CPU / vCPU
  • 2 GB RAM or more
  • 20 GB Hard drive
  • Internet Connection (In case you don’t have local configured apt repository server)

Following is my lab setup details for ansible demonstration.

  • Ansible Control Node – control.example.com (192.168.1.192)
  • Ansible Managed Nodes – node1.example.com & node2.example.com
  • sysops sudo user on control and managed nodes with privileges.

Note: Here node1 is a Ubuntu System and node2 is a fedora system and control node is the system where we will install ansible. I am assuming sysops is already created on each host.

To configure sudo user (sysops) to run all the commands without prompting for the password then run the following echo and tee command on each managed host

$ echo "sysops ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/sysops

Let’s jump into the Installation steps of Ansible

Step 1) Apply Updates on Control Node

Login to Ubuntu 20.04 LTS / 21.04 system and run below apt commands to apply updates.

$ sudo apt update
$ sudo apt upgrade -y

Once all the updates are installed then reboot the system once, run

$ sudo reboot

Step 2) Enable Ansible PPA Repository

Ansible package and it’s dependencies are available in the default package repositories of Ubuntu 22.04/20.04, but it is not latest Ansible version. So, to install latest and stable ansible, enable its ppa repository, run following commands,

$ sudo apt install -y software-properties-common
$ sudo add-apt-repository --yes --update ppa:ansible/ansible

Now update repository by running beneath apt command.

$ sudo apt update

Step 3) Install latest version of ansible

Now, we are ready to install latest version of Ansible on Ubuntu 20.04 LTS / 21.04, run following apt command.

$ sudo apt install -y ansible

After the successful installation of Ansible, verify its version by executing the command

$ ansible --version

Ansible-Version-Check-Ubuntu-Linux

Great, above output confirms that Ansible version 2.13 is installed.

Step 4) Setup SSH keys and share it among managed nodes

Configure password-less ssh authentication for sysops user. Generate the SSH keys for sysops user from the control node and share it among managed hosts. Run ssh-keygen command,

$ ssh-keygen

Hit enter when prompting for the input, output is shown below

ssh-keygen-output-ubuntu-linux

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

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

To share the ssh keys between control to managed hosts, run ssh-copy-id command example is shown below

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

Output of above commands would look like below

sshkeys-exchange-with-sshcopyid-command

Step 5) Create ansible cfg and inventory file

It is always recommended to have separate ansible.cfg and inventory file for each project. For the demonstration purpose, I am using demo as the project name. So, create the project folder first by running mkdir command.

$ mkdir demo

Download sample ansble.cfg file to ~/demo folder using following wget command

$ cd demo
$ wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/examples/ansible.cfg

Edit the ~/demo/ansible.cfg file, set the following parameters,

$ vi ~/demo/ansible.cfg

Under the default sections

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

Under the privilege_escalation section

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

Save and close the file. Now, let’s create the inventory file that we have defined in ~/demo/ansible.cfg file.

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

[prod]
node1.example.com

save and exit the file

Re-run ansible --version command to verify that new config file is set
$ cd demo/
$ ansible --version

Ansible-Version-Check-command-Ubuntu

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

$ ansible all -m ping

Note : Make sure run ansible command from demo folder.

Output of command would look like below:

Ansible-Ping-Pong-Connectivity-Ubuntu

This output confirms that connectivity is in place from control node to managed hosts.

Step 6) Create a Demo Ansible playbook

To verify the ansible installation and configuration, let’s create a sample playbook named as packages.yaml under demo folder. This playbook will install packages on managed nodes.

$ vi packages.yaml
---
- name: Playbook to Install Packages
  hosts:
    - dev
    - prod
  tasks:
  - name: Install php and mariadb
    package:
      name:
        - php
        - mariadb-server
      state: present

save and Close the file

Now run the playbook using ansible-playbook command,

$ ansible-playbook packages.yaml

Output:

Ansible-Playbook-Execution-Ubuntu

Above output confirms that playbook has been executed successfully. To verify result, run following ad-hoc commands,

$ ansible dev -m shell -a 'dpkg -l | grep -E "php|mariadb"'
$ ansible prod -m shell -a 'rpm -qa | grep -E "php|mariadb"'

That’s all from this post, in case you have found it informative then please do not hesitate to share it. Kindly do post queries and feedback in below comments section.

Read Also : How to Use Handlers in Ansible Playbook

2 thoughts on “How to Install Ansible on Ubuntu 22.04 | 20.04”

  1. A great manual, thank you.

    How to use Ansible for the Windows platform? For example, how to install IIS remotely?
    Is it possible to change IP address remotely by using Ansible?

    Reply

Leave a Comment