How to Install Ansible on Linux and Amazon Linux

How to Install Ansible on Linux and Amazon Linux

Reading time1 min
#Ansible#Linux#DevOps#Automation#AmazonLinux

How to Install Ansible on Linux and Amazon Linux

Ansible is agentless. You install it on one machine, the control node (your laptop, a bastion host, a CI runner), and it connects to the managed nodes over SSH. Managed nodes need only SSH access and Python; nothing Ansible-specific gets installed on them.

Two packages exist:

  • ansible-core: the engine, ansible-playbook and the built-in modules.
  • ansible: ansible-core plus a large curated set of collections (community.general, amazon.aws, ansible.posix and many more).

Start with ansible unless you want to manage collections yourself with ansible-galaxy.

Check Python first

Current ansible-core releases need a fairly new Python on the control node: 2.20 and 2.21 require Python 3.12 or later, and 2.22 (due November 2026) requires 3.13 or later. Managed nodes can run older Python, 3.9 and up.

python3 --version

This matters because pip on an older Python does not fail. It quietly installs the last ansible-core that still supports that Python, which may already be out of support.

Option 1: pipx (any distribution)

pipx is the method the Ansible project documents. It puts Ansible into its own virtual environment and links the commands into ~/.local/bin, without touching system Python packages:

sudo apt install pipx        # Fedora: sudo dnf install pipx
pipx ensurepath              # adds ~/.local/bin to PATH; open a new shell after
pipx install --include-deps ansible
ansible --version

--include-deps exposes the commands that come from ansible-core (ansible, ansible-playbook, ansible-galaxy and so on). Upgrade and add Python libraries that some modules need:

pipx upgrade --include-injected ansible
pipx inject ansible boto3 botocore      # needed by the amazon.aws collection

This works well on Ubuntu 24.04+, Debian 13 and current Fedora. On Ubuntu 22.04 the system Python is 3.10, so use the PPA below instead.

Option 2: distribution packages

Ubuntu (the Ansible PPA, newer than Ubuntu's own package):

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

Debian: sudo apt install ansible gives the version from the Debian archive, which can be well behind upstream. The Ansible docs describe how to use the Ubuntu PPA on Debian if you need newer.

Fedora:

sudo dnf install ansible           # or ansible-core for the minimal package

RHEL, Rocky Linux, AlmaLinux: ansible-core is in the AppStream repository. The full ansible package comes from EPEL:

sudo dnf install ansible-core                  # AppStream
sudo dnf install epel-release                  # Rocky, Alma; on RHEL install EPEL from its URL
sudo dnf install ansible                       # EPEL

Arch: sudo pacman -S ansible. openSUSE: sudo zypper install ansible.

Distribution packages update with the system, which is convenient. They also tend to trail upstream, so check ansible --version if a playbook uses newer features.

Amazon Linux 2023

AL2023 has ansible in its repositories, but at an old version (Ansible 8 with ansible-core 2.15):

sudo dnf install -y ansible

That is enough for simple playbooks. For a current release, use one of AL2023's newer Python packages in a virtual environment. Do not sudo pip install into the system Python: /usr/bin/python3 stays 3.9 for the life of AL2023 and dnf depends on it.

sudo dnf install -y python3.12 python3.12-pip
python3.12 -m venv ~/.venvs/ansible
~/.venvs/ansible/bin/pip install --upgrade pip
~/.venvs/ansible/bin/pip install ansible boto3 botocore
echo 'export PATH="$HOME/.venvs/ansible/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
ansible --version

ansible --version shows which Python it runs on, so you can confirm it picked up 3.12.

Amazon Linux 2 reached end of support on 2026-06-30. The old amazon-linux-extras install ansible2 route installs an outdated Ansible on an unsupported OS. Move the control node to AL2023 or another current distribution.

Test it against your hosts

Create an inventory file, inventory.ini:

[web]
203.0.113.10 ansible_user=ubuntu
203.0.113.11 ansible_user=ec2-user

[web:vars]
ansible_ssh_private_key_file=~/.ssh/my-key.pem

Then run the ping module:

ansible localhost -m ping            # control node only, no inventory needed
ansible web -i inventory.ini -m ping

Each host should answer with "ping": "pong". Despite the name, this is not ICMP: it logs in over SSH and runs a small Python module, so it proves that SSH, the user and Python on the target all work.

Common failures:

  • "Host key verification failed": connect once with ssh to accept the host key, or add it with ssh-keyscan 203.0.113.10 >> ~/.ssh/known_hosts.
  • "Permission denied (publickey)": wrong user or key. On EC2 the default user is ubuntu for Ubuntu AMIs and ec2-user for Amazon Linux.
  • No Python interpreter found on a target: install Python there (sudo apt install python3 or sudo dnf install python3), or bootstrap it with the raw module.

Then run a playbook:

ansible-playbook -i inventory.ini site.yml --check --diff    # dry run first
ansible-playbook -i inventory.ini site.yml

Quick reference

pipx install --include-deps ansible                              # any distro, Python 3.12+
sudo add-apt-repository --yes --update ppa:ansible/ansible       # Ubuntu PPA
sudo apt install ansible
sudo dnf install ansible                                         # Fedora; AL2023 (older)
sudo dnf install ansible-core                                    # RHEL, Rocky, Alma (AppStream)
python3.12 -m venv ~/.venvs/ansible && ~/.venvs/ansible/bin/pip install ansible   # AL2023, current
ansible --version
ansible all -i inventory.ini -m ping