DevOps Classroom Series 12/Nov/2019

Ansible Playbook for lamp (Contd..)

Script from Previous Series

  • Ubuntu:
---
- hosts: all
  become: yes
  tasks:
    - name: install apache2 and update packages
      apt:
        name: apache2
        update_cache: yes
        state: present
      notify:
        - restart and enable apache2
    - name: install php modules
      apt:
        name: "{{ item }}"
        state: present
      loop:
        - php
        - libapache2-mod-php
        - php-mysql
        - php-cli
      notify:
        - restart and enable apache2
  handlers:
    - name: restart and enable apache2
      service:
        name: apache2
        enabled: yes
        state: restarted
    
  • RedHat
---
- hosts: all
  become: yes
  tasks:
    - name: install httpd
      yum: 
        name: httpd
        state: present
      notify:
        - restart and enable httpd
    - name: install php modules
      yum:
        name: "{{ item }}"
        state: present
      loop:
        - php
        - php-mysql
      notify:
        - restart and enable httpd
  handlers:
    - name: restart and enable httpd
      service:
        name: httpd
        enabled: yes
        state: restarted

Goal is have one playbook for Redhat and Ubuntu

  • Combining both the playbooks into one
---
- hosts: all
  become: yes
  tasks:
    - name: install httpd
      yum: 
        name: httpd
        state: present
      notify:
        - restart and enable httpd
    - name: install php modules
      yum:
        name: "{{ item }}"
        state: present
      loop:
        - php
        - php-mysql
      notify:
        - restart and enable httpd
    - name: install apache2 and update packages
      apt:
        name: apache2
        update_cache: yes
        state: present
      notify:
        - restart and enable apache2
    - name: install php modules
      apt:
        name: "{{ item }}"
        state: present
      loop:
        - php
        - libapache2-mod-php
        - php-mysql
        - php-cli
      notify:
        - restart and enable apache2
  handlers:
    - name: restart and enable httpd
      service:
        name: httpd
        enabled: yes
        state: restarted
    - name: restart and enable apache2
      service:
        name: apache2
        enabled: yes
        state: restarted
  • Things to be resolved are
    • Run apt tasks only on ubuntu and yum tasks only on redhat machines.
  • To do this ansible facts come to the rescue.

Ansible Facts

  • Facts are information about the node.
  • Facts can be gathered using setup module
  • Facts can be used in the playbooks directly fact_name or by using expression "{{ fact_name }}"
  • For Setup module Refer Here

Ansible Conditionals

Continuing with the Playbook combining facts and when statement

---
- 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"
  • Need to optimize further by having less tasks and handlers bcoz they almost do the same job.

Example of git installation

  • Manual Steps
#centos
sudo yum install git -y

# ubuntu
sudo apt-get install git -y

  • Playbook in ansible
---
- hosts: all
  become: yes
  tasks:
    - name: install git 
      package: 
        name: git
        state: present

Goal: To make the Playbook Generic

  • We need to use Anisble variables.

Leave a Reply

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

About continuous learner

devops & cloud enthusiastic learner