How to Install Ansible on Ubuntu 22.04 Step-by-Step
Automation is the heart of modern IT infrastructure management, and Ansible is a powerful automation tool that simplifies the automation of complex tasks. Whether you’re managing a single server or a large-scale data center, Ansible can save you time and effort. Ansible works on agent-less architecture, it operates over SSH and does not require any agents to be installed on the target systems. This makes it lightweight and easy to set up.
In this guide, we’ll walk you through the process of installing Ansible on Ubuntu 22.04, ensuring you have this versatile tool at your fingertips.
Prerequisites
- Pre Installed Ubuntu 22.04
- Regular user with sudo rights
- 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)
Lab Details:
- 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
Without any further delay, let’s jump into Ansible Installation steps,
1) Apply Updates
Login to your Ubuntu 22.04 system and run below apt commands to apply all available updates.
$ sudo apt update $ sudo apt upgrade -y
Once all the updates are installed then reboot the system once, run beneath command
$ sudo reboot
2) Add Ansible PPA Repository
Ansible package and it’s dependencies are available in the default package repositories but there are chances that you will not get latest version of Ansible. So, in order to install latest Ansible version, add 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 package index by running beneath apt command.
$ sudo apt update
3) Install Ansible
Now, we are ready to install latest version of Ansible on Ubuntu 22.04 LTS / 20.04, run following apt command.
$ sudo apt install -y ansible
Once the installation is complete, you can verify that Ansible is installed correctly by checking its version:
$ ansible --version
You should see the installed version of Ansible displayed on your terminal.
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
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
5) Create Ansible.cfg File and Setup Inventory
It is always recommended to have separate ansible.cfg and inventory file 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.
$ 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.11/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
Great, ansible is now reading our project’s 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 above command would look like below:
This output confirms that connectivity is in place from control node to managed hosts.
6) Test Ansible Installation
In order to test 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
Next, run the playbook using ansible-playbook command,
$ ansible-playbook packages.yaml
Output:
Output above 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 guide, I you have found it informative and useful. Kindly do post your queries and feedback in below comments section.
Conclusion
Installing Ansible on Ubuntu 22.04 is a straightforward process that empowers you to automate system administration tasks efficiently. By following these steps, you’ve laid the foundation for harnessing Ansible’s capabilities in your infrastructure management journey. Whether you’re provisioning servers, deploying applications, or configuring systems, Ansible is your reliable automation companion on Ubuntu.
Read Also : How to Use Handlers in Ansible Playbook