🌟 ANSIBLE ROLES 🌟

Sourav Dinda
4 min readMay 20, 2021

🔰What is Ansible?

Ansible is a configuration management platform that automates storage, servers, and networking. When you use Ansible to configure these components, difficult manual tasks become repeatable and less vulnerable to error. Ansible simplifies IT automation by capturing an array of IT resources and supporting multitier deployments from day 1. Ansible consolidates resources across multiple systems to manage them from a single platform rather than requiring management from one system at a time .

🔰 What is Ansible Roles ?

Roles provide a framework for fully independent, or interdependent collections of variables, tasks, files, templates, and modules.

In Ansible, the role is the primary mechanism for breaking a playbook into multiple files. This simplifies writing complex playbooks, and it makes them easier to reuse. The breaking of playbook allows you to logically break the playbook into reusable components.

Each role is basically limited to a particular functionality or desired output, with all the necessary steps to provide that result either within that role itself or in other roles listed as dependencies.

Roles are not playbooks. Roles are small functionality which can be independently used but have to be used within playbooks. There is no way to directly execute a role. Roles have no explicit setting for which host the role will apply to.

Top-level playbooks are the bridge holding the hosts from your inventory file to roles that should be applied to those hosts.

🔰 So Let’s begin….

Now I have only one running instances .

I am going to launch one instances for load balancer i.e. haproxy and 3 instances for webserver . To make it simple , I have launched manually . But if you are expert of dovops , you can automate these steps as I did for configuration automatically . for better understanding to automate everything , you may follow my one blog named “ provisioning ec2 instances …… ” . the link is attached below .

……………………………………………………

♦️ Launching instances manually ….

♦️ Inventory File :

I have updated public ip manually in my inventory file .

♦️ Ansible Configuration File :

🔅 Create an ansible role “webserver” and “ mylb ”to configure webServer and load balancer respectively.

# ansible-galaxy role init webserver

# ansible-galaxy role init mylb

♦️ Here, you can see two roles created :

♦️ In mylb role,

In tasks/main.yml file

In vars/main.yml file

In templates/haproxy.cfg file

♦️ In webserver role,

In tasks/main.yml file

In vars/main.yml file

In templates/my.php file

🔅 We need to combine both of these roles controlling webserver versions and solving challenge for host ip’s addition dynamically over each Managed Node in HAProxy.cfg file.

♦️ In main playbook “setup.yml”

Before running playbook , It’s always good practice to ping all managed node to check connectivity .

Before running playbook

♦️ Let’s run main playbook

Hurrah…playbook is working great . Let’s check through browser .

Load balancer i.e. Haproxy is working properly.

🔰 Thanks for Reading !! 🔰

🔰 Keep Learning !! Keep Sharing !! 🔰

--

--

Sourav Dinda
Sourav Dinda

No responses yet