Skip to content

Latest commit

 

History

History
79 lines (52 loc) · 1.3 KB

File metadata and controls

79 lines (52 loc) · 1.3 KB

Ansible Use Case: Basic Variable Usage in Playbook

Overview

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.


Project Structure

.
├── greeting_playbook.yml
└── output.txt

Implementation Steps

1. Create Playbook

vi 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 }}."

2. Run Playbook

ansible-playbook /home/user/greeting_playbook.yml > output.txt

Redirects the output to output.txt.


3. Verify Output

cat output.txt

Expected output:

Hello, Alice. You are a Developer.

Key Concepts

  • Defining variables using vars
  • Using Jinja2 templating ({{ }})
  • Debug module for output display
  • Output redirection to file

Summary

This lab demonstrates how Ansible variables can be used to create dynamic and customizable playbooks, improving flexibility and reusability.