On CentOS 7 or RHEL 7 you can install Ansible in two ways: with yum from the EPEL repository, which gives you Ansible 2.9, or with pip inside a Python 3 virtual environment. The commands above install the EPEL package three times over, turn off the firewall, and use a pip bootstrap URL that no longer works with the system's Python 2.7. This guide gives a corrected, tested sequence for each method, explains how to deal with CentOS 7 reaching end of life, and covers the errors you are likely to hit.
How to install Ansible in Centos 7/RHEL 7?
$ sudo yum install wget -y
$ wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
$ yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
$ sudo rpm -i epel-release-latest-7.noarch.rpm
$ sudo yum update -y
$ sudo yum install ansible -y
$ systemctl stop firewalld
$ systemctl disable firewalldCode language: JavaScript (javascript)
Install Ansible via Pip in RHEL/CENTOS
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py --insecure
$ python get-pip.py
$ pip --version
$ python get-pip.py
$ pip uninstall ansible
$ pip install ansible
$ ansible --versionCode language: JavaScript (javascript)
Before you start: CentOS 7 and RHEL 7 are end of life
CentOS 7 reached end of life on 30 June 2024. RHEL 7 left its standard maintenance phase on the same date and now only gets updates through paid Extended Life Cycle Support. One practical result is that mirrorlist.centos.org no longer serves CentOS 7, so a fresh yum install or yum update usually fails until you point yum at the CentOS Vault archive. EPEL 7 has also been archived, so the epel-release-latest-7 URL used above may no longer work.
If yum fails on CentOS 7 with Could not resolve host: mirrorlist.centos.org, switch the repository files to the vault:
# Back up the repo files first
sudo cp -a /etc/yum.repos.d /etc/yum.repos.d.bak
# Comment out mirrorlist and point baseurl at vault.centos.org
sudo sed -i -e 's/^mirrorlist/#mirrorlist/' \
-e 's|^#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|' \
/etc/yum.repos.d/CentOS-*.repo
sudo yum clean all
sudo yum makecacheCode language: PHP (php)
Note: Use these steps for labs and for legacy servers you cannot migrate yet. For anything new, use Rocky Linux, AlmaLinux or RHEL 8/9, where sudo dnf install ansible-core works from the standard AppStream repository.
Method 1: install Ansible with yum from EPEL (corrected)
On CentOS 7, the epel-release package is in the built-in extras repository, so you do not need wget or a manual RPM download. One command enables EPEL.
- Enable EPEL. On CentOS 7 run
sudo yum install -y epel-release. - On RHEL 7, enable the optional and extras repositories first, because some EPEL packages depend on them:
sudo subscription-manager repos --enable rhel-7-server-optional-rpms --enable rhel-7-server-extras-rpms. Then install theepel-releaseRPM from the Fedora EPEL archive. - Check that EPEL appears in the repository list:
yum repolist. - Install Ansible:
sudo yum install -y ansible. - Check the installation:
ansible --version.
# CentOS 7
sudo yum install -y epel-release
yum repolist | grep -i epel
sudo yum install -y ansible
ansible --versionCode language: PHP (php)
The EPEL 7 package is Ansible 2.9 running on the system Python 2.7. It works well for learning classic playbooks, but it does not support Ansible collections the way current releases do, and it no longer receives fixes.
Verify that Ansible works
ansible --version should print output similar to the following. Exact patch versions will vary.
ansible 2.9.27
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, ...)Code language: JavaScript (javascript)
Next, run a module against the local machine. This confirms that Ansible can actually execute modules, not just that it is installed:
ansible localhost -m ping
# Expected result (a warning about an empty inventory is normal):
# localhost | SUCCESS => {
# "changed": false,
# "ping": "pong"
# }Code language: PHP (php)
To manage remote servers, add them to /etc/ansible/hosts or to your own inventory file. Make sure you can SSH to each one with a key, then run ansible all -i inventory -m ping.
Method 2: install Ansible with pip (corrected)
The pip commands above no longer work as written. https://bootstrap.pypa.io/get-pip.py now refuses to run on Python 2.7. Running get-pip.py as root also installs packages system-wide, where they can conflict with files that yum manages. A safer approach is to use the Python 3.6 packages from the CentOS/RHEL 7 base repository and install Ansible into a virtual environment.
# Python 3.6 is in the base repo on CentOS/RHEL 7.7 and later
sudo yum install -y python3 python3-pip
# Create and activate an isolated environment
python3 -m venv ~/ansible-venv
source ~/ansible-venv/bin/activate
# Upgrade pip first; old pip cannot use modern binary wheels
pip install --upgrade pip
# Install Ansible and confirm
pip install ansible
ansible --versionCode language: PHP (php)
On Python 3.6, pip uses each package's Python version requirements to pick the newest compatible release. That is the Ansible 4.x community package, which includes ansible-core 2.11. Newer ansible-core releases need Python 3.8 or later on the control node, so you cannot get them on a stock CentOS 7 system.
Note: Run source ~/ansible-venv/bin/activate in every new shell before using Ansible, or add ~/ansible-venv/bin to your PATH. Do not use curl --insecure, which turns off TLS certificate checks. Fix the system CA bundle instead with sudo yum reinstall ca-certificates.
Keep firewalld running
You do not need to stop or disable firewalld to use Ansible. The control node opens outbound SSH connections. Managed nodes only need inbound TCP port 22. Turning off the firewall leaves every listening service on the host exposed. Allow SSH instead:
sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload
sudo firewall-cmd --list-servicesCode language: PHP (php)
Open other ports only when a playbook deploys a service that needs them, and preferably do it from the playbook itself with the firewalld module.
Common errors and fixes
| Error | Cause | Fix |
|---|---|---|
Could not resolve host: mirrorlist.centos.org | CentOS 7 is end of life and its mirrors are gone | Point the repo files at vault.centos.org as shown at the top of this guide |
No package ansible available. | EPEL is not enabled, or yum cache is stale | Install epel-release, then run sudo yum clean all and check yum repolist |
package epel-release ... is already installed | Running rpm -i after yum install of the same RPM | Nothing to fix; install the package only once |
ERROR: This script does not work on Python 2.7 | Current get-pip.py dropped Python 2.7 | Use the Python 3 venv method above |
Build fails compiling cryptography or asks for Rust | Old pip cannot find a prebuilt wheel | Run pip install --upgrade pip inside the venv, then retry |
UNREACHABLE! ... Permission denied (publickey) | SSH key not set up for the remote user | Run ssh-copy-id user@host and test with plain ssh first |
Which method to choose
| yum + EPEL | pip in a venv | |
|---|---|---|
| Ansible version | 2.9 | ansible-core 2.11 (Ansible 4.x) |
| Python used | System Python 2.7 | Python 3.6 |
| Root needed | Yes | Only for installing python3 |
| Collections support | Limited | Yes |
| Best for | Quick labs, legacy playbooks | Using newer modules on a legacy host |
The best long-term choice is to run the control node on a supported distribution. If that control node still has to manage CentOS 7 hosts, remember that ansible-core 2.17 dropped support for Python 2.7 and 3.6 on managed nodes. Pin ansible-core to 2.16 or earlier until those hosts are retired.
Frequently asked questions
Which Ansible version does yum install on CentOS 7?
The EPEL 7 repository provides Ansible 2.9, which runs on the system Python 2.7. For a newer release on the same host, use pip in a Python 3 virtual environment, which gives you ansible-core 2.11.
Do I need to disable firewalld to use Ansible?
No. Ansible connects over SSH, so managed nodes only need port 22 open. Allow it with firewall-cmd --permanent --add-service=ssh and keep the firewall running.
Why does yum fail on CentOS 7 with a mirrorlist error?
CentOS 7 reached end of life in June 2024 and its mirror network was shut down. Point the repo files at vault.centos.org, or better, move to a supported distribution such as Rocky Linux or AlmaLinux.
Can the latest Ansible manage CentOS 7 servers?
Not with ansible-core 2.17 or newer, because those releases no longer support Python 2.7 or 3.6 on managed nodes. Use ansible-core 2.16 or earlier on the control node while you still have CentOS 7 hosts.
Should I install Ansible with pip as root?
No. A root-level pip install can overwrite or conflict with Python packages that yum manages. Install into a virtual environment, or use pip install --user.
Related tutorials
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals
A valuable addition would be guidance on maintaining installation consistency across multiple control nodes. In enterprise environments, installing Ansible is rarely a one-time manual task. Teams typically standardize the Python environment, package versions, collections, and configuration files so every automation node behaves identically. Without this consistency, playbooks can produce different results depending on where they’re executed, making troubleshooting and change management much more difficult. Building a reproducible installation process from the beginning helps ensure long-term reliability as automation adoption grows.