DevOps Classroom Series -13/Nov/2019

Goal: Write Common Tasks and Handlers

  • From Previous Series this playbook will be our starting point
---
- hosts: all
  become: yes
  tasks:
    - name: install httpd
      yum: 
        name: httpd
        state: present
      notify:
        - restart and enable httpd
      when: ansible_facts['os_family'] == "RedHat"
    - name: install php modules
      yum:
        name: "{{ item }}"
        state: present
      loop:
        - php
        - php-mysql
      notify:
        - restart and enable httpd
      when: ansible_facts['os_family'] == "RedHat"
    - name: install apache2 and update packages
      apt:
        name: apache2
        update_cache: yes
        state: present
      notify:
        - restart and enable apache2
      when: ansible_facts['os_family'] == "Debian"
    - name: install php modules
      apt:
        name: "{{ item }}"
        state: present
      loop:
        - php
        - libapache2-mod-php
        - php-mysql
        - php-cli
      notify:
        - restart and enable apache2
      when: ansible_facts['os_family'] == "Debian"
  handlers:
    - name: restart and enable httpd
      service:
        name: httpd
        enabled: yes
        state: restarted
      when: ansible_facts['os_family'] == "RedHat"
    - name: restart and enable apache2
      service:
        name: apache2
        enabled: yes
        state: restarted
      when: ansible_facts['os_family'] == "Debian"
  • To replace apt or yum we can use package module. In the current Scenario, Package names are different for RedHat and Ubuntu
  • Lets use Ansible Variables to remove the package name hard coding.
  • Approach 1: Define Variables in inventory files.
  • Initial Inventory looks as shown below
172.31.26.196
172.31.25.93
  • Lets add Groups to the inventory to something like below in inventory file
[ubuntu]
172.31.26.196

[redhat]
172.31.25.93

[webservers]
172.31.26.196 
172.31.25.93  
  • Lets add the variable package_name to webserver group as shown below
[ubuntu]
172.31.26.196

[redhat]
172.31.25.93

[webservers]
172.31.26.196 package_name=apache2
172.31.25.93  package_name=httpd
  • For now lets comment the install php modules section
---
- hosts: all
  become: yes
  tasks:
    - name: install httpd
      yum: 
        name: httpd
        state: present
      notify:
        - restart and enable httpd
      when: ansible_facts['os_family'] == "RedHat"
    #- name: install php modules
    #  yum:
    #    name: "{{ item }}"
    #    state: present
    #  loop:
    #    - php
    #    - php-mysql
    #  notify:
    #    - restart and enable httpd
    #  when: ansible_facts['os_family'] == "RedHat"
    - name: install apache2 and update packages
      apt:
        name: apache2
        update_cache: yes
        state: present
      notify:
        - restart and enable apache2
      when: ansible_facts['os_family'] == "Debian"
    #- name: install php modules
    #  apt:
    #    name: "{{ item }}"
    #    state: present
    #  loop:
    #    - php
    #    - libapache2-mod-php
    #    - php-mysql
    #    - php-cli
    #  notify:
    #    - restart and enable apache2
    #  when: ansible_facts['os_family'] == "Debian"
  handlers:
    - name: restart and enable httpd
      service:
        name: httpd
        enabled: yes
        state: restarted
      when: ansible_facts['os_family'] == "RedHat"
    - name: restart and enable apache2
      service:
        name: apache2
        enabled: yes
        state: restarted
      when: ansible_facts['os_family'] == "Debian"
  • Now Replace apt and yum with package module and also replace all in hosts with webservers
---
- hosts: webservers
  become: yes
  tasks:
    - name: install apache
      package: 
        name: "{{ package_name }}"
        state: present
      notify:
        - restart and enable apache
      when: ansible_facts['os_family'] == "RedHat" or ansible_facts['os_family'] == "Debian"
  handlers:
    - name: restart and enable apache
      service:
        name: "{{ package_name }}"
        enabled: yes
        state: restarted
      when: ansible_facts['os_family'] == "RedHat" or or ansible_facts['os_family'] == "Debian"

  • Now lets write the same inventory in YAML format with modules included
all:
  children:
    webservers:
      hosts:
        172.31.26.196:
          package_name: ubuntu
          php-modules:
            - php
            - libapache2-mod-php
            - php-mysql
            - php-cli
        172.31.25.93:
          package_name: httpd
          php-modules:
            - php
            - php-mysql
  • Now lets use php-modules variable in playbook to install modules
  • Ensure you use a debug module to print variables
  • Right now playbook is
---
- hosts: webservers
  become: yes
  tasks:
    - name: install apache
      package: 
        name: "{{ package_name }}"
        state: present
      notify:
        - restart and enable apache
      when: ansible_facts['os_family'] == "RedHat" or ansible_facts['os_family'] == "Debian"
    - name: printing php modules
      debug:
        var: php-modules
    - name: install php modules
      package:
        name: "{{ item }}"
        state: present
      notify:
        - restart and enable apache
      when: ansible_facts['os_family'] == "RedHat" or ansible_facts['os_family'] == "Debian"
      loop: "{{ php-modules }}"
  handlers:
    - name: restart and enable apache
      service:
        name: "{{ package_name }}"
        enabled: yes
        state: restarted
      when: ansible_facts['os_family'] == "RedHat" or or ansible_facts['os_family'] == "Debian"

  • php-modules is not recognized as variable, find the resolution.

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
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