• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

UnixArena

  • Home
  • kubernetes
  • DevOps
    • Terraform
    • Jenkins
    • Docker
    • Openshift
      • OKD
    • Ansible engine
    • Ansible Tower
      • AWX
    • Puppet
  • Cloud
    • Azure
    • AWS
    • Openstack
    • Docker
  • VMware
    • vCloud Director
    • VMware-Guests
    • Vcenter Appliance 5.5
    • vC OPS
    • VMware SDDC
    • VMware vSphere 5.x
      • vSphere Network
      • vSphere DS
      • vShield Suite
    • VMware vSphere 6.0
    • VSAN
    • VMware Free Tools
  • Backup
    • Vembu BDR
    • Veeam
    • Nakivo
    • Azure Backup
    • Altaro VMBackup
    • Spinbackup
  • Tutorials
    • Openstack Tutorial
    • Openstack Beginner’s Guide
    • VXVM-Training
    • ZFS-Tutorials
    • NetApp cDot
    • LVM
    • Cisco UCS
    • LDOM
    • Oracle VM for x86
  • Linux
    • How to Articles
    • Q&A
    • Networking
    • RHEL7
  • DevOps Instructor-led Training
  • Contact

Ansible – What is Roles ? Playbook vs Role

October 22, 2018 By Cloud_Devops 2 Comments

Playbook is often used to complete simple tasks. When you would like to perform the multiple tasks, restarting services and copying files for a single job, you must consider using “roles” instead of writing the lengthy playbooks. Roles uses the known file structure to keep the various elements in different directories. For an example, handlers(restarting service) will be kept in handlers directory. Roles often simplify the code since it’s breaking into small pieces.

 

Here are the few Rules to use Role: 

  • Group the tasks in a single role.
  • Each role has a single responsibility.
  • The role requires the directory structure.

 

Role Structure : 

Ansible - Roles - Structure

Ansible – Roles – Structure

Creating a New Role: 

1. Identify the default role path for the ansible environment from a configuration file.

[linadm@ansible-server roles]$ grep roles ansible.cfg
roles_path    = /home/linadm/automation/roles:/usr/share/ansible/roles
[linadm@ansible-server roles]$

 

2. Navigate to the roles directory and create the new role to install VMware Tools on RHEL Linux VM.

[linadm@ansible-server roles]$ ansible-galaxy init Install_VMware_tools_RHEL
- Install_VMware_tools_RHEL was created successfully
[linadm@ansible-server roles]$ ls -ld Install_VMware_tools_RHEL
drwxrwxr-x 2 linadm linadm 6 Aug  3 17:14 Install_VMware_tools_RHEL
[linadm@ansible-server roles]$

 

3. Create the directory for the new role. Navigate to the newly created role directory.

[linadm@ansible-server Install_VMware_tools_RHEL]$ ls -lrt
total 4
-rw-r--r-- 1 root root 1328 Aug  9 18:54 README.md
drwxr-xr-x 2 root root    6 Aug  9 18:54 templates
drwxr-xr-x 2 root root    6 Aug  9 18:54 files
drwxr-xr-x 2 root root   22 Aug  9 18:54 defaults
drwxr-xr-x 2 root root   22 Aug  9 18:54 handlers
drwxr-xr-x 2 root root   22 Aug  9 18:54 meta
drwxr-xr-x 2 root root   22 Aug  9 18:54 tasks
drwxr-xr-x 2 root root   39 Aug  9 18:54 tests
drwxr-xr-x 2 root root   22 Aug  9 18:54 vars
[linadm@ansible-server Install_VMware_tools_RHEL]$

Note: This article intention is to just explain the functionality of the ansible role.

 

4. Here, I have already created the required files under each directory.

[linadm@ansible-server Install_VMware_tools_RHEL]$ tree
.
├── default
│   └── main.yml
├── files
│   ├── kmod-vmware-tools-vmxnet3-1.4.rpm
│   ├── kmod-vmware-tools-vsock-9.8.1.rpm
│   └── vmware-tools-core-10.3.2-1.el5.rpm
├── handlers
│   └── restart_vmware_tools.yml
├── tasks
│   └── RedHat.yml
└── tests
    └── Redhat.yml

5 directories, 7 files
[linadm@ansible-server Install_VMware_tools_RHEL]$

  • All the required files will go in the “files” directory
  • All the tasks will go in to “tasks“
  • Post restart tasks will go into “handlers” directory
  • The “default” directory’s  main.yml will consist of the pre-validation and calling the tasks.

 

Conclusion: 

In Playbook, you will be combining all the elements into a single file. But roles split the code in the respective directory and simplifies the code.

 

Filed Under: Ansible engine, Automation, Configuration Management, DevOps Tagged With: Ansible

Reader Interactions

Comments

  1. Vikrant says

    November 15, 2018 at 12:50 pm

    May be following command can be helpful to create the ansible role directory structure in single shot.

    # ansible-galaxy init

    Reply
    • Lingeswaran R says

      November 18, 2018 at 9:11 pm

      Yes. ansible-galaxy is the easiest way to do.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Follow UnixArena

  • Facebook
  • LinkedIn
  • Twitter

Copyright © 2025 · UnixArena ·

Go to mobile version