This lab demonstrates how to use variables in an Ansible playbook to generate dynamic output. It focuses on defining variables and using them within tasks using Jinja2 templating.
.
├── greeting_playbook.yml
└── output.txtvi greeting_playbook.yml---
- name: Print Customized Greeting Message
hosts: localhost
vars:
user_name: Alice
user_role: Developer
tasks:
- name: Output Greeting
debug:
msg: "Hello, {{ user_name }}. You are a {{ user_role }}."ansible-playbook /home/user/greeting_playbook.yml > output.txtRedirects the output to output.txt.
cat output.txtExpected output:
Hello, Alice. You are a Developer.
- Defining variables using
vars - Using Jinja2 templating (
{{ }}) - Debug module for output display
- Output redirection to file
This lab demonstrates how Ansible variables can be used to create dynamic and customizable playbooks, improving flexibility and reusability.