DevOps Classroom Series – 16/Nov/2019

Ansible Files

  • In DevOps Pipelines, it will necessary to download/copy the package built by build system (Maven, Msbuild, make) to the Node.
  • To Download the file into node use module get_url
  • To copy the file from ACS to node use copy
  • Now lets use the same lamp playbook and create a file called as info.php with following contnets
<?php
phpinfo();
?>
  • Now lets add the copy module to the existing playbook
---
- hosts: webservers
  become: yes
  tasks:
    - name: fail if it is unsupported os
      fail:
        msg: This playbook is supported only on redhat and debain variants
      when: ansible_facts['os_family'] != "RedHat" and ansible_facts['os_family'] != "Debian"
    - name: install apache
      package: 
        name: "{{ package_name }}"
        state: present
      notify:
        - restart and enable apache
    - name: printing php modules
      debug:
        var: php_modules
    - name: install php modules
      package:
        name: "{{ item }}"
        state: present
      loop: "{{ php_modules }}"
    - name: copy the home page
      copy:
        src: info.php
        dest: /var/www/html/info.php
      notify:
        - restart and enable apache
    #- name: Download info php page
    #  get_url:
    #    url: https://qt-s3-new-testing.s3-us-west-2.amazonaws.com/info.php
    #    dest: /var/www/html/info.php
    #  notify:
    #    - restart and enable apache
  handlers:
    - name: restart and enable apache
      service:
        name: "{{ package_name }}"
        enabled: yes
        state: restarted

Exercise

  • Create a file called as Readme.txt in the root folder.
  • The contents of the file should be
Create by Ansible
Os family is <os >
Os distribution is <distribution>
version is <version>

Solution to above Exercise

  • Whenever you have dynamic values, ansible has a templating system which is widely used in python based developments. That templating system is Jinja
  • Jinja templates are the files with extension .j2
  • Lets try to solve the above situation
  • Create a Readme.txt.j2 file with following content
Create by Ansible
Os family is {{ ansible_os_family }}
Os distribution is {{ ansible_distribution }}
version is {{ ansible_distribution_version }}
  • Write a simple playbook with following content
---
- hosts: all
  tasks:
  - name: copy the readme
    template:
      src: Readme.txt.j2
      dest: "{{ ansible_user_dir }}/Readme.txt"
  • Run this playbook and check the results
  • In the J2 file facts and variables (user defined) can be used.
  • Individual find and replaces can be done with a module lineinfile

Pre-Tasks and Post-Tasks

  • Pre-Tasks are the tasks which get executed before execution of tasks
  • Post-Tasks are the tasks which get executed after execution of tasks
---
- hosts: all
  become: yes
  pre_tasks:
    - name: write a message
      debug:
        msg: "Starting Execution of Playbook"
  tasks:
    - name: install tree
      package:
        name: tree
        state: present
  post_tasks:
    - debug:
        msg: Playbook execution completed

How to Reuse Playbooks

  • You already have lamp.yaml, now you want to use same lamp installation along with some other configurations
  • You can call existing playbooks using include or import playbooks. Refer
  • Lets write a simple playbook to call other playbook
---
- hosts: all
  become: yes
  tasks:
   - name: call lamp playbook
     import_playbook: lamp.yaml
   - name: install mysql
     package:
       name: mysql-server
       state: present

Ansible roles

  • Its all about reusability
  • Using others automation efforts in ansible is simplified using roles
  • Any user can share the work by converting playbook to ansible role and submit to ansible community. Ansible community which shares these roles is Ansible Galaxy
  • As an example lets install java using Ansible Galaxy role. Refer Here
  • Download the Ansible Role by executing ansible-galaxy install geerlingguy.java
  • Now to install java lets write a playbook
--- 
- hosts: all 
  become: yes
  roles:
    - role: geerlingguy.java

Creating Ansible Roles

  • Sample folder structure
    • playbooks
      • roles
        • rolename
  • Login into the ACS and create a new directory called roles and execute the following command
ansible-galaxy init lamp
  • Now use lamp playbook to copy tasks in to tasks\main.yml, and do the same for handlers
  • if you have any files or templates copy them in files or templates folder
  • Now cd into playbooks folder and create one more folder called as environments
    • playbooks
      • roles
        • lamp (folder)
      • environments
        • DEV (file)
        • TEST (file)
        • UAT (file)
      • myplaybook.yml
  • Edit myplaybook.yml file with
---
- hosts: webservers
  become: yes
  roles:
    - role: lamp
  • To create DEV Environment execute
ansible-playbook -i environments/DEV  myplaybook.yml

By continuous learner

devops & cloud enthusiastic learner

Leave a ReplyCancel reply

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

Please turn AdBlock off
Social Network Integration by Acurax Social Media Branding Company

Discover more from Direct DevOps from Quality Thought

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

Continue reading

Exit mobile version
%%footer%%