The Definitive Guide to Deploying and Orchestrating Ansible on CentOS and RHEL Architectures

The implementation of Ansible within the CentOS and Red Hat Enterprise Linux (RHEL) ecosystems represents a critical shift from manual server administration to scalable, infrastructure-as-code (IaC) orchestration. Ansible is a sophisticated automation engine designed to manage complex tasks, ranging from the simultaneous configuration of multiple remote hosts and the precise modification of configuration files to the seamless deployment of entire application stacks. By leveraging a push-based architecture, it eliminates the need for agent installation on managed nodes, utilizing standard SSH protocols to execute tasks. This capability simplifies routine administration and reduces the operational overhead associated with maintaining large-scale server environments.

Comprehensive Installation Frameworks for CentOS and RHEL

The installation process for Ansible varies slightly depending on the specific version of the operating system and the desired level of control over the package versioning. Whether utilizing CentOS Stream 9, CentOS Stream 10, or RHEL 9, the primary objective is to ensure the availability of the Extra Packages for Enterprise Linux (EPEL) repository, as Ansible is not typically included in the default base repositories.

The EPEL Repository Integration Process

The EPEL repository, maintained by the Fedora project, provides a curated set of high-quality software packages for Enterprise Linux. This repository is a mandatory prerequisite for the standard installation of Ansible.

On CentOS Stream 9 or RHEL 9, the repository is enabled via the DNF package manager: sudo dnf install epel-release -y

For RHEL users, an additional technical requirement exists: the CodeReady Builder (CRB) repository must be enabled. The CRB repository provides essential build dependencies that Ansible and its associated modules require to function correctly. This is achieved through the following sequence: sudo dnf config-manager --set-enabled crb sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm -y

Once the repository infrastructure is in place, the installation is executed using the following command: sudo dnf install ansible -y

In environments utilizing older versions of CentOS or specific legacy configurations where YUM is the primary manager, the process follows a similar logical path: sudo yum update sudo yum install epel-release sudo yum install ansible

Specialized Installation for CentOS Stream 10

For deployments on CentOS Stream 10, the process is optimized to install the core components. The installation sequence involves the following commands: sudo yum -y install epel-release sudo yum install ansible-core

To enhance the capabilities of Ansible on CentOS Stream 10, particularly for network automation or virtualization management, additional Python-based dependencies and collections are required. These are managed via the Python Package Index (pip) and Ansible Galaxy: sudo yum install epel-release python-pip sudo yum install python-pip sudo pip install --upgrade pyvmomi sudo pip install --upgrade paramiko sudo pip install --user ansible-pylibssh ansible-galaxy collection install ansible.netcommon ansible-galaxy collection install vyos.vyos

Alternative Installation via Pip

For administrators requiring granular control over the specific version of Ansible—often necessary to maintain compatibility with specific legacy modules—installation via pip is the recommended method. This allows the user to bypass the versioning constraints of the EPEL repository and install a specific release of the Ansible core.

Installation Verification and Technical Validation

After the execution of the installation commands, it is imperative to verify that the binary is correctly mapped to the system path and that the Python environment is properly linked.

The verification command is: ansible --version

The resulting output provides critical technical data, including the version of ansible-core, the version of Python currently being utilized by the engine, and the specific path to the configuration file. This step ensures that the installation was not interrupted and that the system recognizes the command.

Post-Installation Configuration and Inventory Management

Once Ansible is installed, the system must be configured to identify the targets it will manage. This is handled through the inventory system and the primary configuration file.

The Configuration Hierarchy

Ansible utilizes two primary files located in the /etc/ansible/ directory: - /etc/ansible/hosts: This file serves as the inventory, containing the list of all managed hosts and their group assignments. - /etc/ansible/ansible.cfg: This file defines the global settings for the Ansible engine, such as default SSH users, timeout settings, and remote user definitions.

Inventory Design and Grouping

The inventory file is not merely a list of IP addresses but a structured document that allows for logical grouping of servers. This enables the administrator to apply specific configurations to certain tiers of the infrastructure (e.g., applying database updates only to database servers).

A standard inventory snippet looks as follows: [mc] server1 server2 [database] db1 db2

In more advanced scenarios, such as managing a vYOS virtual machine, the inventory can include specific variables for the host: [servers] vyos1 ansiblehost=192.168.1.12 ansibleuser=vyos

Establishing Secure Connectivity and Authentication

Ansible operates over SSH, and while password-based authentication is possible, it is highly discouraged for production environments due to security risks and the inefficiency of manual password entry. Key-based authentication is the industry standard.

The SSH Key Exchange Process

To establish a secure, passwordless connection between the Ansible control node and the managed node, the following workflow is implemented:

  1. Generation of the public/private key pair on the control box: ssh-keygen (The user must press enter through the prompts to accept default settings).

  2. Transfer of the public key to the remote managed node: ssh-copy-id [email protected]

