Defining & Using vars in a Play
Declare values and reference them.
Defining & Using vars in a Play is a free Ansible Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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. ✨
Frequently asked questions
Is the “Defining & Using vars in a Play” lesson free?
Yes — the full text of “Defining & Using vars in a Play” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.
What will I learn in “Defining & Using vars in a Play”?
Declare values and reference them. You practise Ansible Academy 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 Ansible Academy?
No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
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 Ansible Academy lesson?
Yes. Every Ansible Academy 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