Day03 - harishgorla5/Ansible GitHub Wiki

Handlers, Templates, Loops, and Roles


๐Ÿ” 1. Handlers โ€“ What, Why, and How

โœ… What is a Handler?

A handler is a special task that only runs when explicitly notified by another task.

โœ… Why Use Handlers?

Avoid restarting services unnecessarily. Instead, only restart if something actually changed โ€” like config files.

โœ… How Handlers Work

  • A task uses notify: <handler-name>
  • If the task causes a change, Ansible queues the handler
  • Handlers are run at the end of the play (not immediately)

๐Ÿ”ง Example:

- name: Copy configuration file
  copy:
    src: my.cnf
    dest: /etc/my.cnf
  notify: Restart MariaDB

handlers:
  - name: Restart MariaDB
    service:
      name: mariadb
      state: restarted

๐Ÿงฉ 2. Jinja2 Templates โ€“ Full Explanation

โœ… What is Jinja2?

A template language that allows:

  • Variable substitution: {{ var }}
  • Control structures: {% if %}, {% for %}
  • Filters: {{ var | lower }}, {{ list | join(',') }}

โœ… Purpose in Ansible:

To generate dynamic config files based on host-specific data.

๐Ÿ“„ Example Template: index.html.j2

<h1>Hello from {{ inventory_hostname }}</h1>
{% if ansible_distribution == "Amazon" %}
<p>This is Amazon Linux.</p>
{% endif %}

๐Ÿ“ฅ Example Playbook:

- name: Deploy homepage
  template:
    src: index.html.j2
    dest: /var/www/html/index.html

๐Ÿ” 3. Loops โ€“ Repeating Tasks

โœ… Why Loops?

To repeat a task for each item in a list โ€” like creating users, installing packages, etc.

๐Ÿ“˜ Syntax:

- name: Create users
  user:
    name: "{{ item }}"
    state: present
  loop:
    - alice
    - bob
    - carol

๐Ÿ“ฆ 4. Roles โ€“ Organized Reusable Ansible Content

โœ… Why Use Roles?

To structure and reuse code for tasks like installing Apache, setting up MariaDB, etc.

๐Ÿงฑ Role Structure

apache/
โ”œโ”€โ”€ tasks/main.yml
โ”œโ”€โ”€ handlers/main.yml
โ”œโ”€โ”€ templates/index.html.j2
โ”œโ”€โ”€ vars/main.yml

๐Ÿ”ง Generate Role:

ansible-galaxy init apache

๐Ÿ”ง Use Role in Playbook:

- name: Setup Apache
  hosts: web
  become: true
  roles:
    - apache

๐Ÿงช Real-World Use Case: Deploy LAMP Stack Using All Concepts

๐Ÿ’ก Goal:

  • Install Apache with dynamic homepage using template
  • Install MariaDB using role
  • Use handlers to restart services on config change
  • Use loops to add system users

๐Ÿ“ Directory Layout

project/
โ”œโ”€โ”€ inventory
โ”œโ”€โ”€ site.yml
โ”œโ”€โ”€ roles/
โ”‚   โ”œโ”€โ”€ apache/
โ”‚   โ”‚   โ”œโ”€โ”€ tasks/main.yml
โ”‚   โ”‚   โ”œโ”€โ”€ templates/index.html.j2
โ”‚   โ”‚   โ””โ”€โ”€ handlers/main.yml
โ”‚   โ””โ”€โ”€ mariadb/
โ”‚       โ”œโ”€โ”€ tasks/main.yml
โ”‚       โ”œโ”€โ”€ templates/my.cnf.j2
โ”‚       โ””โ”€โ”€ handlers/main.yml

๐Ÿ”ธ Example: roles/apache/tasks/main.yml

- name: Install Apache
  yum:
    name: httpd
    state: present

- name: Deploy dynamic homepage
  template:
    src: index.html.j2
    dest: /var/www/html/index.html
  notify: Restart Apache

๐Ÿ”ธ roles/apache/handlers/main.yml

- name: Restart Apache
  service:
    name: httpd
    state: restarted

๐Ÿ”ธ roles/apache/templates/index.html.j2

<h1>Hello from {{ inventory_hostname }}</h1>
<p>Powered by Ansible</p>

๐Ÿ”ธ roles/mariadb/tasks/main.yml

- name: Install MariaDB
  yum:
    name: mariadb-server
    state: present

- name: Deploy DB config
  template:
    src: my.cnf.j2
    dest: /etc/my.cnf
  notify: Restart MariaDB

๐Ÿ”ธ roles/mariadb/handlers/main.yml

- name: Restart MariaDB
  service:
    name: mariadb
    state: restarted

๐Ÿ”ธ site.yml (Main Playbook)

- name: Configure Web Server
  hosts: web
  become: true
  roles:
    - apache

- name: Configure DB Server
  hosts: db
  become: true
  roles:
    - mariadb

- name: Add system users
  hosts: all
  become: true
  tasks:
    - name: Create users
      user:
        name: "{{ item }}"
        state: present
      loop:
        - dev
        - qa
        - ops

โœ… Summary: What You Practiced Today

Concept Real Use
Handlers Restart httpd/mariadb on config change
Jinja2 Dynamic HTML and config file templates
Loops User creation and package installs
Roles Reusable, modular playbooks

โš ๏ธ **GitHub.com Fallback** โš ๏ธ