Red Hat Ansible Automation Platform is an enterprise-grade automation platform. Ansible was open-source automation tool, which was acquired by Red Hat in 2015 and still we can use Ansible core as free of charge to use, which doesn’t provide rich features like RedHat Automation platform offers.
There are several options available for IT Automation, RedHat Ansible is known for simplicity and agent less architecture and great community support.
Let’s remove complexity and increase productivity and eliminate boring repetitive tasks.
It uses YAML language, which is super easy to read and learn, self-documenting, and doesn’t require a grad-level computer science degree to understand, look below sample,
Step 1: Create an Ansible Inventory
First, create an inventory file that lists your appservers and dbserver
---
all:
hosts:
Appserver1:
ansible_host: 10.10.0.2
ansible_user: admin
ansible_ssh_private_key_file: /path/to/private_key.pem
webserver_role: frontend
Appserver2:
ansible_host: 10.10.0.3
ansible_user: admin
ansible_ssh_private_key_file: /path/to/private_key.pem
webserver_role: frontend
dbserver1:
ansible_host: 10.20.0.2
ansible_user: dbadmin
ansible_password: dbadmin_password
dbserver_role: backend
children:
Appservers:
hosts:
appserver1:
appserver2:
databases:
hosts:
dbserver1:
In this example:
The hosts are defined under the all group. Each host is identified by a unique name, such as Appserver1, Appserver2, and dbserver1.
For each host, you specify the ansible_host with the corresponding IP address or hostname.
The ansible_user specifies the remote username for SSH authentication (in this case, admin for web servers and dbadmin for the database server).
The ansible_ssh_private_key_file is the path to the private key file used for SSH authentication (for web servers).
For the database server, ansible_password is used instead, indicating a password-based authentication method.
The webserver_role and dbserver_role variables are custom variables assigned to each host, representing their respective roles.
The webservers and databases groups are created under the children section. Hosts are assigned to groups using the hosts attribute.
This is a basic example, and you can extend the inventory with additional host variables, groups, and group variables to suit your infrastructure’s needs. The inventory file allows Ansible to manage and organize hosts efficiently during playbook execution.
Step 2: Write the Ansible Playbook
Next, create a YAML playbook named app-db-updates.yaml
with the following content:
---
- name: Update Appservers
hosts: Appservers
become: yes
tasks:
- name: Update packages
yum:
name: '*'
state: latest
In this playbook:
name
: Your desired name for the playbook.hosts
: The group nameAppservers
refers to the group of hosts defined in the inventory file.become: yes
: This enables privilege escalation (sudo) to perform the updates.
The main task is defined under tasks
, where the yum
module is used with the following parameters:
name: '*'
: This tells Ansible to update all packages (*
means all packages).state: latest
: This indicates that Ansible should ensure that the latest version of each package is installed.
Once you create the playbook, create a new job template in RedHat Ansible Automation platform portal.
Step 3: Finally Run the Ansible Playbook
This is a test example, and you can extend the inventory with additional host variables, groups, and group variables to suit your infrastructure’s needs. The inventory file allows Ansible to manage and organize hosts efficiently during playbook execution.
You can create many job templates based on your need of Automation task. Additionally, it’s always a good practice to test playbooks in a non-production environment before applying them to production systems.