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

Preview

  • Now go through the documentation and understand how to use the role and the variables Preview
  • Now lets create an inventory file
localhost
  • Now lets write a playbook which uses the role downloaded
---
- hosts: all
  roles:
    - name: geerlingguy.mysql
      become: yes
  tasks:
    - name: print role execution completed
      debug:
        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
    installtomcat.yaml
    roles/
      khaja.tomcat/
    
    1. If it doesn’t find it there it navigates to $(home directory)/.ansible/roles/<role-name>
    ~/.ansible/roles/khaja.tomcat/
    
    1. If it doesn’t find here then it searches in /etc/ansible/roles/<role-name>
    /etc/ansible/roles/khaja.tomcat/
    
  • 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

Block

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

Leave a Reply

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

About learningthoughtsadmin