DevOps Classroomnotes 08/Jan/2023

Ansible Contd

Tomcat Playbook optimization

  • Refer Here for the rest of steps added
  • Generally there are some tasks which need to be executed as a reaction to other task i.e. restart tomcat only when configuration files have changed
  • For acheiving the above we need to know about ansible Handlers
  • Refer Here for the changes done to include handlers
  • The way we copied the same content into two different locations with two different files Refer Here for the changes
  • Refer Here for the changes done to include loop in ansible Refer Here
  • Lets summarize the improvements which we have done
    • Using Generic modules like package over apt or yum
    • Using variables to parametrize gives us options to set host and group level values
    • conditionally execute tasks based on facts
    • use handlers for automating steps which need not execute every time and they are required based on some changes
    • use loops rather than copy paste of modules

Ansible Handlers

Exercise

  • Write a playbook to install the following
  • ubuntu steps
sudo apt update
sudo apt install apache2
sudo apt install php libapache2-mod-php php-mysql
# create a file /var/www/html/info.php  with following content
<?php
phpinfo();
?> 
  • centos steps
sudo dnf install httpd
sudo systemctl enable httpd
sudo systemctl start httpd
sudo dnf install php php-mysqlnd
sudo systemctl restart httpd
# create a file /var/www/html/info.php  with following content
<?php
phpinfo();
?>
  • Sample solution – First version
---
- name: install apache server
  become: yes
  tasks:
    - name: install apache on ubuntu
      ansible.builtin.apt:
        name: apache2
        update_cache: yes
        state: present
      when: ansible_facts['distribution'] == "Ubuntu"
      notify:
        - enable and restart apache2
    - name: install apache on centos
      ansible.builtin.dnf:
        name: httpd
        state: present
      notify:
        - enable and restart httpd
      when: ansible_facts['distribution'] == "Centos"
    - name: install php modules
      ansible.builtin.apt:
        name:
          - php
          - libapache2-mod-php
          - php-mysql
        state: present
      when: ansible_facts['distribution'] == "Ubuntu"
      notify:
        - enable and restart apache2
    - name: install php modules on centos
      ansible. builtin.dnf:
        name:
          - php
          - php-mysqlnd
        state: present
       when: ansible_facts['distribution'] == "Centos"
       notify:
        - enable and restart apache2
    - name: copy info.php
      ansible.builtin.copy:
        src: info.php
        dest: /var/www/html/info.php    
  handlers:
    - name: enable and restart apache2
      ansible.builtin.systemd:
        name: apache2
        enabled: yes
        state: restarted
    - name: enable and restart httpd
      ansible.builtin.systemd:
        name: httpd
        enabled: yes
        state: restarted

Published
Categorized as Uncategorized Tagged

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
Animated Social Media Icons by Acurax Wordpress Development Company

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