Ansible for Ubuntu 24.04
How to install and configure Ansible on Ubuntu 24.04
Ansible is a configuration management system written in the Python programming language. It is used for automating the configuration and deployment of software. This is an open-source tool for managing software, configuration, and the deployment of applications on multiple hosts.
Installation
First, update the package index:
sudo apt update
Use the PPA repository for installation. Install the software-properties-common package, which simplifies managing third-party software repositories.
sudo apt install software-properties-common
Then, add the Ansible PPA repository using the following command:
sudo apt-add-repository ppa:ansible/ansible
Press Enter to confirm.
Update the package database again and install Ansible:
sudo apt update
sudo apt install ansible
After this, the Ansible software required for managing your hosts will be installed on the server.
Configuring SSH Access to Hosts
Create an SSH key to be used for connecting to the hosts with the following command:
ssh-keygen -t rsa -b 4096
On the Ansible server, use the cat command to display the SSH public keys in the terminal:
cat ~/.ssh/id_rsa.pub
root@kvmde67:~# ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Zvr1I9IfDK3wCK9K5h5KtWBtC0R2ppUtgQOB/fikhNE root@kvmde67.fornex.org
The key's randomart image is:
+---[RSA 4096]----+
|+=o.=+ |
|ooE=o . |
| oo+ . |
|..o.o . |
| .+++ . S . . |
| ..=.o * + + |
| . * . o.+ o |
| . = . o..o... |
| ..+.. .. oo. |
+----[SHA256]-----+
root@kvmde67:~# cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQClTC6YhDZdHhiTj2UjDdOv5gpJufT0YKF6tnE1YwIFRrROBH8+hG7hyNlRtc7c5NGTp7fXXoVhnFRFe+thN/4ZDJ/1A44Pe91NldOBVovMlulk9RsRQqvLX8sZXLzY17i+2DINVF3aoOh6o3QEHrk3Axh+qz5DUYvJ70wM6GM40szUBf0wpA1GwoUaOmT4oZVDN6xRT05k++DGcik/EgwWy8hWK8hW8fmg5kXiWSvslLM8i/URaFFtTiFrna1NB75Rbl9brMLc3xMwUzqXRnohRBGbP/M7Js7HoaiS3yHqxIeo2JPVfdmxwpdCB2y14FTG0GU7gTeEOg6BUYNsSlU6IKv593RIGZ9hyPGnDiTZd3jgaOdOoYTKiHWj23zlrqqwSWbNQ== root@kvmde67.fornex.org
Copy the generated key to each host that will be managed by Ansible:
ssh-copy-id user_name@ip-address
For example:
ssh-copy-id root@5.187.2.32
Use a text editor to open the following configuration file:
sudo nano /etc/ansible/hosts
Using the following syntax, define the group [group_name] with two different servers, each with its own identifier: server1, server2.
Note
Be sure to replace the highlighted IP addresses with the IP addresses of your Ansible hosts.
[group_name]
server1 ansible_host=your_server_ip_1
server2 ansible_host=your_server_ip_2
[group_name:vars]
ansible_python_interpreter=/usr/bin/python3
Note
The subgroup group_name:vars defines the ansible_python_interpreter parameter, which will be valid for all hosts in the servers group. With this value, the remote server will use the Python 3 executable /usr/bin/python3 instead of /usr/bin/python (Python 2.7).
For example:
[servers]
server1 ansible_host=5.187.2.32
server2 ansible_host=5.187.5.236
[servers:vars]
ansible_python_interpreter=/usr/bin/python3
Save the changes and close the file (CTRL + X, Y, Enter).
Warning
Each host must have the python language and python-apt package installed:
apt-get install python python-apt
Testing the Connection
By default, Ansible connects to remote hosts as the root user. If you are using another user, you will need to perform additional configurations. Create a directory in the configuration structure to hold YAML files for each group:
sudo mkdir /etc/ansible/group_vars
Create a subdirectory with the name of your group:
sudo nano /etc/ansible/group_vars/group_name
For example:
sudo nano /etc/ansible/group_vars/servers
Insert the following line, replacing user_name with your user’s name:
---
ansible_user: user_name
Save the changes.
Note
Individual hosts can be configured by creating aliases and files named according to their alias in the /etc/ansible/host_vars directory.
To check the connection, run the ping command:
ansible -m ping all
Result:
server1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
server2 | SUCCESS => {
"changed": false,
"ping": "pong"
}