0Pricing
Ansible Academy · Lesson

The {{ }} Substitution Syntax

Inject variables into strings and args.

The {{ }} Substitution Syntax 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.

Meet Jinja2

The double curly braces come from Jinja2, the templating engine Ansible uses. Anything inside them is an expression to evaluate. 🌟

The Basic Substitution

At its simplest, {{ name }} is replaced with the value of name. Ansible evaluates it before the module runs.

msg: "Welcome, {{ name }}!"

Mind the Spaces

Spaces inside the braces are conventional and ignored: {{ name }} equals {{name}}. The readable, spaced form is the standard style.

Quote When It Starts a Value

If a value begins with braces, YAML needs quotes around the whole string. Otherwise it mistakes it for a dictionary.

name: "{{ app_name }}"

Combine Text and Variables

You can mix literal text with substitutions freely in one string, building up paths or messages piece by piece.

path: "/var/www/{{ site_name }}/index.html"

Access Dictionary Keys

For a dictionary variable, reach a key with dot or bracket notation. Both db.port and db['port'] return the same value.

msg: "DB on {{ db.host }}:{{ db.port }}"

Access List Items

Index into a list variable with brackets. Counting starts at zero, so packages[0] is the first element.

msg: "First package is {{ packages[0] }}"

Expressions, Not Just Names

Braces can hold real expressions: math, comparisons, even concatenation. Ansible evaluates the whole thing.

msg: "Total: {{ workers * 2 }}"

Filters Transform Values

A pipe applies a filter to reshape a value. Here upper turns the string into capitals before printing.

msg: "{{ region | upper }}"

Braces Only in Strings

Substitution lives in string values. A bare key like name still needs its value quoted when it leads with {{ }}.

Watch for Undefined

Referencing a name that was never set raises an undefined variable error. Define it first, or supply a default filter.

msg: "{{ tier | default('web') }}"

Quick Check

Why does name: {{ app_name }} need quotes?

Recap

You now substitute values with {{ }}, reach into lists and dictionaries, apply filters, and quote leading braces. ✨

Frequently asked questions

Is the “The {{ }} Substitution Syntax” lesson free?

Yes — the full text of “The {{ }} Substitution Syntax” 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 “The {{ }} Substitution Syntax”?

Inject variables into strings and args. 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 “The {{ }} Substitution Syntax” 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. Defining & Using vars in a Play
  2. The {{ }} Substitution Syntax
  3. Extra Vars on the Command Line
  4. Where Variables Come From & Precedence
← Back to Ansible Academy