DevOps Classroom Series – 21/Aug/2020

Reusability in Ansible

  • On a Broader note in Ansible we have 3 major ways

    1. Including and importing
    2. Roles
    3. Collections
  • Includes vs Imports:

    • They are very similar statements to reuse ansible playbooks but the way ansible handles it is different
      • All import* statements are pre-processed at the time playbook is parsed
      • All include* statements are processed as they are encountered during execution
  • Roles:

    • Roles have certain directory structure and they expect files in certain directory names
    • Roles written by community can be searched in Ansible Galaxy

Lets use a role to install mysql

  • Role which will be used is this
  • Now login into Ansible control Node and execute the following command in new directory
ansible-galaxy install geerlingguy.mysql


  • Now go through the documentation and understand how to use the role and the variables Preview
  • Now lets create an inventory file
  • Now lets write a playbook which uses the role downloaded
- hosts: all
    - name: geerlingguy.mysql
      become: yes
    - name: print role execution completed
        msg: "mysql role has been executed"

  • Now if we execute this ansible will try to install mysql. But how is this stuff working
  • To understand that lets write our own role

Ansible role for the tomcat

  • Lets create a role in ansible control node

  • The default folder structure of role is as shown below Preview

  • Now lets understand the importance of folders in roles

    • tasks: All the tasks of your playbook will be placed in this folder. The default file is main.yaml
    • Handlers: All the handlers will have a common location
  • Command for creating role is ansible-galaxy init <role-name>

  • Once you have created the roles, ansible has some folders where it checks for roles

    1. IN the same folder where playbook.yaml is present, it expects a roles folder and inside roles folder it searches for role names
    1. If it doesn’t find it there it navigates to $(home directory)/.ansible/roles/<role-name>
    1. If it doesn’t find here then it searches in /etc/ansible/roles/<role-name>
  • The role developed in class is shared @ here

  • Even today lots of organization are still using roles.

Ansible Collections

  • Collections are distribution format for Ansible that contains
    • playbooks
    • roles
    • modules
    • plugins
  • You can install and use collections from ansible-galaxy
  • We will be discussing about this in our next session


  • In programming world when we have code which can get execptions
    # some lines of code
catch (Exception)
    # handling code
    # cleanup or must call code block
  • Ansible blocks allow for logical grouping of task in a play for error handling
- hosts: all
  become: yes
    - name: install & configure apache
        - name: install apache
            name: apache2
          state: present
          when: ansible_os_family == "Debian"
        - name: install php modules
            name: "{{ item }}"
            - php
            - php-mysql
            - php-cli
          when: ansible_os_family == "Debian"
        - name: error occurred
            msg: error occurred
        - name: this executes always
            msg: "This always executes"

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin