11-Day4-3-Jinja2Template
Day 4: Lab Series 3 - Dynamic Configurations with Jinja2 Templates
Lab 3.1: Basic Jinja2 Template for Configuration
server { listen 80; server_name {{ inventory_hostname }}; root /var/www/{{ web_root }}; location / { index index.html; } }
- 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
ansible-playbook ~/day4/nginx_config.yaml
Lab 3.2: Using Host Variables with Templates
Lab 3.3: Conditional Rendering in Templates
Optional Lab: Nested Loops and Templates
Key Learning Points:
Last updated