Skip to main content

Quick start guide to Ansible for Linux sysadmins

In this second article, you'll explore the how-to of Ansible installation.
An advanced look at demystifying Ansible for Linux sysadmins

In this article, I discuss the prerequisites for installing Ansible, Ansible installation using the Satellite server, setting up Ansible for automation, introduction to playbooks, inventory files, and go through a use case for Ansible. Check out the first article, Demystifying Ansible for Linux sysadmins, if you have not already.

Prerequisites for installing Ansible

To use Ansible in your environment, you must set up a management node (also called a control node) where you will install Ansible. You can install Ansible on any machine with Python 2 (version 2.7) or Python 3 (versions 3.5 and higher). With RHEL 7, we have Python 2.7 installed by default, and with RHEL 8, we have Python 3.6 by default. To use all Ansible modules, you need to install Python on the managed nodes as well. By default, Ansible uses SSH to communicate with managed nodes. Managed nodes can also use SFTP or SCP for communication, and this can be modified in the Ansible configuration file (ansible.cfg).

If you are using RHEL Satellite to install Ansible on an RHEL 8 system, make sure you add the repositories for Red Hat Ansible Engine 2.9 for RHEL 8 x86_64 (RPMs) from the Satellite GUI and sync the repositories from the CDN to the Satellite. On the control node, you must register with the Satellite server, enable the Satellite tools, and enable the Ansible repository by using the following commands:

# subscription-manager repos --enable satellite-tools-6.6-for-rhel-8-x86_64-rpms
Repository' satellite-tools-6.6-for-rhel-8-x86_64-rpms' is enabled for this system.

# subscription-manager repos --enable ansible-2.9-for-rhel-8-x86_64-rpms
Repository' ansible-2.9-for-rhel-8-x86_64-rpms' is enabled for this system.

Installing Ansible on RHEL

To install Ansible on the RHEL 8 control node after registering with the Satellite server, run the command yum install ansible. This command installs six packages:

  1. Install Ansible from the Ansible repo:
    1. Ansible
  2. Install dependencies:
    1. sshpass
    2. Python3-markupsafe
    3. Python3-jinja2
    4. Python3-babel
  3. Install weak dependency:
    1. Python3-jmespath

After the installation, run the ansible --version command to check the version of Ansible installed.

# ansible --version

ansible 2.9.6

 config file = /etc/ansible/ansible.cfg

 configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']

 ansible python module location = /usr/local/lib/python3.6/site-packages/ansible-2.9.6-py3.6.egg/ansible

 executable location = /usr/local/bin/ansible

 python version = 3.6.8 (default, Oct 11 2019, 15:04:54) [GCC 8.3.1 20190507 (Red Hat 8.3.1-4)]

After the installation, you can make changes to the default Ansible settings by modifying parameters in the ansible.cfg file.

[ You might also enjoy reading: Renewing my thrill at work with Ansible ]

Setting up Ansible for automation

Now that Ansible is installed, you must configure SSH key-based authentication (which uses asymmetric cryptography) from the control node to the managed nodes for Ansible to work seamlessly. You will need to generate a key pair on your control node and transfer the public key to the managed nodes. Use the command ssh-keygen to generate the key pair. The keys are stored in your home directory inside the .ssh folder. The default keys are named (public key) and id_rsa (private key). To enable SSH key-based authentication on the managed nodes, run the following command on your control node:

# ssh-copy-id <target user>@<IP address/Hostname of the managed node>

Note that this will require a one-time password for the target user on the managed node. The command ssh-copy-id will copy the control node's public key to the authorized_keys file on the managed nodes.

Once configured, you can add the remote nodes to an inventory file and perform the ping test using the following Ansible ad-hoc command:

# ansible all -m ping -i <inventory-file>

More about playbooks

Playbooks are the files where Ansible functions are written. The playbooks are written in YAML format. YAML stands for Yet Another Markup Language. Playbooks are one of the core features of Ansible and tell Ansible what to execute. They are like a to-do list for Ansible that contains a series of tasks. Playbooks are the building blocks for all the use cases of Ansible. You can create independent modular Ansible playbooks for each action. You can incorporate these modular playbooks into a single master playbook using the module import_playbook. This ensures the reusability of the playbooks. You can use variables in the playbook and pass these variables from the Ansible inventory, included files, and during runtime. Some of the key features of an Ansible playbook include:

  • Ability to execute tasks as different users based on the become method
  • Control where to execute each task based on the hosts in your inventory
  • Specify conditionals and loops in your code


Ansible playbook displaying sections
An example Ansible playbook

More about inventory

Inventory is the list of nodes that can be managed by Ansible. You can specify individual managed hosts or create a group of hosts in the inventory file. For each managed host, you can assign variables that can be used by the playbook while executing tasks on that particular host. The default inventory file is /etc/ansible/hosts, but it is recommended that you create your own inventory file and call it during the playbook's execution using the -i command option. Inventory files can be in INI or YAML format. You can either maintain a static inventory file or a dynamic list of hosts.


# cat hosts-b2b

b2bwebprod1 urlpath=



Ansible use case

Let's explore an Ansible use case for system monitoring. We can create playbooks for various sysadmin-related monitoring tasks, including:

  • Filesystem usage monitoring and email an alert if disk space is more than 90% utilized
  • Application and port availability
  • Database availability by connecting and querying the database
  • URL availability by using the uri module and email alerts if the return code is greater than 200. You can also use retries and delay features in the uri module to be doubly sure
  • Service availability, report if service is down, and attempt to restart it
  • Check component versions to keep an up-to-date inventory
  • Monitor system uptime and send email alert if a system is not accessible

[ Need more on Ansible? Take a free technical overview course from Red Hat. Ansible Essentials: Simplicity in Automation Technical Overview. ] 

Wrap up

This article gives you an overview of Ansible installation, setting up Ansible, creating playbooks, and managing inventory with examples. Stay tuned for more articles on automation using Ansible.

Check out these related articles on Enable Sysadmin

Ansible Galaxy is a repository for Ansible Roles that are available to drop directly into your Playbooks to streamline your automation projects.
Introduction to Ansible Tower
Not sure whether you need Red Hat Ansible or Red Hat Ansible Tower? Read on, and learn how to get started with Ansible Tower as well.
Topics:   Linux   Linux Administration   Ansible  
Author’s photo

Pratheek Prabhakaran

Pratheek is a Sysadmin at a Fortune 500 company in India where he specializes in IT automation with Ansible. He has experience in SAP Basis, RHEL Satellite, AWS Cloud, and VMware Administration. An avid reader, cricketer, and artist. More about me

On Demand: Red Hat Summit 2021 Virtual Experience

Relive our April event with demos, keynotes, and technical sessions from
experts, and sign up to attend breakout sessions June 15–16.

Related Content