...

Role In Ansible Demystified: A Simple and Master Guide in 4 Easy Steps

Rate this post

What is a role in ansible?

Role in Ansible serves as organized units encapsulating related tasks, handlers, variables, files, templates, default values, dependencies (defined in the meta directory), and test cases. A role comprises sub-folders within the designated role folder, each containing a main.yml file to encapsulate its specific content. This structuring simplifies the management of intricate task sets, similar to how objects in programming encapsulate functionalities for reuse.

Usecase for a role in Ansible

Roles significantly enhance playbook readability and maintainability by providing a modular and reusable structure. Assembling related functionalities into a single role, it promotes cleaner code, easier debugging, and facilitates code reuse across multiple projects. Just as functions in programming abstract complex logic, roles encapsulate Ansible configurations, allowing for streamlined execution across various environments. This modular approach fosters better organization, readability, and efficiency within Ansible playbooks, contributing to smoother automation workflows.

Role in Ansible

Prerequisite

Install Ansible

It will install ansible-playbook, ansible-galaxy, and other ansible commands as well.
On Mac, brew update; brew install ansible.For other platforms refer to https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html

Learn more about ansible setup

Create base setup

Let us understand this by creating a role and understanding its folder structure. We will first set up the root directory.

create ansible dir

mkdir ~/ansible

go inside this directory

cd ansible

Create playbooks and roles folder

mkdir playbooks roles

Create inventory.yml with the below contents

# inventory.yml
---
all:
  children:
    webservers:
      hosts:
        webserver-01:

Now, let us create a role named role1 by executing

go inside roles directory

cd roles

create a role role1

ansible-galaxy role init role1

A new role created using the above command has the below directory structure

role1
├── README.md
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

create role in ansible

Now we can add our contents in main.yml or create new files as per our requirements. We should also delete unused folders and files.

Let us learn it further by automating sudo apt update command

Add below task in roles/role1/tasks/main.yml

- name: Update apt cache
  apt:
    update_cache: yes

Create a sample webservers playbook (webservers.yml)

cd playbooks
touch webservers.yml

and add the below contents

—
# webservers.yml

- name: Setup webserver
   hosts: webservers
   become: yes
   roles:
      - ../roles/role1

Execute webservers.yml as below
cd playbooks && ansible-playbook webservers.yml -i inventory.yml

Reusing roles in another role or task

We can reuse a role in a task file or within a task file of a given role by using include_role.

Let us understand this by setting up a git repo on our target servers.

Setup git role

First create a git role

cd roles && ansible-galaxy create role git

Add below contents in roles/git/tasks.yml

- name: Install git
  apt:
    name:
      - git
    state: latest
    update_cache: yes

- name: Clone repo - {{repo}}
  git:
    repo: '{{repo}}'
    dest: '{{dest}}'
    version: '{{version}}'
    force: yes
  become_user: '{{user}}'

Setup up service role

Create a role named service

cd roles && ansible-galaxy create role service

Add below contents in roles/service/tasks/main.yml

- name: Setup Repo - repo1
  include_role:
    name: ../roles/git
  vars:
    dest: 'repo1'
    version: 'release'
    repo: repo1

- name: Setup Repo - repo2
  include_role:
    name: ../roles/git
  vars:
    dest: 'repo2'
    version: 'release'
    repo: repo2

Execute webservers.yml as below
cd playbooks && ansible-playbook webservers.yml -i inventory.yml

Benefits of using roles in Ansible

  • Simplifies complex playbook and task organization.
  • Increases modularity
  • Task reuse by using include_role in Ansible
  • Readability

Conclusion

Role in Ansible is a pivotal organizational tool, streamlining playbook management by encapsulating related tasks, handlers, variables, and dependencies into modular units. They emulate the concept of objects in programming by bundling functionalities for reuse and clarity. The hierarchical structure they establish simplifies the orchestration of complex tasks, promoting a more systematic and maintainable approach to infrastructure automation.

Role in Ansible offers a multitude of benefits, such as enhancing readability, promoting reusability, and facilitating collaboration among team members. By compartmentalizing tasks into distinct roles, they foster code modularity and scalability, enabling easier maintenance and debugging. Moreover, roles encourage best practices by enforcing a standardized layout for playbooks, promoting consistency across projects and teams.

Ultimately, leveraging roles in Ansible empowers DevOps teams to efficiently manage configurations, enforce consistent deployments, and foster a more organized and scalable automation framework, leading to smoother and more reliable infrastructure management.

Reference: https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_reuse_roles.html

Spread the love

4 thoughts on “Role In Ansible Demystified: A Simple and Master Guide in 4 Easy Steps”

Leave a Comment

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.