In this blog post, we will explain how to install Ansible on Ubuntu 24.04 step-by-step including creating ansible.cfg and inventory files for the project.
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.
Prerequisites
- Pre Installed Ubuntu 24.04
- 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 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) so that it can run all the commands without prompting for the password, 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 on Ubuntu 24.04.
1) Apply Updates
Login to your Ubuntu 24.04 system and run below apt commands to install 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 on Ubuntu 24.04
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 -t rsa
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 && cd demo
Next, generate the ansible.cfg file using following command
$ ansible-config init --disabled | tee ansible.cfg
Noe, 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
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 reflected or not.
$ 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 post, I believe you have found it informative and useful. Kindly do post your queries and feedback in below comments section.
Read Also : How to Use Handlers in Ansible Playbook
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?
Hi Alexey,
To manage Windows system using Ansible, please refer the below URL:
https://www.linuxtechi.com/manage-windows-host-using-ansible/
In step 6, Are the hosts supposed to be dev, prod? I would venture to guess that’s a typo and it should be what you named the hosts before which was node1.example.com and node2.example.com
Thoughts?
Hi Allan,
I have categorized the hosts into two groups: dev and prod. Each group contains systems like node1.example.com and node2.example.com. Instead of specifying the hostnames individually, we are referring to them using their group names.