Orchestrating Infrastructure: The Definitive Guide to Installing and Configuring Ansible on CentOS 7

The pursuit of infrastructure automation is a cornerstone of modern systems administration, and Ansible stands as a premier open-source automation software developed in Python. Designed to run on UNIX-like systems, Ansible provides a powerful mechanism to provision and configure both UNIX-like and Windows environments. Its primary architectural advantage is its agentless nature; unlike competing automation frameworks, Ansible does not require a proprietary agent to be installed on the target nodes. Instead, it leverages standard SSH connections and the Python interpreter already present on the target system to execute tasks. This capability allows an administrator to install Ansible on a central cloud server to manage a fleet of other cloud servers, or alternatively, configure it on a personal workstation to manage a hybrid mix of cloud and on-premises infrastructure. By removing the need for agent management, Ansible reduces the overhead of software maintenance on managed nodes and minimizes the security attack surface.

System Prerequisites and Environmental Requirements

Before initiating the installation process, the administrative environment must meet specific hardware and software criteria to ensure stability and performance. The baseline requirements for a deployment environment include a Cloud VPS or a Dedicated Server equipped with at least 1GB of RAM and a clean installation of CentOS 7. From an access perspective, the operator must be logged into the system via SSH with root privileges or as a user possessing sudo (superuser do) permissions. This is critical because the installation of system-level repositories and the modification of global configuration files in the /etc directory require elevated permissions.

The initial stage of any deployment on CentOS 7 should begin with a comprehensive update of the operating system. This ensures that all existing system libraries are current and that the kernel is patched against known vulnerabilities, which provides a stable foundation for the Python-based Ansible environment. The command to synchronize the system with the latest available packages is:

sudo yum update

Comprehensive Installation Methodologies

Depending on the specific needs of the production environment—whether the priority is stability, ease of maintenance, or access to the absolute latest feature set—there are multiple paths to installing Ansible on CentOS 7.

Standard Installation via EPEL Repository

The most common method for installing Ansible involves the Extra Packages for Enterprise Linux (EPEL) repository. EPEL is a community-managed repository that provides a vast array of add-on packages for Enterprise Linux, including the Ansible binaries. While EPEL typically provides a recent and stable version of the software, it may not always contain the absolute latest release.

To utilize this method, the EPEL repository must first be integrated into the system's package manager:

sudo yum install epel-release

Once the EPEL repository is active and the system can resolve the package metadata, the Ansible installation is performed via the yellowdog updater, modified (yum) package manager:

sudo yum install ansible

Advanced Installation for Latest Stable Versions

For organizations that require the absolute latest stable version of Ansible—often necessary for accessing new modules or specific bug fixes—the official Ansible RPM repository is the recommended path. This bypasses the version lag occasionally found in EPEL.

To implement this, a custom repository file must be created at /etc/yum.repos.d/ansible.repo. The file must contain the following configuration blocks:

[ansible] name=Ansible RPM repository for Enterprise Linux 7 - $basearch baseurl=https://releases.ansible.com/ansible/rpm/release/epel-7-$basearch/ enabled=1 gpgcheck=1 gpgkey=https://releases.ansible.com/keys/RPM-GPG-KEY-ansible-release.pub

Alternatively, administrators can use an automated approach to set up the latest release by executing the following sequence:

yum install https://extras.getpagespeed.com/release-latest.rpm yum install ansible-release

Post-Installation Verification

Following the completion of the installation process, it is imperative to verify that the binary is correctly placed in the system path and that the Python dependencies are functioning. The verification is performed by querying the version of the installed software:

ansible --version

This command outputs the installed version, the config file location, and the Python version currently being utilized. If the command returns an error or a "command not found" prompt, it indicates an interruption in the installation process or a failure in the package manager's ability to resolve the dependencies.

Technical Architecture and Configuration

Ansible operates through a combination of configuration files and execution scripts. The primary configuration files are located in the /etc/ansible directory.

The Role of the Hosts File

The /etc/ansible/hosts file is the central inventory mechanism. It keeps track of all the servers that the Ansible control node is aware of. This file is highly flexible and can be configured in multiple ways to categorize servers into logical groups. To edit this file, root privileges are required:

sudo vi /etc/ansible/hosts

Upon opening the file, users will encounter numerous example configurations that are commented out. It is advised to retain these examples as they serve as a reference for implementing complex scenarios in the future.

Inventory Management and Grouping

An inventory file allows for the organization of hosts into groups, which enables the administrator to target specific sets of servers for different tasks. For example, a snippet of an inventory file might look like this:

[mc] server1 server2

[database] db1 db2

In this configuration, server1 and server2 are grouped under the "mc" label, while db1 and db2 are grouped under "database". This allows the administrator to deploy software to the database group without affecting the application servers in the mc group.

The ansible.cfg Configuration File

