162

I am looking for a way to perform a task when Ansible variable is not registers or 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

4 Answers 4

273

From the ansible documentation:

If a required variable has not been set, you can skip or fail using Jinja2’s defined test. For example:

tasks:
    - name: Run the command if "foo" is defined
      ansible.builtin.shell: echo "I've got '{{ foo }}' and am not afraid to use it!"
      when: foo is defined

    - name: Fail if "bar" is undefined
      ansible.builtin.fail: msg="Bailing out. This play requires 'bar'"
      when: bar is undefined

So, in your case, when: deployed_revision is not defined should work.

6
  • 4
    thanks this worked for me when: deployed_revision is not defined or deployed_revision.stdout is not defined or deployed_revision.stdout == ''
    – sakhunzai
    May 8, 2015 at 9:46
  • 6
    You can also combine it with different conditions: when: item.sudo is defined and item.sudo == true
    – czerasz
    Feb 11, 2016 at 10:23
  • 7
    Don't do what I did and put curly brackets around the foo in when: foo is defined (eg this doesn't work: when: {{ foo }} is defined
    – David
    Aug 15, 2016 at 1:59
  • 3
    @David I faced the same issue as you. putting curly braces in when breaks the conditional. To get this working you need to add parenthesis around the conditional. e.g when: ({{ foo }} in undefined)
    – Tarun
    Sep 29, 2016 at 13:27
  • 9
    The usage of curly braces for conditionals in Ansible is deprecated. Also, no Ansible statement can start with variable expansion (like {{ foo }}). This is not because of Ansible, but Yaml would interpret this as an object. If you need to start with a variable expansion, simply surround the whole thing with double quotes (like "{{ foo }}"), to force Yaml to see it as a string and pass it as-is to Ansible. Apr 26, 2017 at 9:51
25

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.

tasks:
    - shell: echo "I've got '{{ foo }}' and am not afraid to use it!"
      when: foo is defined

    - fail: msg="Bailing out. this play requires 'bar'"
      when: bar is undefined

Ansible Documentation

0
11

Strictly 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 and bar in the example below. Both are defined but only foo 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 and xyz.msg in the example below:

cat > test.yml <<EOF
- hosts: 127.0.0.1

  vars:
    foo: ""          # foo is defined and foo == '' and foo != None
    bar:             # bar is defined and bar != '' and bar == None

  tasks:

  - debug:
      msg : ""
    register: xyz    # xyz is defined and xyz != '' and xyz != None
                     # xyz.msg is defined and xyz.msg == '' and xyz.msg != None

  - debug:
      msg: "foo is defined and foo == '' and foo != None"
    when: foo is defined and foo == '' and foo != None

  - debug:
      msg: "bar is defined and bar != '' and bar == None"
    when: bar is defined and bar != '' and bar == None

  - debug:
      msg: "xyz is defined and xyz != '' and xyz != None"
    when: xyz is defined and xyz != '' and xyz != None
  - debug:
      msg: "{{ xyz }}"

  - debug:
      msg: "xyz.msg is defined and xyz.msg == '' and xyz.msg != None"
    when: xyz.msg is defined and xyz.msg == '' and xyz.msg != None
  - debug:
      msg: "{{ xyz.msg }}"
EOF
ansible-playbook -v test.yml
-3

You can check if ansible variable is empty or not using this code.

tasks:

- fail: msg="The variable 'bar' is empty"
  when: bar|length == 0

- shell: echo "The variable 'foo' is not empty: '{{ foo }}'"
  when: foo|length > 0

I hope it is helpful for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.