How to Install and Configure Ansible on CentOS 7

Ansible is an automation configuration management system that can control a large number of client machines with easy administration from a central location. Ansible communicates over SSH tunnels without requiring any software installation on client machines, making it agentless and lightweight.

The Ansible configuration files use YAML data format due to its expressiveness and similarity to popular programming languages. Clients can be managed using command-line tools or playbooks for complex automation tasks.

Prerequisites

  • CentOS 7 system with root user access

  • SSH keys configured for passwordless authentication

  • Network connectivity between control node and managed hosts

Installing Ansible on CentOS 7

Ansible is not available in the default CentOS repository, so we need to add the EPEL repository first.

Step 1 − Add EPEL Repository

sudo yum install epel-release -y

Step 2 − Update System Packages

sudo yum update -y

Step 3 − Install Ansible

sudo yum install ansible -y

This command will install Ansible along with its dependencies including python-paramiko, PyYAML, python-jinja2, and sshpass.

Configuring Ansible Hosts

The inventory file located at /etc/ansible/hosts tracks all servers and clients that Ansible will manage.

Edit the Hosts File

sudo nano /etc/ansible/hosts

Host Configuration Syntax

[group_name]
alias ansible_ssh_host=server_ip_address

Sample Configuration

[servers]
client1 ansible_ssh_host=192.168.0.10
client2 ansible_ssh_host=192.168.0.11

SSH Key Authentication Setup

Configure SSH keys for passwordless authentication by creating a group variables file that specifies the SSH user.

sudo mkdir /etc/ansible/group_vars
sudo vi /etc/ansible/group_vars/servers

Add the following content to specify the SSH user −

---
ansible_ssh_user: root

Note: The triple dashes (---) must be at the beginning of YAML files. For global settings, use /etc/ansible/group_vars/all.

Testing Ansible Configuration

Ping All Hosts

ansible -m ping all
client1 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
client2 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

Ping Specific Groups or Hosts

ansible -m ping servers
ansible -m ping client1

Running Shell Commands

Use the shell module to execute commands remotely on managed hosts.

Check Disk Usage

ansible -m shell -a 'df -h' client1

Check Memory Usage

ansible -m shell -a 'free -m' client1
client1 | SUCCESS | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:           3.7G         868M        1.9G        8.4M        1.0G        2.6G
Swap:          2.0G           0B        2.0G

Key Points

  • Agentless − No software installation required on managed hosts

  • SSH-based − Uses secure SSH connections for communication

  • YAML syntax − Human-readable configuration format

  • Inventory management − Centralized host grouping and organization

  • Modular design − Built-in modules for various system tasks

Conclusion

Installing and configuring Ansible on CentOS 7 provides a powerful automation platform for managing multiple servers from a central location. With proper SSH key setup and inventory configuration, you can execute commands and deploy configurations across your infrastructure efficiently. This foundation enables advanced automation through playbooks and roles for complex deployment scenarios.

Updated on: 2026-03-17T09:01:38+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements