However, I later found out that jinja templates {{}} or {%%} are not allowed/recommended in the when condition.
This is because the arguments to when
are evaluated in an implicit template context. In other words, write exactly what you would write inside {{...}}
markers, but you don't need the markers because the context is intrinsic to the command.
In other words, instead of:
when: {{ home_directory}}/var_name['var_key'] != command_output['stdout']
Write:
when: home_directory ~ "/" ~ var_name['var-key'] != command_output['stdout']
Where ~
is the Jinja string concatenation operator.
We can simplify that a bit:
when: "%s/%s" % (home_directory, var_name.var_key) != command_output.stdout
This takes advantage of Linux string formatting syntax to substitute variables into a string.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…