DevOps Classroom Series – 20/Mar/2020

Ansible Galaxy

  • Repository of Reusable Ansible Artifacts
  • Lets do a simple expermient of installing of installing nodejs using ansible-galaxy reusable assets.
  • Reusability is provided by Ansible Roles.
  • Now navigate to ansible galaxy and search for node js and select any role Preview
  • Install role using ansible-galaxy command
ansible-galaxy install geerlingguy.nodejs
  • Now write a simple playbook to use this role
---
- hosts: all
  become: yes
  roles:
    - geerlingguy.nodejs
  • Now run this playbook
ansible-playbook node.yaml --syntax-check
ansible-playbook node.yaml
  • Now verify whether the node is installed or not
node --version
npm --version
  • DevOps Responsibilities:
    • You should know how to use community roles (ansible-galaxy)
    • You should know how to create custom roles

Reusable Assests in Ansible

  • Refer Here for the official documentation
  • Experiment:
    • Write a playbook to install git, nano and tree
    ---
    - hosts: all
    become: yes
    vars:
        packages:
        - git
        - tree
        - nano
        - elinks
    tasks:
        - name: install packages
        package:
            name: "{{ item }}"
            state: present
        loop: "{{ packages }}"
    
    • Write a playbook to install apache2
    ---
    - hosts: all
    become: yes
    tasks:
        - name: install apache
        package:
            name: apache2
            state: present
        notify: 
            - restart apache
    handlers:
        - name: restart apache
        service:
            name: apache2
            enabled: yes
            state: restarted
    
    • Now while installing apache, you are asked to install utilites (git, nano, tree, elinks)
    • Possible Solution 1: Use ansible-playbook to execute util.yaml and then apache.yaml
    • Possible Solution 2: Use import or include
---
- import_playbook: util.yaml
- hosts: all
  become: yes
  tasks:
    - name: install apache
      package:
        name: apache2
        state: present
      notify: 
        - restart apache
  handlers:
    - name: restart apache
      service:
        name: apache2
        enabled: yes
        state: restarted    
* Possible Solution 3: Creating a role

Ansible Roles:

  • Refer Here for official docs
  • Lets create a reusable role for tomcat8. The tomcat8 playbook is already created Refer Here
  • Now create a tomcat8 role by using the following command
ansible-galaxy role init tomcat8
  • Now try to create a ansible role for tomcat8 by copying the necessary contents in necessary yaml files.

Leave a Reply

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

Please turn AdBlock off
Floating Social Media Icons by Acurax Wordpress Designers

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