DevOps Classroom Series – 17/Mar/2020

Lamp Contd

  • As of now our playbook is as shown below
---
- hosts: webservers
  become: yes
  vars:
    package_apache: apache2
    packages_php_modules:
      - php
      - libapache2-mod-php 
      - php-mysql
      - php-cli
  tasks:
    - name: fail playbook on unsupported platforms
      fail:
        msg: 'This playbook is currently developed only for ubuntu 18'
      when: ansible_distribution != 'Ubuntu'
    - name: update and install apache
      apt: # https://docs.ansible.com/ansible/latest/modules/apt_module.html
        name: "{{ package_apache }}"
        update_cache: yes
        state: present
    - name: restart and enable apache2
      service:
        name: "{{ package_apache }}" 
        enabled: yes 
        state: restarted
    - name: install php modules
      apt:
        name: "{{ item }}"
        state: present
      loop: "{{ packages_php_modules}}"
    - name: restart apache2
      service:
        name: "{{ package_apache }}" 
        state: restarted
      
      
  • In the above playbook the problem is restart service is written multiple times and restart apache should happen only when the apache is installed or php modules are installed
  • To solve this Ansible has handlers Refer Here
  • To understand handlers lets write a simple playbook
---
- hosts: all
  become: yes
  tasks:
    - name: uninstall git
      package:
        name: git
        state: absent
    - name: log message for uninstallation
      debug:
        msg: "git is uninstalled"

  • This playbook displays git is uninstalled message every time, but actually it has to display the message when git is uninstalled by ansible, so using handlers will solve the issue
---
- hosts: all
  become: yes
  tasks:
    - name: uninstall git
      package:
        name: git
        state: absent
      notify:
        - log message for uninstallation
  handlers:
    - name: log message for uninstallation
      debug:
        msg: "git is uninstalled"
  • Now in the same approach if we add handlers to handle service restarts our playbook will look like
---
- hosts: webservers
  become: yes
  vars:
    package_apache: apache2
    packages_php_modules:
      - php
      - libapache2-mod-php 
      - php-mysql
      - php-cli
  tasks:
    - name: fail playbook on unsupported platforms
      fail:
        msg: 'This playbook is currently developed only for ubuntu 18'
      when: ansible_distribution != 'Ubuntu'
    - name: update and install apache
      apt: # https://docs.ansible.com/ansible/latest/modules/apt_module.html
        name: "{{ package_apache }}"
        update_cache: yes
        state: present
      notify:
        - debug message for apache installation
        - restart and enable apache2
    - name: install php modules
      apt:
        name: "{{ item }}"
        state: present
      loop: "{{ packages_php_modules}}"
      notify:
        - debug message for php modules
        - restart and enable apache2
  handlers:
    - name: restart and enable apache2
      service:
        name: "{{ package_apache }}" 
        enabled: yes 
        state: restarted
    - name: debug message for apache installation
      debug:
        msg: "{{ package_apache }} is installed"
    - name: debug message for php modules
      debug:
        msg: "php modules are installed"
      
  • We have one last step of creating a file called as info.php in /var/www/html/info.php with following content
<?php
phpinfo();
?>
  • This step means we have to create a file. Creating a file can be done into two ways
    • If the file is available on some url download the file
    • Copy the file from your system into server whereever you want to run
  • For file demo use this playbook
---
- hosts: all
  tasks:
    - name: create an empty file # This module creates an empty file on node
      file:
        path: /home/devops/readme.txt
        state: touch
    - name: copy the files on your server # This module creates a symlink between two files on the node
      file:
        src: /home/devops/readme.txt
        dest: /home/devops/readmecopy.txt
        state: link
    - name: create directory # This module creates empty directory on node
      file:
        path: /home/devops/temp
        state: directory
    - name: download file from url # This module downloads the file from url into node
      get_url:
        url: https://war-jar-files.s3-us-west-2.amazonaws.com/gameoflife.war 
        dest: /home/devops/gameoflife.war
    - name: copy the file from acs to node # This module copies the file from acs to node
      copy:
        src: demo.txt
        dest: /home/devops/demo.txt
  • Create info.php in the folder where you are writing playbook with following content
<?php
phpinfo();
?>
  • Now use copy module to copy this info.php to node and the playbook looks like below
---
- hosts: webservers
  become: yes
  vars:
    package_apache: apache2
    packages_php_modules:
      - php
      - libapache2-mod-php 
      - php-mysql
      - php-cli
  tasks:
    - name: fail playbook on unsupported platforms
      fail:
        msg: 'This playbook is currently developed only for ubuntu 18'
      when: ansible_distribution != 'Ubuntu'
    - name: update and install apache
      apt: # https://docs.ansible.com/ansible/latest/modules/apt_module.html
        name: "{{ package_apache }}"
        update_cache: yes
        state: present
      notify:
        - debug message for apache installation
        - restart and enable apache2
    - name: install php modules
      apt:
        name: "{{ item }}"
        state: present
      loop: "{{ packages_php_modules}}"
      notify:
        - debug message for php modules
        - restart and enable apache2
    - name: copy info.php
      copy:
        src: info.php
        dest: /var/www/html/info.php
      notify:
        - restart and enable apache2
  handlers:
    - name: restart and enable apache2
      service:
        name: "{{ package_apache }}" 
        enabled: yes 
        state: restarted
    - name: debug message for apache installation
      debug:
        msg: "{{ package_apache }} is installed"
    - name: debug message for php modules
      debug:
        msg: "php modules are installed"
      
  • After this navigate to url http://<publicip>/info.php and you should see the php test page.

  • Exercise: Make this work for centos 7 as well using docs over here

  • In the next class we would configure tomcat on linux machines and learn the subsequent ansible topics along with that.

By continuous learner

devops & cloud enthusiastic learner

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