I am looking for a way to perform a task when ansible variable is not registers /undefined e.g
-- name: some task
command: sed -n '5p' "{{app.dirs.includes}}/BUILD.info" | awk '{print $2}'
when: (! deployed_revision) AND ( !deployed_revision.stdout )
register: deployed_revision
As per latest Ansible Version 2.5, to check if a variable is defined and depending upon this if you want to run any task, use
undefined
keyword.Ansible Documentation
From the ansible docs: If a required variable has not been set, you can skip or fail using Jinja2’s defined test. For example:
So in your case,
when: deployed_revision is not defined
should workStrictly stated you must check all of the following: defined, not empty AND not None.
For "normal" variables it makes a difference if defined and set or not set. See
foo
andbar
in the example below. Both are defined but onlyfoo
is set.On the other side registered variables are set to the result of the running command and vary from module to module. They are mostly json structures. You probably must check the subelement you're interested in. See
xyz
andxyz.msg
in the example below: