Defining & Using vars in a Play
Declare values and reference them.
Defining & Using vars in a Play is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, and your progress syncs across the web and the CoddyKit app. The DevOps Bootcamp course includes 4 lessons in total.
Why Variables Exist
Hardcoding values like a port or package name everywhere makes change painful. A variable names a value once so you can reuse it. 🌟
The vars Section
Inside a play you add a vars key holding a mapping of names to values. Everything under it becomes available to that play's tasks.
- hosts: web
vars:
http_port: 80
app_name: shop
tasks: []Naming Your Variables
Use lowercase letters, digits and underscores for variable names. A clear name like http_port beats a cryptic p1 every time.
Variables Hold Any Type
A value can be a string, number, boolean, list or dictionary. YAML decides the type from how you write it.
vars:
max_clients: 200
debug: true
packages:
- nginx
- gitReferencing a Variable
To use a value you wrap its name in double curly braces. Writing {{ http_port }} tells Ansible to substitute the stored value.
tasks:
- debug:
msg: "Port is {{ http_port }}"Variables Inside Module Args
You can drop a variable into any module argument, not just messages. Here the name argument is filled from a var.
tasks:
- apt:
name: "{{ app_name }}"
state: presentThe debug Module
The debug module prints a value during a run. It is your friend for checking what a variable actually holds.
- debug:
var: http_portvar vs msg in debug
Use var to print a variable's raw value, or msg to print a sentence with braces inside. They are two distinct styles.
- debug:
msg: "Serving {{ app_name }} on {{ http_port }}"vars_files for Longer Lists
When you have many values, move them to a separate file and point to it with vars_files. It keeps the play clean.
- hosts: web
vars_files:
- vars/main.yml
tasks: []vars_prompt for Input
Need a value at runtime? vars_prompt asks the operator a question and stores their answer as a variable.
vars_prompt:
- name: username
prompt: "Enter a username"Scope: Play-Level vars
Variables defined in a play's vars apply to every task in that play, but not to other plays in the same file.
Quick Check
How do you reference a variable's value inside a task?
Recap
You learned to declare values under vars, name them clearly, and pull them back with double curly braces. Variables make plays reusable. ✨
Learn DevOps Bootcamp with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 142
- Lessons
- 568
Frequently Asked Questions
Is the “Defining & Using vars in a Play” lesson free?
Yes — any 3 lessons of the DevOps Bootcamp learning path, including “Defining & Using vars in a Play”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The DevOps Bootcamp course includes 4 lessons in total.
What will I learn in “Defining & Using vars in a Play”?
Declare values and reference them. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “Defining & Using vars in a Play” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Defining & Using vars in a Play
- The {{ }} Substitution Syntax
- Extra Vars on the Command Line
- Where Variables Come From & Precedence