Templating and facts reference

on this page

Placeholders

A {{ expression }} placeholder renders inside task names, when conditions, and parameter values at any depth of nested maps and lists.

text
{{ vars.content_root }}
{{ facts.os.arch }}
{{ target.name }}
{{ env.COMPUTERNAME }}

Expressions use expr syntax with these Preflight rules:

SyntaxBehavior
a.b.cDot-path lookup in a namespace
==, !=Compare the string forms of both operands
>, >=, <, <=Compare numerically; a non-numeric operand is an error
cond ? a : bTernary; cond uses the truthy rules in when conditions
&&, ||, !Boolean logic
'text'String literal
secret("name")Secret contents; valid only in file content_template
toJSON(value)JSON text of a value, such as a list input passed into a script
yaml
when: "{{ vars.theme_mode != 'unchanged' }}"
data: "{{ vars.theme_mode == 'light' }}"

A rendered value that contains another placeholder is rendered again, up to 16 passes.

When a parameter value is exactly one placeholder and the result is a map or list, the value keeps that type. Other results render as strings.

Namespaces

NamespaceContentsUnknown key
varsMerged variablesError: undefined variable "vars.<path>"
factsGathered target factsEmpty string
targetTarget connection metadataEmpty string
envTarget environment variablesEmpty string

Any other first segment renders as an empty string.

During plan, placeholders that reference unknown facts, target, or env keys stay unrendered, including comparisons and ternaries that read them. check and apply render them against the live target. Staged bundles keep them for apply time.

Variables

vars holds the merged project, inventory, group, host, playbook, and --var layers. See variable merge order.

Inside an action, vars also holds the action’s inputs: input defaults overlaid by the task’s with values. The overlay is shallow; a with map replaces a variable of the same name.

Built-in preflight variables

VariableValue
vars.preflight.projectproject from preflight.yml
vars.preflight.environmentenvironment from preflight.yml

Playbook vars and --var can override these; inventory, group, and host vars cannot.

when conditions

when renders its template, trims the result, and lowercases it. These results are false and skip the task:

  • empty string
  • false
  • 0
  • no

Any other result is true.

yaml
when: "{{ vars.computer_name }}"
when: "{{ facts.os.build >= 22000 }}"

Secrets in templates

A string that is exactly secret:<name> resolves in any parameter; see Secret references.

secret("name") works only in the content_template parameter of the file module, which renders after all other parameters. Anywhere else it fails with secret lookup is not configured.

Facts

preflight facts prints these values, and check and apply gather them for templates. Gathering is best-effort: a failed probe logs a warning and leaves its fields empty.

FieldDescription
facts.os.familywindows or unknown
facts.os.nameOS product name
facts.os.versionOS version string
facts.os.buildBuild number
facts.os.archArchitecture
facts.os.hostnameHost name reported by the OS
facts.hostnameHost name
facts.disksList of disks
facts.envEnvironment variables

Each facts.disks entry:

FieldDescription
pathDrive path
total_gbCapacity in GB
free_gbFree space in GB
used_gbUsed space in GB

Target metadata

target holds connection metadata and no credentials.

FieldDescription
target.nameInventory host name
target.hostnameInventory host name
target.addressaddress, or the host name when address is unset
target.transportlocal, ssh, or winrm
target.portConnection port

For the local machine, name, hostname, and address are localhost, transport is local, and port is unset.