DevOps Classroom Series – 16/Mar/2020

Ansible Playbook for Lamp

  • Refer Here for installation steps
  • Manual Linux Commands for Lamp Stack
sudo apt update
sudo apt install apache2
sudo systemctl restart apache2
sudo apt install php libapache2-mod-php php-mysql
sudo nano /etc/apache2/mods-enabled/dir.conf
<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
sudo systemctl restart apache2
sudo apt install php-cli
sudo nano /var/www/html/info.php
<?php
phpinfo();
?>
sudo systemctl restart apache2

preview

  • The playbook which we are writing is expected to work only on ubuntu to start with, now when any user tries to run this playbook on other distribution, we should fail/stop the execution
  • To loop through the task ansible supports loops refer here
  • Excluding file creations the playbook looks like below
---
- hosts: webservers
  become: yes
  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: apache2
        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:
        - php
        - libapache2-mod-php 
        - php-mysql
        - php-cli
    - name: restart apache2
      service:
        name: apache2 
        state: restarted
      
  • Try running this playbook on any ubuntu server
 ansible-playbook -i inventory lamp.yaml --syntax-check
 ansible-playbook -i inventory lamp.yaml --list-hosts
 ansible-playbook -i inventory lamp.yaml --check
 ansible-playbook -i inventory lamp.yaml

Preview

  • Our playbook is hardcoding package names not a good approach, so lets define the package names as variables in playbook. After adding variables, 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
      
      
  • Our problem as of now is service is present multiple times and service restart should not happen every time, they should be reactive action to installations.

Leave a Reply

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

About continuous learner

devops & cloud enthusiastic learner