Ansible is a free, open-source automation tool that allows system administrators to configure and control hundreds of nodes from a central server without having to install any agents on the nodes. In this article, we are going to teach you How to Configure and Use Ansible on AlmaLinux 8.5.

Introduction to Ansible

Ansible is an automated tool or open-source platform, used for IT tasks such as managing configuration, deploying applications, setting up in-service, and delivering. Automation is very important these days, and IT environments are very complex and usually need to be scaled very quickly so that system administrators and developers can continue the process if they need to do things manually.

Automation not only simplifies complex tasks, but also makes developers’ jobs more controllable, and also allows them to focus on other tasks that add value to an organization. In other words, it frees up time and increases efficiency. Ansible is rapidly rising in the list of world automation tools.

How to Install Ansible on AlmaLinux 8.5

First, you need to make sure the system is up to date using the following commands:

sudo dnf update
sudo dnf upgrade

Then you need to enable EPEL using the following command:

sudo dnf install epel-release

Then, use the following commands to install Python 3 and run other dependencies:

sudo dnf module install python38
sudo alternatives --config python

In this step, install Ansible from the official AlmaLinux repository:

sudo dnf install ansible

To install Ansible, you need to run the following commands:

sudo pip3 install setuptools-rust wheel
sudo pip3 install --upgrade pip
sudo python -m pip install ansible

When done, it is time to confirm the installation using the following command:

ansible –version

How to Configure Ansible on AlmaLinux 8.5

In the first step of this step, to configure Ansible, it is necessary to install openssh-server using the following commands:

sudo apt install openssh-server
sudo systemctl enable ssh

Enter the following command to allow port 22 in the firewall:

sudo ufw allow 22

You can use the following commands for RHEL or CentOS Linux VPS:

sudo dnf install openssh-server
sudo systemctl enable sshd

And you should allow port 22 in firewall:

sudo firewall-cmd --zone=public --permanent --add-port=22/tcp

To perform these steps, you must have three servers: Ubuntu, Debian, and CentOS:

Ubuntu – server_IP
CentOS – server_IP
Debian – server_IP

How to Use Ansible on AlmaLinux 8.5

In the continuation of this article, we intend to teach you step by step How to Use Ansible on AlmaLinux 8.5.

How to Generate SSH Keys on Ansible Installed AlmaLinux

To install the package or deploy on the target remote server, create a pair of SSH keys on the localhost and then press them on each remote server so that you can manage them using SSH. Type the command and press the Enter key several times until the keys are generated:

ssh-keygen

How to Copy SSH keys to Remote or Target Servers

Press the key created in almaLinux to the remote servers that you want to configure or manage. You need to know the username of the remote server or use the default root user.

Note: Replace linda with sudo users on remote servers or use the default root and replace the IP address with the server address.

ssh-copy-id [email protected] – server_IP
ssh-copy-id [email protected] – server_IP
ssh-copy-id [email protected] – server_IP

Run the following command on each server so that you can run sudo commands on them using Ansible without entering a password:

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

How to Inventory File for Remote Hosts

In Ansible, create a file that defines the entire remote host or target system that you want to manage. Also create a group of hosts, for example, a group is a web server that only includes a remote system that runs some web servers such as Apache, and another group can be a Mysql group running Database server and so on. The Inventory file is also important because it uses the commands, modules, and tasks in a playbook. Now as you have three remote servers in this tutorial, add them to the Ansible host file:

sudo dnf install nano -y
sudo nano /etc/ansible/hosts

If you do not want to create a group, put your remote server IP address or domain name in the file, and specify a group before adding IP addresses. Edit the default values of the Ansible inventory file that already contains samples, or add your own samples at the bottom of the file. In this section, add two servers to a host group identified by the web servers, and one server is defined as an individual.

They are in a group called a web server. The advantage of creating a group is that you can issue a command to a complete set of servers defined in that particular group of hosts.

Inventory with custom SSH port

If you are using a server that does not have a default 22 SSH port, for example, a server running on a Docker, you can also define it by its IP address:

your_IP_address ansible_user=remote-server-username ansible_port=49153

Note: In the above command, replace the IP address, remote-server-username and port number and add it to the inventory file.

Create-Ansible-Inventary-file-Rocky-Linux-1

To save the file, just press Ctrl + X, Type “-y” and press Enter.

How to Ping all added Remote Servers on AlmaLinux

Once you have successfully created the inventory file, check if Ansible can ping all the added servers for it. To ping group server:

ansible -m ping group-name

example:

ansible -m ping web-servers

To ping a single server:

ansible -m ping ip-address

example:

ansible -m ping your_IP_address

and finally you can type the following command to ping all:

ansible -m ping all

Ansible Commands to Install Packages from Remote Servers

In this section, we are going to explain about common Ansible commands to install packages or retrieve info from remote servers. Suppose you want to install the Apache web server on a group of servers that you have defined in the Inventory file. Use a web server, as we have already mentioned here, you can use any name you give to your server group.

Command Syntax 

You can use the following command as syntax command:

ansible -b --become-method = sudo -m shell -a 'command to execute' web servers

For example, running an update and installing the Apache server on Debian and Ubuntu remote systems simultaneously:

ansible -b --become-method=sudo -m shell -a 'apt update' webservers

Installing Apache

You can use the following command to install apache:

ansible -b --become-method=sudo -m shell -a 'apt install -y apache2' webservers

commands-to-Install-Ansible-on-almaLinux-Almalinux-1

To execute the same command above for all defined remote PCs:

ansible -b --become-method=sudo -m shell -a 'apt install -y apache2 ' all

For non-grouped hosts, you can use their IP address, for example, the following command:

ansible -b --become-method=sudo -m shell -a 'apt install -y apache2' your_IP_address

Also use other commands that do not require sudo, such as working time check:

ansible -m command -a "uptime" group-name/ip-adress

The command can be used for other purposes, just replace the uptime with the command you want to run on the remote server and rename the group/IP address.

Conclusion

In this tutorial, you learned How To Configure and Use Ansible on AlmaLinux 8.5. You are also familiar with the features of Ansible. The Ansible automation platform has grown over the years to provide powerful automation solutions that work for IT operators, managers and decision makers in a variety of technology fields. We hope you find this tutorial useful. Share your comments with us via the form below.