DevOps Classroom Series – 03/Aug/2020

Ansible Modules (Contd..)

  • Lets try to install lamp stack on ubuntu 18.04 and centos 7
  • Lets create a hosts file with the following entries with centos and ubuntu as group names
[centos]
172.31.4.122

[ubuntu]
172.31.3.192
  • Lets create lamp_centos.yaml
---
- name: install lamp stack on Centos
  become: yes
  hosts: centos
  tasks:
    - name: install apache
      yum:
        name: httpd
        state: present
    - name: start and enable apache
      service:
        name: httpd
        enabled: yes
        state: started
  • Lets create lamp_ubuntu.yaml file
---
- name: 'Install and configure lamp'
  hosts: ubuntu
  become: yes
  tasks:
    - name: install apache2 and update ubuntu package definitions
      apt:
        name: apache2
        update_cache: yes
        state: present
    - name: enable and start apache2
      service:
        name: apache2
        enabled: yes
        state: started
  • Now lets execute playbook on centos group
ansible-playbook -i hosts lamp_centos.yaml

Preview

  • If you need more logs execute the following commands
ansible-playbook -v -i hosts lamp_centos.yaml
ansible-playbook -vv -i hosts lamp_centos.yaml
ansible-playbook -vvv -i hosts lamp_centos.yaml
  • Now navigate to http://publicurl:centos Preview
  • Now lets execute the lamp yaml written so far on ubuntu
ansible-playbook -i hosts lamp_ubuntu.yaml

Preview

  • Now navigate to http://publicurl:ubuntu Preview
  • Groups in inventory file are for logical grouping of servers
[webserver]
192.168.10.11
192.168.10.111
192.168.10.112
192.168.10.113

[dbserver]
192.168.10.10
192.168.10.102
192.168.10.103
192.168.10.104

  • Now for centos & ubuntu we will be skipping mysql installation.
  • For centos node the php packages which we need to install are
    • php
    • php-mysql
    • php-fpm
  • Lets try to restart apache, Now the centos yaml will look like
---
- name: install lamp stack on Centos
  become: yes
  hosts: centos
  tasks:
    - name: install apache
      yum:
        name: httpd
        state: present
    - name: start and enable apache
      service:
        name: httpd
        enabled: yes
        state: started
    - name: install php
      yum:
        name: php
        state: present
    - name: install php-mysql
      yum:
        name: php-mysql
        state: present
    - name: install php-fpm
      yum:
        name: php-fpm
        state: present
    - name: restart apache
      service:
        name: httpd
        state: restarted
  • For ubuntu we need to install the following php packages and restart apache
    • php
    • libapache2-mod-php
    • php-mysql
    • php-cli
  • Now the lamp_ubuntu.yaml will be like
---
- name: 'Install and configure lamp'
  hosts: ubuntu
  become: yes
  tasks:
    - name: install apache2 and update ubuntu package definitions
      apt:
        name: apache2
        update_cache: yes
        state: present
    - name: enable and start apache2
      service:
        name: apache2
        enabled: yes
        state: started
    - 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: install php-cli
      apt:
        name: php-cli
        state: present
    - name: restart apache
      service:
        name: apache2
        state: restarted
  • Now lets execute this playbook
ansible-playbook -i hosts lamp_ubuntu.yaml
ansible-playbook -i hosts lamp_centos.yaml

Preview Preview

  • We still have work on lamp stack left, but we have following questions to be addressed
    1. Why should i copy paste tasks with different values? Can’t i use some thing like looping structure
    - name: install php-mysql
      yum:
        name: php-mysql
        state: present
    - name: install php-fpm
      yum:
        name: php-fpm
        state: present
    
    1. Why should i write different playbooks per os, can’t we write one?

Ansible Modules (Contd..)

  • Lets try to install lamp stack on ubuntu 18.04 and centos 7
  • Lets create a hosts file with the following entries with centos and ubuntu as group names
[centos]
172.31.4.122

[ubuntu]
172.31.3.192
  • Lets create lamp_centos.yaml
---
- name: install lamp stack on Centos
  become: yes
  hosts: centos
  tasks:
    - name: install apache
      yum:
        name: httpd
        state: present
    - name: start and enable apache
      service:
        name: httpd
        enabled: yes
        state: started
  • Lets create lamp_ubuntu.yaml file
---
- name: 'Install and configure lamp'
  hosts: ubuntu
  become: yes
  tasks:
    - name: install apache2 and update ubuntu package definitions
      apt:
        name: apache2
        update_cache: yes
        state: present
    - name: enable and start apache2
      service:
        name: apache2
        enabled: yes
        state: started
  • Now lets execute playbook on centos group
ansible-playbook -i hosts lamp_centos.yaml

Preview

  • If you need more logs execute the following commands
ansible-playbook -v -i hosts lamp_centos.yaml
ansible-playbook -vv -i hosts lamp_centos.yaml
ansible-playbook -vvv -i hosts lamp_centos.yaml
  • Now navigate to http://publicurl:centos Preview
  • Now lets execute the lamp yaml written so far on ubuntu
ansible-playbook -i hosts lamp_ubuntu.yaml

Preview

  • Now navigate to http://publicurl:ubuntu Preview
  • Groups in inventory file are for logical grouping of servers
[webserver]
192.168.10.11
192.168.10.111
192.168.10.112
192.168.10.113

[dbserver]
192.168.10.10
192.168.10.102
192.168.10.103
192.168.10.104

  • Now for centos & ubuntu we will be skipping mysql installation.
  • For centos node the php packages which we need to install are
    • php
    • php-mysql
    • php-fpm
  • Lets try to restart apache, Now the centos yaml will look like
---
- name: install lamp stack on Centos
  become: yes
  hosts: centos
  tasks:
    - name: install apache
      yum:
        name: httpd
        state: present
    - name: start and enable apache
      service:
        name: httpd
        enabled: yes
        state: started
    - name: install php
      yum:
        name: php
        state: present
    - name: install php-mysql
      yum:
        name: php-mysql
        state: present
    - name: install php-fpm
      yum:
        name: php-fpm
        state: present
    - name: restart apache
      service:
        name: httpd
        state: restarted
  • For ubuntu we need to install the following php packages and restart apache
    • php
    • libapache2-mod-php
    • php-mysql
    • php-cli
  • Now the lamp_ubuntu.yaml will be like
---
- name: 'Install and configure lamp'
  hosts: ubuntu
  become: yes
  tasks:
    - name: install apache2 and update ubuntu package definitions
      apt:
        name: apache2
        update_cache: yes
        state: present
    - name: enable and start apache2
      service:
        name: apache2
        enabled: yes
        state: started
    - 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: install php-cli
      apt:
        name: php-cli
        state: present
    - name: restart apache
      service:
        name: apache2
        state: restarted
  • Now lets execute this playbook
ansible-playbook -i hosts lamp_ubuntu.yaml
ansible-playbook -i hosts lamp_centos.yaml

Preview Preview

  • We still have work on lamp stack left, but we have following questions to be addressed
    1. Why should i copy paste tasks with different values? Can’t i use some thing like looping structure
    - name: install php-mysql
      yum:
        name: php-mysql
        state: present
    - name: install php-fpm
      yum:
        name: php-fpm
        state: present
    
    1. Why should i write different playbooks per os, can’t we write one?

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