How to Install and Configure Ansible on AlmaLinux
Ansible is a free and open source automation tool that allows system administrators to configure and control hundreds of nodes from a centralized server without requiring installed agents. It uses the SSH protocol to communicate with remote nodes. Ansible is the preferred management solution over Puppet and Chef because of its ease of use and installation.
What is Ansible Used For?
Ansible can be used in provisioning to build up the numerous servers required in your architecture. Ansible is a tool for configuration management that may be used to start and stop services, install or update software, implement security policies, and carry out a broad range of other configuration-related operations. Ansible makes DevOps easier by enabling the automatic deployment of internally developed applications to your production systems.
Requirements
- Operating system & Version: AlmaLinux OS 9
- Root access to your Linux system or via the sudo command or user with admin privileges.
Install and Configure Ansible on AlmaLinux
In this section, you learn how to install and configure Ansible on Almalinux. This setup uses three Almalinux 9.1 servers, one for the control node and the other two for Managed hosts.
- Control Node - AlmaLinux OS 9.1.
- Managed Host 1 - AlmaLinux OS 9.1.
- Managed Host 2 - AlmaLinux OS 9.1.
- nouf-admin user with admin rights.
Install Ansible with the dnf Command
To install Ansible AlmaLinux with dnf command, follow the steps below.
Step 1: Update the System
It is essential to ensure your existing operating version is up-to-date before installing new software. Update your system by executing the below command.
[root@nouf-node ~]# sudo dnf update -y
Step 2: Configure the EPEL Repository
The Ansible package and its dependencies are unavailable in AlmaLinux 9's default package repositories. The EPEL repository must first be configured to install Ansible AlmaLinux using the dnf command.
Install the EPEL repository on the system by executing the below command.
[root@nouf-node ~]# sudo dnf install -y epel-release
Step 3: Install Ansible With the dnf Command
Execute the below command to install the Ansible package from the EPEL repository.
[root@nouf-node ~]# sudo dnf install ansible -y
After successfully installing Ansible and its dependencies, verify its version by executing the below command.
[root@nouf-node ~]# ansible --version
ansible [core 2.13.3]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.9/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.9.14 (main, Jan 9 2023, 00:00:00) [GCC 11.3.1 20220421 (Red Hat 11.3.1-2)]
jinja version = 3.1.2
libyaml = True
Install Ansible with Pip
If you wish to install the most recent version of Ansible, you can use Pip. To install Ansible on AlmaLinux with Pip, follow the below steps.
Step 1: Install All Updates
Update your system by executing the below command.
[root@nouf-node ~]# sudo dnf update -y
Step 2: Install Python 3.9 and Other Dependencies
Execute the below command to install Pip with Python 3.9 and other dependencies.
[root@nouf-node ~]# sudo dnf install python3-pip
[root@nouf-node ~]# sudo pip3 install --upgrade pip
[root@nouf-node ~]# python -V
Python 3.9.13
[root@nouf-node ~]# pip3 --version
pip 23.0.1 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)
Step 3: Install the Latest Version of Ansible With Pip
Execute the below commands one by one to install Ansible with Pip.
[root@nouf-node ~]# sudo pip3 install setuptools-rust wheel
[root@nouf-node ~]# sudo python -m pip install ansible
After successfully installing Ansible, verify its version by executing the below command.
[root@nouf-node ~]# ansible --version
ansible [core 2.14.3]
config file = None
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.9/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/local/bin/ansible
python version = 3.9.14 (main, Jan 9 2023, 00:00:00) [GCC 11.3.1 20220421 (Red Hat 11.3.1-2)] (/bin/python)
jinja version = 3.1.2
libyaml = True
Verify the Ansible Installation
Ansible's default configuration file, ansible.cfg, is automatically created under the /etc/ansible folder when installed using the dnf or yum commands. But if you install it via Pip, you must manually build its configuration file. It is recommended that ansible.cfg files be created for each project.
The following commands create a project called project-ansible for the demonstration.
[root@nouf-node ~]# mkdir project-ansible
[root@nouf-node ~]# cd project-ansible
Create the ansible.cfg file under the folder project-ansible with the following details and save the file.
[nouf-admin@nouf-node project-ansible]# vi ansible.cfg
[defaults]
inventory = /home/nouf-admin/project-ansible/inventory
remote_user = nouf-admin
host_key_checking = False
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
Then, create an inventory file under the folder project-ansible with the following details and save the file.
[nouf-admin@nouf-node project-ansible]# vi inventory
[Hostserver1]
172.31.2.186
[Hostserver2]
172.31.2.187
Now, you must create SSH keys for your remote_user and share them among all the managed host servers. Here, nouf-admin is a remote_user.
[nouf-admin@nouf-node ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/nouf-admin/.ssh/id_rsa):
Created directory '/home/nouf-admin/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/nouf-admin/.ssh/id_rsa.
Your public key has been saved in /home/nouf-admin/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:MYbYFfbWGwV3SuyhDShArL+4MD9hVng1OzR6UgHzqOE nouf-admin@nouf-node
The key's randomart image is:
+---[RSA 3072]----+
| o=o=o ..o+ .|
| o.BB....+oo |
| oo+===o o=.. |
| .oo=.++ .oo |
| E+ oS. . |
| + . |
| oo o . |
| +o . |
| oo |
+----[SHA256]-----+
You can now share the SSH keys using the ssh-copy-id command.
$ ssh-copy-id nouf-admin@172.31.2.186
$ ssh-copy-id nouf-admin@172.31.2.187
Here is the output.
[nouf-admin@nouf-node ~]$ ssh-copy-id nouf-admin@172.31.2.186
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/nouf-admin/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
nouf@172.31.2.186's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'nouf-admin@172.31.2.186'"
and check to make sure that only the key(s) you wanted were added.
[nouf-admin@nouf-node ~]$ ssh-copy-id nouf-admin@172.31.2.187
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/nouf-admin/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
nouf@172.31.2.187's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'nouf-admin@172.31.2.187'"
and check to make sure that only the key(s) you wanted were added.
Execute the below command on each managed host server to run all the commands without prompting a password.
# echo "nouf-admin ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/nouf-admin
Replace nouf-admin with your user name.
Using the ping module, verify the connectivity from the control node to managed hosts.
$ ansible -i inventory all -m ping
Here is the output.
[nouf-admin@nouf-node project-ansible]$ ansible -i inventory all -m ping
172.31.2.186 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
172.31.2.187 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
Now, create a sample playbook (web.yaml) to install NGINX and PHP on these managed hosts.
Create a web.yaml file under the folder project-ansible with the following details and save the file.
- name: Play to Packages
hosts:
- Hostserver1
- Hostserver2
tasks:
- name: Install php and nginx
package:
name:
- php
- nginx
state: present
Run the playbook by executing the below command.
$ ansible-playbook -i inventory web.yaml
Here is the output.
[nouf-admin@nouf-node project-ansible]$ ansible-playbook -i inventory web.yaml
PLAY [Play to Packages] *************************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************************
ok: [172.31.2.186]
ok: [172.31.2.187]
TASK [Install php and nginx] ********************************************************************************************************************************************
changed: [172.31.2.186]
changed: [172.31.2.187]
PLAY RECAP **************************************************************************************************************************************************************
172.31.2.186 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.31.2.187 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Conclusion
Ansible is an agentless system that uses remote SSH to carry out tasks. It is an open-source IT automation engine that may help you run more efficiently while improving your IT infrastructure's scalability, dependability, and consistency. It also simplifies job orchestration using automated workflows, provisioning, and other features.
Ansible in Devops is an excellent tool that deserves excellent hosting. Consider Liquid Web’s Dedicated Servers with AlmaLinux for your next project with Ansible at the helm. Contact the sales team and customize your server specifically to your project needs.
Related Articles:

About the Author: Mohammed Noufal
Mohammed Noufal has worked as a senior server administrator for 8+ years. He can be found on LinkedIn to know more or connect.
Our Sales and Support teams are available 24 hours by phone or e-mail to assist.
Latest Articles
CentOS Linux 7 EOL — everything you need to know
Read ArticleHow to install Node.js on Linux (AlmaLinux)
Read ArticleUpgrading or installing PHP on Ubuntu — PHP 7.2 and Ubuntu 22.04
Read ArticleWhy is your IP blocked?
Read ArticleLiquid Web Terraform Provider Deploying WordPress
Read Article