Ansible for Ubuntu 24.04
How to install and configure Ansible on Ubuntu 24.04
Ansible is a configuration management system written in Python. It allows you to automate the setup and deployment of software on VPS and dedicated servers. With Ansible, you can manage applications, configurations, and infrastructure without having to perform tasks manually on each host.
Ansible is an open-source tool used for managing software, configurations, and application deployment across multiple hosts.
Installation
First, update the package index:
sudo apt update
Install the software-properties-common
package, which simplifies managing third-party repositories:
sudo apt install software-properties-common
Add the Ansible PPA repository:
sudo apt-add-repository ppa:ansible/ansible
Press Enter to confirm.
Update the package index again and install Ansible:
sudo apt update
sudo apt install ansible
After this, Ansible will be installed on the server, providing the tools you need to manage your hosts.
Setting up SSH access to hosts
Generate an SSH key for connecting to your hosts:
ssh-keygen -t rsa -b 4096
Display the SSH public key on the Ansible server:
cat ~/.ssh/id_rsa.pub
Example output:
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 key to each host that will be managed by Ansible:
ssh-copy-id user_name@ip-address
Example:
ssh-copy-id root@5.187.2.32
Open the Ansible configuration file:
sudo nano /etc/ansible/hosts
Define a group [group_name]
with your servers:
[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
Replace the IP addresses with the actual IPs of your hosts.
Note
The [group_name:vars]
subgroup sets the ansible_python_interpreter
parameter for all hosts in the group, ensuring Python 3 (/usr/bin/python3
) is used instead of Python 2.7 (/usr/bin/python
).
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 file and exit (CTRL + X, Y, Enter).
Warning
Each host must have Python and the 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 use a different user, additional configuration is required.
Create a directory for YAML files for each group:
sudo mkdir /etc/ansible/group_vars
Create a subdirectory with your group name:
sudo nano /etc/ansible/group_vars/group_name
Example:
sudo nano /etc/ansible/group_vars/servers
Specify the user to connect as:
---
ansible_user: user_name
Save the changes.
Note
Individual hosts can also be configured by creating files with their aliases in the /etc/ansible/host_vars
directory.
To test the connection, run the ping module:
ansible -m ping all
Example output:
server1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
server2 | SUCCESS => {
"changed": false,
"ping": "pong"
}