Install Ansible on AlmaLinux 9 / RHEL 9 with Best Steps

Posted on

Install Ansible on AlmaLinux 9 / RHEL 9 with Best Steps

Install Ansible on AlmaLinux 9 / RHEL 9 with Best Steps

This guide provides a comprehensive walkthrough on how to Install Ansible on AlmaLinux 9 / RHEL 9. Ansible, a powerful open-source automation tool, simplifies IT infrastructure management by allowing administrators to configure and manage numerous nodes from a central server. Its agentless architecture, relying on SSH for communication, makes it easy to deploy and configure. The Orcacore team has put together the following steps to guide you through the Install Ansible on AlmaLinux 9 / RHEL 9 process.

Complete Guide To Install Ansible on AlmaLinux 9 / RHEL 9

Before you begin the Install Ansible on AlmaLinux 9 / RHEL 9 procedure, ensure you have access to your server as a non-root user with sudo privileges. Refer to these guides for assistance: Initial Server Setup with AlmaLinux 9 and Initial Server Setup with Rocky Linux 9.

For demonstration purposes, we will use three AlmaLinux OS instances: one as the control node and two as host servers. The user orca-admin, with sudo privileges, will be used as the non-root user.

You can Install Ansible on AlmaLinux 9 / RHEL 9 via DNF (with the EPEL release) or using pip. Let’s explore both methods.

Step 1. Ansible Installation with DNF on AlmaLinux 9 / RHEL 9

First, update the system and install the EPEL repository using these commands:

# sudo dnf update -y
# sudo dnf install epel-release -y

Next, use the DNF package manager to Install Ansible on AlmaLinux 9 / RHEL 9:

sudo dnf install ansible -y

Once the installation is complete, verify it by checking the Ansible version:

ansible --version
Ansible version on AlmaLinux 9
Install Ansible on AlmaLinux 9 / RHEL 9 with DNF

Step 2. Ansible Installation with pip on AlmaLinux 9 / RHEL 9

To get the latest version of Ansible, installing it with pip (Python’s package installer) is recommended. First, install Python and pip:

# sudo dnf update -y
# sudo dnf install python3-pip -y
# sudo pip3 install --upgrade pip

Verify the Python and pip installations by checking their versions:

python3 -V
**Output**
Python 3.9.16
pip3 --version
**Output**
pip 23.3.1 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)

Now, use pip to Install Ansible on AlmaLinux 9 / RHEL 9:

# sudo pip3 install setuptools-rust wheel
# sudo python3 -m pip install ansible
**Output**
...
Successfully installed MarkupSafe-2.1.3 ansible-8.5.0 ansible-core-2.15.5 cffi-1.16.0 cryptography-41.0.5 importlib-resources-5.0.7 jinja2-3.1.2 packaging-23.2 pycparser-2.21 resolvelib-1.0.1

Step 3. Configure Ansible on AlmaLinux 9 / RHEL 9

When you Install Ansible on AlmaLinux 9 / RHEL 9 using DNF, the default configuration file (ansible.cfg) is automatically created in /etc/ansible. However, if you install Ansible with pip, you need to create it manually.

It’s best practice to have a separate config file for each project. Here’s how:

First, create a project directory and navigate into it:

# mkdir ansible-project
# cd ansible-project

Then, create the Ansible configuration file using a text editor like Vi or Nano:

vi ansible.cfg

Add the following content, adjusting the values to match your environment:

[defaults]
inventory      = /home/orca-admin/ansible-project/inventory
remote_user = orca-admin
host_key_checking = False

