Variables & Expressions in Jinja2
Inject values into the template.
Variables & Expressions in Jinja2 is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 2 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.
Variables Inside Templates
Every variable available to your play is also available in the template. Just wrap its name in {{ }} to print the value. 🌟
server_name {{ domain }};
worker_processes {{ cpu_count }};Accessing Dictionary Keys
For a dictionary variable, reach a key with dot or bracket notation. Both db.host and db['host'] return the same value.
host = {{ db.host }}
port = {{ db['port'] }}Indexing a List
Pick one item from a list by its position, counting from zero. {{ servers[0] }} gives you the first entry.
primary = {{ servers[0] }}
backup = {{ servers[1] }}Using Ansible Facts
Gathered facts are just variables, so templates can read them too. ansible_hostname drops the host's own name into the file.
# This host is {{ ansible_hostname }}
bind {{ ansible_default_ipv4.address }}Math in Expressions
Jinja2 does arithmetic inside the braces. You can compute a value rather than hardcode it, like doubling a worker count.
max_workers {{ cpu_count * 2 }}String Concatenation
Join strings with the ~ operator. It glues pieces together regardless of their type, unlike plus which is numeric.
url = http://{{ host ~ ':' ~ port }}Comparison Expressions
Expressions can also test things. Comparisons like cpu_count > 4 return true or false for use later in logic.
{{ http_port == 443 }}
{{ replicas >= 3 }}Comments in Templates
Wrap notes for yourself in {# ... #}. Jinja2 strips these out entirely, so they never appear in the rendered file.
{# This file is managed by Ansible #}
listen {{ http_port }};Undefined Variables Fail
By default, referencing a variable that does not exist stops the play with an undefined variable error. Jinja2 is strict on purpose.
Quote Strings Carefully
A literal string inside an expression needs quotes. Without them Jinja2 treats prod as a variable name, not the text prod.
{{ 'production' if env == 'prod' else 'staging' }}Three Brace Flavors
Remember the trio: {{ }} prints a value, {% %} runs logic, and {# #} holds a comment. Each has its own job.
Quick Check
You want a template to output the value stored in a variable. Which delimiter prints it?
Recap
You can now read variables, facts, dict keys and list items in templates, do math, join strings and add comments. Expressions bring config to life. ✨
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 “Variables & Expressions in Jinja2” lesson free?
Yes — any 3 lessons of the DevOps Bootcamp learning path, including “Variables & Expressions in Jinja2”, 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 “Variables & Expressions in Jinja2”?
Inject values into the template. 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 2 of 4.
How long does the “Variables & Expressions in Jinja2” 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
- The template Module & .j2 Files
- Variables & Expressions in Jinja2
- Loops & if Blocks in Templates
- Filters: default, join & upper