0Pricing
Ansible Academy · Lesson

Variables & Expressions in Jinja2

Inject values into the template.

Variables & Expressions in Jinja2 is a free Ansible Academy lesson on CoddyKit — lesson 2 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.

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. ✨

Frequently asked questions

Is the “Variables & Expressions in Jinja2” lesson free?

Yes — the full text of “Variables & Expressions in Jinja2” 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 “Variables & Expressions in Jinja2”?

Inject values into the template. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

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 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

  1. The template Module & .j2 Files
  2. Variables & Expressions in Jinja2
  3. Loops & if Blocks in Templates
  4. Filters: default, join & upper
← Back to Ansible Academy