DevOps Classroom Series – Ansible – 11/Nov/2019

Widely Used Modules

  • Installations:
    • apt
    • yum
    • package
  • Services:
    • service
    • systemd
  • Files:
    • file
    • get_url
  • Templating
    • template

Sample Deployment in Ansible

  • We will try to deploy Apache with PHP modules
  • Ubuntu Steps
sudo apt-get update
sudo apt-get install apache2 -y
sudo systemctl enable apache2
sudo apt-get install php libapache2-mod-php php-mysql php-cli -y
sudo vi /var/www/html/info.php
<?php
phpinfo();
?>
sudo systemctl restart apache2
  • Centos Steps:
sudo yum install httpd -y
sudo systemctl enable httpd
sudo yum install php php-mysql
sudo vi /var/www/html/info.php
<?php
phpinfo();
?>
sudo systemctl restart httpd
  • Initial Playbook for Ubuntu looks as shown below
---
- hosts: all
  become: yes
  tasks:
    - name: install apache2 and update packages
      apt:
        name: apache2
        update_cache: yes
        state: present
    - name: enable and restart apache2 service
      service:
        name: apache2
        enabled: yes
        state: restarted
    - name: install php
      apt:
        name: php
        state: present
    - name: install libapache2-mod-php
      apt:
        name: libapache2-mod-php
        state: present
    - name: install php-mysql
      apt:
        name: php-mysql
        state: present
    - name: php-cli
      apt:
        name: php-cli
        state: present
    - name: restart apache2
      service:
        name: apache2
        state: restarted
  • Playbook for Redhat/Centos looks as shown below
---
- hosts: all
  become: yes
  tasks:
    - name: install httpd
      yum: 
        name: httpd
        state: present
    - name: enable and restart httpd
      service:
        name: httpd
        enabled: yes
        state: restarted
    - name: install php
      yum:
        name: php
        state: present
    - name: install php-mysql
      yum:
        name: php-mysql
        state: present
    - name: restart httpd
      service:
        name: httpd
        state: restarted
  • Ansible Handlers: Refer Here
  • Lets change our playbooks by adding handlers.
  • Ubuntu Playbook looks as shown below
---
- 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
      apt:
        name: php
        state: present
    - name: install libapache2-mod-php
      apt:
        name: libapache2-mod-php
        state: present
    - name: install php-mysql
      apt:
        name: php-mysql
        state: present
    - name: php-cli
      apt:
        name: php-cli
        state: present
      notify:
        - restart and enable apache2
  handlers:
    - name: restart and enable apache2
      service:
        name: apache2
        enabled: yes
        state: restarted
    
  • RedHat Playbook looks as shown below
---
- hosts: all
  become: yes
  tasks:
    - name: install httpd
      yum: 
        name: httpd
        state: present
      notify:
        - restart and enable httpd
    - name: install php
      yum:
        name: php
        state: present
    - name: install php-mysql
      yum:
        name: php-mysql
        state: present
      notify:
        - restart and enable httpd
  handlers:
    - name: restart and enable httpd
      service:
        name: httpd
        enabled: yes
        state: restarted
  • Now in the playbooks we can write it in a simplified way using looping constructs Refer Here

  • After applying loops ubuntu playbook looks as shown below

---
- 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
    
  • After applying loops redhat/centos playbook looks as shown below
---
- 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

Leave a Reply

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

About continuous learner

devops & cloud enthusiastic learner