How to Install Ansible (Ubuntu, RHEL, CentOS, macOS)

Published:27 April 2021 - 4 min. read

If you’ve got many hosts to manage, it’s time to automate configuration management by installing Ansible! Ansible is the most popular automation tool to manage configuration changes across your on-prem and cloud resources.

In this article, you’re going to learn from the ground up how to download and install an Ansible controller host on Ubuntu, RHEL, and macOS. You’ll also get a jump start on running your first commands!

This tutorial will use v2.9.20 of Ansible but the process to set up Ansible on a controller host should be near identical for later versions.

Let’s get started!

Installing Ansible on Ubuntu

You can install Ansible on any *nix-based operating system. One of the most popular choices out there is Ubuntu. Let’s kick off this tutorial and go through a walkthrough on how to make it happen.

If you’d like to set up Ansible on Ubuntu, this section is going to assume you have an Ubuntu 18+ machine with Python2 or higher installed. This tutorial will be using Ubuntu 18.04.5 LTS with Python3.

1. Connect to your soon-to-be Ansible controller on an Ubuntu host with your favorite SSH client.

2. Ansible is stored as a package that can be downloaded and installed via the apt package manager. To ensure apt can find the proper source and download any dependent packages run apt update.

sudo apt update 

The apt or apt-get command installs the packages from apt software repositories configured in Ubuntu.

3. Continue preparing apt to download and install Ansible by installing the software-properties-common package. sudo apt install software-properties-common

sudo apt install software-properties-common
Installing the package using apt command
Installing the package using apt command

4. Next, add the ansible/ansible personal package archive (PPA) as an apt repository. This repository is managed by Red Hat, not by Ubuntu sources, so you must add it manually.

sudo apt-add-repository --yes --update ppa:ansible/ansible
Addition of ansible PPA repository
Addition of ansible PPA repository

5. Now, install the ansible package by running the apt install command

sudo apt install ansible

6. When complete, confirm Ansible is installed by running the ansible --version command. If all is well ansible --version

ansible --version
Checking the Ansible version
Checking the Ansible version

Installing Ansible on RHEL

Ubuntu isn’t the only operating system Ansible supports. Red Hat Enterprise Linux (RHEL) or CentOS are also common options. Let’s start with RHEL.

If you plan to follow this section, ensure you have an RHEL release 8+ host (this tutorial will use RHEL 8.3 (Ootpa).

1. Connect to your RHEL host via SSH with your favorite SSH client.

2. Install the python3-pip and python3 package with dnf. Ansible works with Python2, but this tutorial will be using Python3 because Python2 is soon to be considered legacy.

sudo dnf install python3 python3-pip -y
Installing the python3 and python3-pip
Installing the python3 and python3-pip

3. Install the Extra Packages for Enterprise Linux (EPEL) repository. The EPEL repository contains various system packages including the Ansible package which you will install in the next step.

sudo dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
Addition of EPEL repository
Addition of EPEL repository

4. Once you’ve installed the repository, install the Ansible package by referencing the required EPEL repository and the package’s name (ansible). The dnf utility will automatically download the latest version of Ansible.

sudo dnf install  --enablerepo epel-playground  ansible

5. Now, check to ensure Ansible has been installed successfully by running

Checking the Ansible version
Checking the Ansible version

Installing Ansible on CentOS

Rounding out the *nix hosts, let’s end with setting up Ansible on CentOS.

If you plan to follow this section, ensure you have a CentOS 7+ host with Python2 or higher installed (this tutorial will use CentOS 7.9.2009 Core).

1. Connect to your CentOS host via SSH with your favorite SSH client.

2. Install the Extra Packages for Enterprise Linux (EPEL) repository. The EPEL repository contains various system packages, including the Ansible package, which you will install in the next step.

# Installing the epel-release package using the yum command yum install epel-release
yum install epel-release

The yum command installs and manages software by using the official Red Hat software repositories, as well as other third-party repositories.

Installing the epel-release package using the yum command
Installing the epel-release package using the yum command

3. Once the repository is complete, install the Ansible package.

sudo yum install ansible
 Installation of Ansible package
Installation of Ansible package

4. Now, confirm Ansible is installed by running ansible --version.

Checking the Ansible Version
Checking the Ansible Version

Installing Ansible on macOS

So far, you have learned how to set up Ansible on various Linux distros. Let’s finish out this tutorial by learning how to install Ansible on a Mac.

This section of the tutorial will focus on the popular macOS package manager called Homebrew. Homebrew is the easiest way to install Ansible on macOS. This tutorial will be using Homebrew v3.0.7 on macOS Catalina.

If you’d like to learn how to install Ansible via the popular Python package manager pip, check out the Ansible pip package.

To install on macOS:

  1. Open up the Apple Terminal.
  2. Run the following command to invoke Homebrew to download and install Ansible on your Mac.
brew install ansible
  1. Once complete, verify Ansible is installed by running ansible --version.
$ ansible --version
ansible 2.7.5
  config file = None
  configured module search path = ['/Users/shanky/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/Cellar/ansible/2.7.5/libexec/lib/python3.7/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.7.2 (default, April 13 2021, 07:35:52) [Clang 10.0.0 (clang-1000.11.45.5)]

Testing Out Ansible by Running Ad-Hoc Commands

Now that Ansible is installed, it’s not doing much good unless you actually use it! One of the easiest ways to get started with Ansible is to learn about ad hoc commands. Ad hoc commands are one-off usually single-line commands that the Ansible invokes on remote computers (or on the Ansible host in this example).

Run an ad-hoc command on your operating system of choice by using the -m switch. The -m switch specifies an Ansible module that contains various functionality.

One of the simplest Ansible modules is the ping module. This module tests connectivity between the Ansible controller and a host. If you don’t have any remote hosts available, provide the name of the Ansible controller itself (localhost) to run the ping module against.

ansible localhost  -m ping

If successful, you should see green output indicating success!

Running the ansible ad hoc ping module
Running the ansible ad hoc ping module

Conclusion

In this tutorial, you learned how to set up the most widely used automation tool, Ansible, on Ubuntu, RHEL, CentOS, and macOS.

Now that you have a new Ansible controller host set up, what do you plan to manage with it?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!