How To Install Ansible In Linux

How To Install Ansible In Linux

Reading time1 min
#Automation#Linux#DevOps#Ansible#LinuxAutomation#ServerManagement

Step-by-Step Guide: Installing Ansible on Linux for Reliable Automation

There are few tools as fundamental in infrastructure automation as Ansible. Clean installs with predictable state are critical, especially when streamlining configuration management in production environments.


OS Updates First: Avoid Dependency Issues

Before installation, update system packages. Skipping this leads to version conflicts or stale repository metadata.

  • Debian/Ubuntu

    sudo apt update && sudo apt upgrade -y
    
  • CentOS 7/8 / RHEL 7/8

    sudo yum update -y
    
  • Fedora

    sudo dnf upgrade --refresh -y
    

Note: Some older hosts (legacy RHEL/Centos) may require yum clean all if you see repodata errors.


Install Method: Repo vs pip

MethodVersionUse Case
System package managerStable, may be oldMinimalist installs, prod servers
Python pipLatest upstreamDevelopment, multi-version handling

Direct-from-source builds exist, but rarely used outside CI or special audit workflows.


Installing via OS Package Manager

Debian / Ubuntu 22.04+ (Ansible 5.x/6.x typical)

To pin Ansible to a supported, up-to-date version on Ubuntu, add the official PPA:

sudo apt install software-properties-common -y
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt update
sudo apt install ansible -y

Check version:

ansible --version
# ansible [core 2.15.x] on python 3.10.x, typically

Gotcha: Without the PPA, Ubuntu often lags multiple versions behind upstream. This breaks playbooks using newer features (e.g., yaml vars plugins).


CentOS/RHEL 7/8

Enable EPEL, then install:

sudo yum install epel-release -y
sudo yum install ansible -y

Fedora, as usual, gets it directly:

sudo dnf install ansible -y

Test installation and where the executable resolves:

ansible --version
which ansible

Installing Ansible via pip (Latest Upstream)

Critical when you need bleeding-edge modules or are working in isolated Python environments.

Dependencies:

  • Python 3.9+ recommended for Ansible >=2.15; use python3.11 on modern distros for new workflows.

Install prerequisites and optional virtualenv support:

# Debian/Ubuntu
sudo apt install python3 python3-pip python3-venv -y
# CentOS/RHEL/Fedora
sudo yum install python3 python3-pip python3-virtualenv -y  # or dnf

Create and activate a virtualenv:

python3 -m venv ~/ansible-venv
source ~/ansible-venv/bin/activate

Upgrade pip and install Ansible:

pip install --upgrade pip
pip install 'ansible>=2.15,<3.0.0'

Check:

ansible --version

When using pip, ensure PYTHONPATH and system path do not conflict with system-level (rpm/deb) installs. Overlapping installations often lead to subtle runtime errors.

Deactivate environment when finished:

deactivate

Practical Sanity Test

Verify functional install using the default connection plugin (no SSH). Create hosts.ini:

localhost ansible_connection=local

Run a ping:

ansible -i hosts.ini all -m ping

Expected:

localhost | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

If you see ModuleNotFoundError or unexpected Python tracebacks, double-check your virtualenv and path priority.


Non-Obvious Tips

  • Managing dozens of environments? Consider pinning Ansible versions using a requirements.txt and pip install -r requirements.txt.
  • When automating with non-root users, ensure inventory and playbooks have appropriate permissions.
  • For remote hosts, SSH key auth is strongly recommended.
  • Native YAML syntax improvements and ansible-lint pay off early—catch subtle errors in role development.
  • For idempotency checks, try: ansible-playbook --check --diff ...

Known Issues

  • Mixing OS package manager and pip installs on the same host can lead to ansible command resolving to the wrong version, especially on CentOS/RHEL.
  • On Ubuntu, the system package sometimes pulls in python2 dependencies. Always verify python --version vs python3 --version.
  • Some RHEL and derivative systems require subscription-manager enabled or repo entitlements to access latest EPEL packages.

Wrap-Up

Efficient Ansible installation isn’t just about running a few commands—it’s about establishing trust in your automation baseline. Pin versions, isolate environments, and always verify install paths in mixed-use systems.

From here, transition to writing simple playbooks. Integrate with CI pipelines or staged deployment flows as your infrastructure requirements evolve.

Side note: For reproducibility in automation pipelines, containerize your Ansible install (e.g., use quay.io/ansible/ansible-runner) rather than managing host packages directly. Not perfect, but more predictable.


Sample Error (for reference):

$ ansible --version
Traceback (most recent call last):
  File "/usr/bin/ansible", line 36, in <module>
    from ansible.cli.adhoc import main
ModuleNotFoundError: No module named 'ansible'

Symptom: Two conflicting Ansible installs. Solution: Remove one, review your $PATH, and prefer venv-based isolation.