Skip to content
English - United States
  • There are no suggestions because the search field is empty.

Getting Started with Red Hat Ansible: A Step-by-Step Guide

This article provides a comprehensive guide to installing and implementing Red Hat Ansible, a powerful automation tool for IT tasks. Whether you're managing cloud infrastructure or on-premises servers, Ansible simplifies complex operations and ensures consistency across your environment.

 

Before You Begin:

  • Understand Ansible Concepts: Familiarize yourself with basic Ansible terminology like playbooks, inventories, modules, and roles. A foundational understanding will make the installation and configuration process smoother.
  • Choose Your Installation Method: Ansible can be installed using package managers (like apt or yum) or pip (the Python package installer). This guide will cover common methods.
  • Identify Your Target Environment: Determine whether you're working with Linux, macOS, or Windows targets, as the installation and configuration steps may vary slightly. Ansible control nodes (where you run Ansible from) are typically Linux-based.
  • Gather Necessary Credentials: You'll need appropriate credentials (SSH keys or passwords) to access the target machines you want to manage with Ansible.

Step 1: Installing Ansible

On Linux (e.g., Ubuntu/Debian):


Bash



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

On Linux (e.g., RHEL/CentOS):


Bash



sudo yum install epel-release -y  # For older RHEL/CentOS versions
sudo dnf module install ansible:stable -y # For newer RHEL/CentOS
# OR
sudo yum install ansible -y # For older RHEL/CentOS

Using pip (Generally applicable, but package manager installation is preferred):


Bash



python3 -m venv ansible_venv  # Create a virtual environment (recommended)
source ansible_venv/bin/activate
pip install ansible-core  # Install the core Ansible engine

On macOS:


Bash



brew install ansible
# OR using pip within a virtual environment (recommended)
python3 -m venv ansible_venv
source ansible_venv/bin/activate
pip install ansible-core

Step 2: Verifying the Installation

After installation, verify that Ansible is installed correctly by checking its version:


Bash



ansible --version

This command should display the installed Ansible version and other relevant information.

Step 3: Setting Up Your Inventory

An inventory file lists the target machines that Ansible will manage. Create a file named inventory (or any name you choose) in your project directory. The inventory file can be simple or complex, depending on your needs.

Example Inventory (Simple):


Ini, TOML



[webservers]
server1 ansible_host=192.168.1.10 user=myuser ansible_ssh_private_key_file=~/.ssh/mykey.pem
server2 ansible_host=192.168.1.20 user=myuser ansible_ssh_private_key_file=~/.ssh/mykey.pem

[database_servers]
db1 ansible_host=192.168.1.30 user=myuser ansible_ssh_private_key_file=~/.ssh/mykey.pem

Example Inventory (Grouped with Variables):


Ini, TOML



[webservers]
server1 ansible_host=192.168.1.10 user=myuser
server2 ansible_host=192.168.1.20 user=myuser

[database_servers]
db1 ansible_host=192.168.1.30 user=myuser

[webservers:vars]
http_port=80

[database_servers:vars]
db_port=3306

Step 4: Running Your First Ansible Command

Let's test your setup by running a simple ping command against your target servers:


Bash



ansible -i inventory webservers -m ping

This command will use the ping module to check the connectivity to the servers in the webservers group defined in your inventory.

Step 5: Creating Your First Playbook

Playbooks are YAML files that define the tasks Ansible will execute. Create a file named playbook.yml (or any name you choose) in your project directory.

Example Playbook (playbook.yml):


YAML



---
- hosts: webservers
  become: true  # Run tasks with elevated privileges
  tasks:
    - name: Install Apache
      apt:
        name: apache2
        state: present
    - name: Start Apache
      service:
        name: apache2
        state: started
        enabled: yes

Step 6: Running Your Playbook

Execute your playbook using the following command:


Bash



ansible-playbook -i inventory playbook.yml

This command will run the tasks defined in your playbook against the specified hosts in your inventory.

Troubleshooting Tips:

  • Connection Errors: Check your SSH keys, passwords, and network connectivity to the target machines.
  • Inventory Issues: Verify the accuracy of your inventory file, including hostnames, IP addresses, and variables.
  • Playbook Errors: Carefully review your playbook syntax and ensure that the modules and tasks are defined correctly.

Further Resources:

This guide provides a basic introduction to installing and using Ansible. As you become more comfortable, you can explore more advanced features like roles, variables, and complex playbooks to automate even the most intricate IT tasks. Remember to consult the official Ansible documentation for more in-depth information and specific use cases.

Sources

  1. https://www.techtarget.com/searchitoperations/tutorial/Automate-IT-operations-with-VMware-and-Ansible
  2. https://www.devopstricks.in/installing-apache-web-server-on-ubuntu-22-04-with-ansible/