Install Ansible on Red Hat Enterprise Linux

Red Hat Enterprise LinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to install Ansible Core on a Red Hat Enterprise Linux (RHEL) system. Ansible is a powerful automation tool that allows you to manage and configure systems, deploy applications, and orchestrate complex IT workflows.

You will use the dnf package manager with sudo privileges to install the ansible-core package, which provides the core Ansible engine and command-line tools. After installation, you will verify that Ansible is working correctly by checking its version and running basic commands.

This is a fundamental skill for system administrators and DevOps engineers working with Red Hat Enterprise Linux systems.

Install Ansible Core Using dnf

In this step, you will install the ansible-core package using the dnf package manager. Ansible Core provides the essential Ansible engine, including the ansible, ansible-playbook, and other core command-line tools needed for automation tasks.

The dnf (Dandified YUM) package manager is the standard tool for managing software packages on Red Hat Enterprise Linux. Since installing software requires administrative privileges, you must use the sudo command.

Run the following command to install Ansible Core with automatic confirmation:

sudo dnf install ansible-core -y

The -y flag automatically answers "yes" to all prompts, making the installation non-interactive. The system will download and install ansible-core along with its Python dependencies including Jinja2 for templating and PyYAML for YAML processing.

You should see output similar to this, showing the package resolution and installation progress:

Updating Subscription Management repositories.
Last metadata expiration check: ...
Dependencies resolved.
================================================================================
 Package                  Arch   Version                Repository         Size
================================================================================
Installing:
 ansible-core             noarch 2.16.x-x.el9            rhel-9-appstream   xx M
Installing dependencies:
 python3-jinja2           noarch x.x.x-x.el9              rhel-9-appstream   xxx k
 python3-yaml             x86_64 x.x.x-x.el9              rhel-9-appstream   xxx k
 ...

Transaction Summary
================================================================================
Install  XX Packages

Complete!

Verify the Ansible Installation

Now that you have installed Ansible Core, let's verify that the installation was successful by checking the version and confirming that the essential command-line tools are available.

First, check the Ansible version by running:

ansible --version

This command displays detailed information about your Ansible installation, including the core version, Python version, and the locations of various components. You should see output like this:

ansible [core 2.14.18]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/labex/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.9/site-packages/ansible
  ansible collection location = /home/labex/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.9.21 (main, Feb 10 2025, 00:00:00) [GCC 11.5.0 20240719 (Red Hat 11.5.0-5)] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True

Let's understand what each line means:

  • ansible [core 2.14.18]: Shows the installed Ansible Core version
  • config file: Points to the main Ansible configuration file that contains default settings
  • configured module search path: Directories where Ansible looks for custom modules
  • ansible python module location: Where the core Ansible Python code is installed
  • ansible collection location: Directories where Ansible collections (packaged modules and plugins) are stored
  • executable location: The actual location of the ansible command binary
  • python version: The Python interpreter version that Ansible uses
  • jinja version: The templating engine version used by Ansible for dynamic content
  • libyaml = True: Confirms that the fast YAML parser is available for better performance

This confirms that Ansible is properly installed and ready to use. Next, let's also check that the ansible-playbook command is available:

ansible-playbook --version

You should see similar version information for the ansible-playbook tool, which is essential for running Ansible playbooks.

Test Ansible with a Simple Command

In this step, you will test your Ansible installation by running a simple command against the local system. Change to the project directory and use the pre-configured inventory file to run an Ansible ad-hoc command.

Navigate to the project directory and test the basic ping functionality:

cd /home/labex/project
ansible localhost -m ping

The ping module doesn't actually send ICMP packets; instead, it verifies that Ansible can connect to the target and execute Python code. A successful response will look like this:

localhost | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

Let's break down this output:

  • localhost | SUCCESS: Shows that the command executed successfully on the localhost target
  • ansible_facts: Contains system information discovered during execution
  • discovered_interpreter_python: The Python interpreter path that Ansible found and will use
  • changed: false: Indicates that no changes were made to the system (ping is read-only)
  • ping: "pong": The classic response confirming Ansible connectivity

The "pong" response confirms that Ansible is working correctly and can communicate with the target system.

Let's also test gathering system information using the setup module:

ansible localhost -m setup -a "filter=ansible_distribution*"

This command uses the setup module to gather system facts, specifically filtering for distribution information. You should see output containing details about your Red Hat Enterprise Linux system:

localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_distribution": "RedHat",
        "ansible_distribution_file_parsed": true,
        "ansible_distribution_file_path": "/etc/redhat-release",
        "ansible_distribution_file_search_string": "Red Hat",
        "ansible_distribution_file_variety": "RedHat",
        "ansible_distribution_major_version": "9",
        "ansible_distribution_release": "Plow",
        "ansible_distribution_version": "9.6",
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false
}

Understanding the system facts output:

  • ansible_distribution: The Linux distribution name (RedHat)
  • ansible_distribution_file_parsed: Whether Ansible successfully read the distribution file
  • ansible_distribution_file_path: The file that contains distribution information
  • ansible_distribution_file_search_string: The text pattern used to identify the distribution
  • ansible_distribution_file_variety: The distribution family (RedHat family)
  • ansible_distribution_major_version: The major version number (9)
  • ansible_distribution_release: The release codename (Plow)
  • ansible_distribution_version: The complete version number (9.6)
  • discovered_interpreter_python: Python interpreter discovered by Ansible

This confirms that Ansible can successfully collect system information from the target host, which is essential for creating conditional automation based on system characteristics.

Explore Available Ansible Modules

Ansible comes with hundreds of built-in modules for various automation tasks. Let's explore some of the available modules to understand what capabilities are immediately available after installation.

To see a list of available modules, run:

ansible-doc -l | head -20

The ansible-doc -l command lists all available modules, and using head -20 shows the first 20 modules. This gives you an idea of the extensive automation capabilities available with Ansible. You'll see output similar to:

ansible.builtin.add_host               Add a host (and alternatively a grou...
ansible.builtin.apt                    Manages apt-packages
ansible.builtin.apt_key                Add or remove an apt key
ansible.builtin.apt_repository         Add and remove APT repositories
ansible.builtin.assemble               Assemble configuration files from fr...
ansible.builtin.assert                 Asserts given expressions are true
ansible.builtin.async_status           Obtain status of asynchronous task
ansible.builtin.blockinfile            Insert/update/remove a text block su...
ansible.builtin.command                Execute commands on targets
ansible.builtin.copy                   Copy files to remote locations
ansible.builtin.cron                   Manage cron.d and crontab entries
ansible.builtin.debconf                Configure a .deb package
ansible.builtin.debug                  Print statements during execution
ansible.builtin.dnf                    Manages packages with the `dnf' pack...
ansible.builtin.dpkg_selections        Dpkg package selection selections
ansible.builtin.expect                 Executes a command and responds to p...
ansible.builtin.fail                   Fail with custom message
ansible.builtin.fetch                  Fetch files from remote nodes
ansible.builtin.file                   Manage files and file properties
ansible.builtin.find                   Return a list of files based on spec...

Understanding the module list format:

  • ansible.builtin.: Indicates these are built-in modules that come with Ansible Core
  • Module name: The name you use when calling the module in playbooks or ad-hoc commands
  • Description: A brief explanation of what the module does

Some important modules you'll commonly use:

  • command: Execute shell commands on target systems
  • copy: Copy files from your control machine to remote hosts
  • dnf: Install, update, or remove packages on Red Hat systems
  • file: Create directories, set permissions, or manage file properties
  • debug: Print messages during playbook execution for troubleshooting

To get detailed documentation for a specific module, you can use the ansible-doc command with the module name. For example, to learn about the copy module:

ansible-doc copy

This will display comprehensive documentation for the copy module, including examples and parameter descriptions. The ansible-doc command provides detailed documentation for any Ansible module, making it easy to learn how to use different automation capabilities. Press q to exit the documentation viewer when you're done reading.

Summary

In this lab, you successfully learned how to install and verify Ansible Core on a Red Hat Enterprise Linux system. Here's what you accomplished:

  1. Installed Ansible Core: You used the sudo dnf install ansible-core -y command to install the core Ansible package and its dependencies from the official Red Hat repositories.

  2. Verified the Installation: You confirmed that Ansible was properly installed by checking the version of both ansible and ansible-playbook commands.

  3. Tested Basic Functionality: You ran simple Ansible commands to verify that the installation works correctly, including:

    • Using the ping module to test connectivity
    • Using the setup module to gather system facts
  4. Explored Available Modules: You learned how to discover and read documentation for the extensive library of Ansible modules using ansible-doc.

You now have a fully functional Ansible installation on RHEL and understand the basic commands needed to start automating your infrastructure. This foundation prepares you for more advanced Ansible topics such as writing playbooks, managing inventories, and implementing complex automation workflows.