The /etc/ansible/ansible.cfg file serves as the primary settings directory for the Ansible engine. It defines global behaviors, such as the default remote user, the location of the inventory file, and the SSH timeout settings. Proper configuration of this file ensures that the control node interacts with the target hosts according to the organization's security and performance standards.

Secure Connectivity and Authentication

While Ansible can connect to remote hosts using standard password authentication, this method is inefficient for scale and poses security risks. The industry standard is to implement SSH key-based authentication.

SSH Key Pair Generation

To establish secure, passwordless communication, the administrator must generate an RSA key pair on the control node. This is achieved using the following command:

ssh-keygen

During this process, the user is prompted for a file location and a passphrase. For maximum automation efficiency, the default location is used, and the passphrase is left empty by pressing enter. The resulting files are stored in the .ssh directory: - Private Key: /home/sneluser/.ssh/idrsa - Public Key: /home/sneluser/.ssh/idrsa.pub

The public key is then distributed to the target servers, allowing the control node to authenticate via a secure handshake rather than a manual password entry.

Operational Execution and Module Usage

Ansible interacts with clients through command-line tools and configuration scripts known as Playbooks. The core functionality is driven by modules, which can be written in any language and communicate using the standard JSON format. Configuration files themselves are written in YAML (YAML Ain't Markup Language), chosen for its expressive nature and similarity to popular markup languages.

Ad-Hoc Commands and Testing

Ad-hoc commands are used for quick tasks and testing connectivity without the need for a full playbook.

The ping module is used to verify connectivity across the inventory. The command to ping all hosts is:

ansible -m ping all

A successful connection yields a JSON response indicating a "pong" and a "changed" status of false. For example: [sneluser@host ~getActiveSheet] ansible -mP ping all server1 | SUCCESS => { "changed": false, "ping": "pong" }

Beyond simple connectivity tests, the shell module allows administrators to execute real-time commands on multiple servers in parallel. To update all servers simultaneously, the following command is used:

ansible -m shell -a 'yum -y update' all

If a task needs to be isolated to a specific machine, the target is specified by name:

ansible -m shell -a 'yum -y update' server1

Playbook Development and Automation

Playbooks are the primary vehicle for complex automation. A playbook is a YAML file that describes the exact order of actions to be performed on a set of hosts.

Creating a Playbook

A playbook file, such as playbook.yml, defines the target hosts and the specific tasks to be executed. Consider a scenario where different software must be installed on different groups:

  • hosts: mc tasks:

    • name: Install mc yum: name: mc state: present
  • hosts: database tasks:

    • name: Install postgresql yum: name: postgresql-server state: present

In this example, the "mc" installation is triggered for all hosts in the mc group, and the PostgreSQL server installation is triggered for the database group. The "state: present" declaration ensures that the package is installed if it is missing, maintaining an idempotent state.

Executing Playbooks

To run the defined playbook against the specified inventory, the ansible-playbook command is used:

ansible-playbook -i inventory playbook.yml

The execution engine reads the inventory file, identifies the hosts associated with the groups mentioned in the playbook, and executes the tasks sequentially across those hosts.

Summary of Technical Specifications and Commands

The following table provides a consolidated view of the essential components and commands required for Ansible deployment on CentOS 7.

Component Action/Command Purpose
Repository sudo yum install epel-release Enables EPEL for Ansible installation
Installation sudo yum install ansible Installs the Ansible framework
Verification ansible --version Confirms installation and version
Inventory /etc/ansible/hosts Manages list of target servers
Configuration /etc/ansible/ansible.cfg Directs global Ansible settings
Authentication ssh-keygen Generates RSA keys for secure login
Connectivity ansible -m ping all Tests connection to all hosts
Remote Task ansible -m shell -a '[command]' all Executes shell commands in parallel
Automation ansible-playbook -i [inv] [file].yml Executes complex automation scripts

Detailed Analysis of Automation Impact

The deployment of Ansible on CentOS 7 transforms the administrative workflow from a manual, error-prone process to a programmatic, repeatable sequence. By leveraging the YAML-based playbook system, organizations achieve "Infrastructure as Code" (IaC), where the state of the server is defined in a file rather than through a series of manual commands. This ensures that every server in a cluster is identical, eliminating "configuration drift" where servers diverge over time due to manual tweaks.

The use of the shell and yum modules demonstrates the power of Ansible's abstraction. Instead of logging into ten different servers to run a yum update, the administrator executes a single command from the control node. This not only saves time but ensures that the update is applied consistently across the entire fleet. Furthermore, the ability to group hosts in the inventory file allows for granular control, enabling the separation of concerns between web servers, database servers, and load balancers within a single management framework.

The transition from password authentication to SSH key-based authentication is a critical security upgrade. By using the ssh-keygen utility, administrators remove the need to store plain-text passwords in scripts or enter them manually during execution. This creates a secure, encrypted tunnel for communication, which is essential for managing cloud-based infrastructure over public networks.

Sources

  1. DigitalOcean
  2. Snel
  3. Serverspace
  4. GetPageSpeed

Related Posts