This process ensures that the control node can authenticate via the SHA256 fingerprint of the key, allowing the Ansible engine to execute commands on the remote host without requiring a password for every task.

Playbook Development and Execution Logic

Playbooks are the core of Ansible's power. They are YAML files that describe the desired state of the system and the sequence of actions required to reach that state.

Playbook Structural Analysis

A playbook defines which hosts to target and which tasks to perform. For example, a playbook designed to install specific system utilities and ensure time synchronization would be structured as follows:

hosts: all become: true tasks: - name: Install useful system utilities ansible.builtin.dnf: name: - vim - htop - tmux state: present - name: Ensure chrony is running for time sync ansible.builtin.systemd: name: chronyd state: started enabled: true - name: Display the kernel version ansible.builtin.command: uname -r register: kernelversion changedwhen: false - name: Show kernel info ansible.builtin.debug: msg: "Kernel: {{ kernel_version.stdout }}"

Execution of the Playbook

To trigger the actions defined in a playbook, the ansible-playbook command is used. If a custom inventory file is being used instead of the default /etc/ansible/hosts, the -i flag must be specified: ansible-playbook -i inventory test-setup.yml

Multi-Tier Application Deployment Example

In complex scenarios, such as deploying a web server and a database, playbooks can target different groups defined in the inventory:

  • hosts: mc tasks:
  • name: Install mc yum: name: mc state: present
  • hosts: database tasks:
  • name: Install postgresql yum: name: postgresql-server state: present

This configuration ensures that the mc package is installed on all hosts in the mc group, while the postgresql-server is exclusively deployed to the database group.

Advanced Deployment Scenarios and Cloud Integration

Ansible's utility extends beyond local virtual machines into cloud environments, such as Amazon EC2. In these environments, Ansible can be used to provision instances immediately after they are brought online.

Provisioning Cloud Instances

When provisioning an instance, the playbook may need to handle specific cloud-based parameters, such as security groups and root access via injected SSH keys. A sample cloud-provisioning task involves: - Setting the user to root. - Disabling gather_facts to speed up execution. - Installing the Apache web server (httpd). - Ensuring the service is started and enabled. - Managing firewall settings, such as stopping the iptables service to allow traffic.

Example task sequence: - name: Install | Apache action: yum pkg=httpd state=installed - name: Machine | Launch Apache service action: service name=httpd state=started enabled=true

Network and Firewall Considerations

Network security is a critical component of any Ansible deployment. CentOS 9 and RHEL utilize firewalld by default. Administrators must ensure that the necessary ports (primarily port 22 for SSH) are open to allow the control node to communicate with the managed nodes. Failure to configure the firewall correctly will result in "unreachable" errors during playbook execution.

Technical Specification Summary

The following table outlines the technical requirements and tools associated with the Ansible deployment on CentOS/RHEL.

Component Requirement/Value Purpose
OS Compatibility CentOS Stream 9, 10 / RHEL 9 Base Operating System
Python Version 3.9+ Execution Engine
Primary Repository EPEL Source for Ansible packages
RHEL Dependency CRB Repository Build dependencies
Auth Method SSH Key-Based Secure remote access
Config File /etc/ansible/ansible.cfg Global settings
Inventory File /etc/ansible/hosts Managed node listing
Package Manager DNF / YUM Software installation

Troubleshooting and Verification Commands

To ensure the environment is functioning correctly, several diagnostic commands can be utilized:

  • To test the connection to all hosts in the inventory: ansible all -m ping

  • To verify facts on a specific network device (e.g., vYOS) using the networkcli connection: ansible all -i 192.168.1.12, -c ansible.netcommon.networkcli -u vyos -k -m vyos.vyos.vyosfacts -e ansiblenetwork_os=vyos.vyos.vyos

Conclusion

The deployment of Ansible on CentOS and RHEL transforms the nature of system administration from a series of manual, error-prone steps into a disciplined, repeatable process. By leveraging the EPEL repository for installation and implementing a strict SSH key-based authentication framework, administrators can achieve a high level of security and efficiency. The ability to group hosts within an inventory file and define complex states within YAML playbooks allows for the simultaneous management of vast server clusters, ensuring consistency across the environment. From simple utility installations to complex cloud provisioning and network device management via specialized collections like ansible.netcommon, Ansible provides a robust framework for any professional DevOps pipeline. The transition to this automation-centric model not only reduces the time required for deployment but also ensures that the infrastructure remains in the desired state, effectively eliminating configuration drift.

Sources

  1. ServerSpace Support
  2. OneUptime Blog
  3. TopVCF
  4. CentOS Wiki

Related Posts