11-Day4-3-Jinja2Template
Day 4: Lab Series 3 - Dynamic Configurations with Jinja2 Templates
Objective:
This lab introduces participants to creating and utilizing Jinja2 templates for dynamic configurations in Ansible playbooks. By the end of these exercises, participants will understand how to use variables, loops, and conditionals within templates to generate configuration files dynamically.
Lab 3.1: Basic Jinja2 Template for Configuration
Scenario:
Generate an Nginx configuration file dynamically using a Jinja2 template.
Steps:
Prepare the Template File:
Create the template file
nginx.conf.j2in the~/day4/templates/directory:server { listen 80; server_name {{ inventory_hostname }}; root /var/www/{{ web_root }}; location / { index index.html; } }
Create the Playbook:
Save as
~/day4/nginx_config.yaml:- name: Generate Nginx Configuration File hosts: localhost become: yes vars: web_root: html tasks: - name: Ensure the Nginx configuration directory exists file: path: /etc/nginx/conf.d state: directory - name: Generate Nginx configuration file from template template: src: ~/day4/templates/nginx.conf.j2 dest: /etc/nginx/conf.d/default.conf - name: Restart Nginx to apply configuration service: name: nginx state: restarted
Run the Playbook:
ansible-playbook ~/day4/nginx_config.yamlVerify Output:
Check
/etc/nginx/conf.d/default.confto ensure it has been correctly generated using the template.
Lab 3.2: Using Host Variables with Templates
Scenario:
Generate different Apache virtual host configurations for multiple servers using Jinja2 templates.
Steps:
Update the Inventory File:
Define host-specific variables:
Prepare the Template File:
Save as
apache_vhost.conf.j2in the~/day4/templates/directory:
Create the Playbook:
Save as
~/day4/apache_config.yaml:
Run the Playbook:
Verify Output:
Check
/etc/httpd/conf.d/for separate configuration files for each host.Validate the configurations by accessing the web servers using their respective ports.
Lab 3.3: Conditional Rendering in Templates
Scenario:
Create an SSH banner message file dynamically, varying the content based on the host’s operating system.
Steps:
Prepare the Template File:
Save as
ssh_banner.j2in the~/day4/templates/directory:
Create the Playbook:
Save as
~/day4/ssh_banner.yaml:
Run the Playbook:
Verify Output:
Check
/etc/issue.netfor the dynamically rendered banner message.Verify that the SSH banner displays on login.
Optional Lab: Nested Loops and Templates
Scenario:
Create user-specific directories and permissions dynamically using a template with nested loops.
Prepare the Template File:
Save as
user_dirs.j2in the~/day4/templates/directory:
Create the Playbook:
Save as
~/day4/user_dirs.yaml:
Run the Playbook:
Verify Output:
Check if the directories are created for each user based on the variables provided.
Key Learning Points:
Templates and Variables:
Use Jinja2 to generate configurations dynamically based on variables and facts.
Conditional Logic:
Utilize conditionals in templates to handle different scenarios.
Advanced Scenarios:
Create reusable and complex configurations using loops and nested data structures.
Optional Challenges:
Add more variables, conditions, or loops to explore dynamic configurations further.
This lab introduces participants to the power of Jinja2 templates for managing dynamic configurations, a key skill in scalable automation.
Last updated