[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False

Save and close the file.

Next, create the inventory file in your Ansible project directory:

vi inventory

Add your host servers with their IP addresses:

[Hostserver1]
198.30.2.186

[Hostserver2]
198.30.2.187

Save and close the file.

Step 4. Generate SSH Keys for Ansible Remote User

Now, generate SSH keys for the remote user and share them with the host servers:

ssh-keygen
Generate SSH Keys for Ansible Remote User

Share the SSH keys with your host servers:

# ssh-copy-id orca-admin@198.30.2.186
# ssh-copy-id orca-admin@198.30.2.187

Finally, on your Host Servers, configure sudo to avoid password prompts:

echo "orca-admin ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/orca-admin

Step 5. Verify Ansible Remote Connection on AlmaLinux 9 / RHEL 9

Use the ping module to verify connectivity between the control node and the host servers:

ansible -i inventory all -m ping
Verify Ansible Remote Connection

Step 6. Test Ansible by Creating a Sample PlayBook on AlmaLinux 9 / RHEL 9

Let’s create a sample playbook to test Ansible’s functionality on the host servers. Create a file named web.yaml in the ansible-project directory:

# cd ansible-project
# vi web.yaml

Add the following content to install Nginx and PHP:

- name: Play to Packages
  hosts:
    - Hostserver1
    - Hostserver2
  tasks:
  - name: Install php and nginx
    package:
      name:
        - php
        - nginx
      state: present

Save and close the file.

Run the Ansible playbook:

ansible-playbook -i inventory web.yaml
Test Ansible by Creating a Sample PlayBook

If the playbook runs successfully, Ansible is working correctly. For more information, refer to the Documentation page.

Conclusion

You have now successfully learned how to Install Ansible on AlmaLinux 9 / RHEL 9. You can install and configure Ansible using either DNF or PIP. Remember to generate and copy SSH keys to your host servers to enable communication and test your setup with a sample playbook.

You may also be interested in these articles:

Python 3.12 Installation on AlmaLinux 9 / RHEL 9

5 Best Free Alternatives To Python

Install OpenSSL in RHEL 9

Restart Network Service on RHEL 9

Install Zabbix 6.4 on AlmaLinux 9

Alternative Solutions for Automating Tasks on AlmaLinux 9 / RHEL 9

While Ansible is a fantastic tool, other options can achieve similar automation goals on AlmaLinux 9 / RHEL 9. Here are two alternative approaches:

1. SaltStack

SaltStack is another powerful open-source automation and configuration management tool. Like Ansible, it aims to streamline IT tasks. However, SaltStack uses a minion (agent) installed on each managed node by default, although it also supports an agentless mode. This agent-based architecture can offer faster execution speeds in some scenarios, as the agent maintains a persistent connection to the Salt master.

Explanation:

SaltStack uses a publish/subscribe model, where the Salt master sends commands, and the minions execute them. Salt states (similar to Ansible playbooks) define the desired state of the system. SaltStack is known for its speed and scalability, making it suitable for large and complex environments.

Code Example (Salt State to install Nginx):

Create a file named nginx.sls (Salt State file) on the Salt master:

nginx:
  pkg.installed:
    - name: nginx
  service.running:
    - name: nginx
    - require:
      - pkg: nginx

This Salt State will ensure that the Nginx package is installed and the Nginx service is running. The require statement ensures that the package is installed before the service is started.

Apply the state to the target nodes using the following command on the Salt master:

salt '*' state.apply nginx

This command tells all minions ('*') to apply the nginx state.

2. Chef

Chef is a configuration management tool that allows you to define infrastructure as code. It uses a client (agent) on each managed node to apply configurations defined in cookbooks. While Chef has a steeper learning curve than Ansible or SaltStack, its powerful DSL (Domain Specific Language) and strong community support make it a viable alternative.

Explanation:

Chef uses a central server (Chef Server) to store cookbooks and node metadata. Clients periodically connect to the Chef Server to download configurations and apply them to the system. Cookbooks are collections of recipes that define the desired state of the system.

Code Example (Chef Recipe to install Nginx):

Create a recipe file named default.rb within a cookbook:

package 'nginx' do
  action :install
end

service 'nginx' do
  action [:enable, :start]
end

This recipe will install the Nginx package and enable and start the Nginx service.

Upload the cookbook to the Chef Server and then apply it to the target nodes using Chef client.

These are two alternatives to Ansible that can be used to automate tasks on AlmaLinux 9 / RHEL 9. Each tool has its strengths and weaknesses, so it’s essential to choose the one that best fits your specific needs and environment.

Leave a Reply

Your email address will not be published. Required fields are marked *