DevOps Classroom Series – 23/Oct/2021

What has to be done if we donot find the right module

  • If we don’t find a module, we have two options
    • Create your own custom module:
      • To do this we need to know how to code in python Refer Here
    • Directly execute the linux/windows command
      • If you are automating any linux/unix/mac we can use shell module Refer Here
      • If you are automating on windows we can use win_shell module Refer Here
      • These modules are not idempotent i.e. whenever the playbook is executed the task execute all the time, so we need to come up with idempotence i.e using some kind of when condition

Ansible Galaxy

  • This hosts the work done by the community to deploy applications, configure softwares etc..
  • We can use this work in our playbooks.
  • There are two possible ways of sharing work in Ansible Galaxy Refer Here
    • Roles
    • Collections
  • Activity: Lets try to find the role to install mysql in node 2 Preview
  • Refer Here for the changes done
  • Log in to the ansible control node and install role
ansible-galaxy install geerlingguy.mysql

Preview

  • Now lets try to run our playbook Preview Preview Preview

  • Using community roles makes our deployments simple & in many case we are expected to create roles for the application deployments so that they can be reused.

  • Note: Playbooks also can be reused by using import playbooks Refer Here

Activity 2: Lets try to create a role from the tomcat playbook which we have created

  • In Ansible, role is the primary mechanism for breaking a playbook into multiple files to simplify writing playbooks & making them easier to reuse
  • Basic structure of ansible role Refer Here Preview
  • Refer Here for the official docs of the role directory strucutre
  • Refer Here this changeset for tomcat role
  • Ansible searches for the role in the following folder and if it doesn’t find it throws error
    • current dir of playbook/roles
    • ~/.ansible/roles
    • /usr/share/ansible/roles
    • /etc/ansible/roles Preview
  • Lets copy tomcat to localdir/roles Preview
  • Now execute the role Preview Preview
  • Refer Here for the changeset containing os specific vars
  • The inventory used is
localhost
centos ansible_host=172.31.29.133
  • The playbook for install tomcat using role
---
- name: install tomcat
  hosts: all
  roles:
    - role: '/home/ansible/AnsibleZone/Oct21/myroles/tomcat'
      become: yes

Include vs Import

  • The import_* and include_*: Ansible has two modes of operation for reusable content with including and importing .
  • The main difference is
    • All import_* statements are preprocessed at the time playbooks are parsed
    • All include_* statements are process as they are encountered during execution
    • import is static and include is dynamic

Exercise:

  • Create a role to deploy the nop commerce and spring petclinic as service

Tags

  • In Ansible we can add tags to a single task or include statement
  • Lets look at the following playbook
---
- name: learning tags
  become: yes
  hosts: all
  tasks:
    - name: install git
      package:
        name: git
        state: present
      tags:
        - essential
        - server
    - name: install tree
      package:
        name: tree
        state: present
      tags:
        - developer

  • Lets play with tags Preview
  • We can also skip tags Preview

Topics left

  • Ansible Vault
  • Ansible Collections
  • Ansible Parallelism
  • Ansible Dynamic Inventory
  • Ansible Tower
  • Ansible on Windows

Leave a Reply

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

Please turn AdBlock off
Social Media Icons Powered by Acurax Web Design Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube