CertGrid CertGrid
Hands-on Lab·Ansible

Ansible Conditionals and Jinja Expressions

`when:` decides whether a task runs, per host and per loop item. It takes a Jinja2 expression **without** the usual braces, which is the source of the best-known mistake in Ansible - and one that current versions handle differently from every tutorial written about it. This covers the three things you branch on and the quoting rule.

Plays and Playbooks Guide 14 of 45 Intermediate

Written against the versions above. `when: "{{ var }}"` was historically always true, because the braces rendered to a non-empty string. On ansible-core 2.20 it evaluates correctly **and** emits a deprecation warning: conditionals surrounded by templating delimiters are removed in 2.23. So the old silent bug is now a loud warning, and the advice is unchanged - write the expression bare.

Both managed nodes, in different groups, so a condition on group membership has something to distinguish.
Server NameIP AddressOSRolesCPURAMHDD
ANS-CTL01192.168.0.36Ubuntu 26.04 LTSAnsible Control Node2 Core3 GB50 GB
ANS-A01192.168.0.37Ubuntu 26.04 LTSManaged Node (group: web)2 Core3 GB50 GB
ANS-B01192.168.0.38Ubuntu 26.04 LTSManaged Node (group: db)2 Core3 GB50 GB

Before you start

  1. The configuration this guide assumes

  2. when against a fact

  3. when against something you registered

  4. Combining conditions

  5. The braces to avoid

Official sources