Module Description: dgt_devops06 – Ansible – Vars, Loops, When, and Templating
“dgt_devops06 – Ansible – Vars, Loops, When, and Templating” is a module on configuration management with Ansible. It focuses on the advanced Ansible features that make automation dynamic and reusable across different environments.
Module Overview:
-
Introduction to Configuration Management:
Explore the fundamentals of configuration management and its significance in maintaining consistency, reliability and efficiency within IT infrastructure. Understand how Ansible fits into this paradigm as a tool for automating repetitive tasks. -
Idempotency Principles:
Learn about idempotency, which ensures that running the same playbook multiple times produces identical results without causing unintended changes. This principle is crucial for keeping systems stable and consistent across deployments. -
Using Variables (Vars):
Study how Ansible manages variables to customize playbooks for different environments: where they are defined, how they are used and in which order they override each other. This is what lets you tell the machine which values must change per environment and check that it placed them correctly. -
Using Loops:
Understand how loops iterate over lists or dictionaries within playbooks, removing redundant code and keeping automation clean and maintainable. You will learn to recognize when the machine should use a loop instead of repeating tasks, and to ask for it. -
Conditional Execution with When:
Discover how thewhenstatement executes tasks conditionally based on specific criteria. This adds conditional logic that adapts a playbook to different scenarios or states of the target systems, and gives you a precise vocabulary for describing those conditions to the machine. -
Templating with Jinja2:
Study templating through Ansible’s integration with Jinja2, which generates dynamic content in configuration files and scripts. Learn how variables, loops and conditionals are used inside templates, so that you can read and validate every template the machine renders.
Natural-Language Practice:
Throughout the module, the theory is reinforced by exercises based on real-world scenarios. You won’t write the playbooks by hand: you’ll explain to the machine, in natural language, what each environment needs, which values vary, what must be repeated and under which conditions, and let it write and run playbooks that use vars, loops, when conditions and templates. Then you review its work: you check variable precedence, verify that the conditions match your intent, inspect the rendered configuration files and run the playbook again to confirm that it is idempotent.
Example Snippet of an Ansible Playbook:
“`yaml
- name: Configure web servers with dynamic settings
hosts: webservers
vars:
http_port: 80
max_clients: 200
tasks:
– name: Ensure the Apache package is installed
apt:
name: apache2
state: present
- name: Template the Apache configuration file
template:
src: templates/httpd.conf.j2
dest: /etc/apache2/sites-available/000-default.conf
notify:
- Restart Apache
handlers:
– name: Restart Apache
service:
name: apache2
state: restarted
“`
In this example, the variables http_port and max_clients are used within a template to configure an Apache web server dynamically. The playbook shows how tasks, variable management, templating and handlers for service restarts work together; this is exactly the kind of output the machine returns and that you must be able to read, verify and, where needed, ask it to fix.
Target Audience:
- IT professionals working on configuration management.
- DevOps engineers who automate with Ansible.
- System administrators who automate infrastructure management.
The module covers Ansible vars, loops, when conditions and templating, starting from the core principles of configuration management and idempotency.
The students can push their exercises to the Academy DevOps & SRE Git project. For this module, create a folder with your username as its name in the following subfolder: https://github.com/Garanti-Del-Talento/gdt_academy
