1

I have two identical tasks in the same playbook:

  1. when: var == "true"
  2. when: var == "false"

Both tasks are using register: result, but first one fails and second one succeeds.

I tried using block: and not just when: and the behaviour is the same.

bug-when.yml

---

- hosts: localhost
  tasks:
    - name: when true
      debug:
        msg: "this is true"
      register: result
      when: var == "true"

     - name: when false
       debug:
         msg: "this is false"
       register: result
       when: var == "false"

     - name: print result
       debug:
         msg: "{{ result }}"

Example running it:

ansible-playbook bug-when.yml -e var=true


PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
Thursday 09 May 2019  18:51:35 +0000 (0:00:02.018)       0:00:02.018 **********
ok: [localhost]

TASK [when true] ***************************************************************
Thursday 09 May 2019  18:51:35 +0000 (0:00:00.437)       0:00:02.456 **********
ok: [localhost] => {
    "msg": "this is true"
}

TASK [when false] **************************************************************
Thursday 09 May 2019  18:51:35 +0000 (0:00:00.027)       0:00:02.483 **********
skipping: [localhost]

TASK [print result] ************************************************************
Thursday 09 May 2019  18:51:36 +0000 (0:00:00.023)       0:00:02.506 **********
ok: [localhost] => {
    "msg": {
        "changed": false,
        "skip_reason": "Conditional check failed",
        "skipped": true
    }
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0

Second example running it:

ansible-playbook bug-when.yml -e var=false


PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
Thursday 09 May 2019  18:52:01 +0000 (0:00:02.019)       0:00:02.019 **********
ok: [localhost]

TASK [when true] ***************************************************************
Thursday 09 May 2019  18:52:01 +0000 (0:00:00.453)       0:00:02.472 **********
skipping: [localhost]

TASK [when false] **************************************************************
Thursday 09 May 2019  18:52:01 +0000 (0:00:00.024)       0:00:02.497 **********
ok: [localhost] => {
    "msg": "this is false"
}

TASK [print result] ************************************************************
Thursday 09 May 2019  18:52:02 +0000 (0:00:00.028)       0:00:02.525 **********
ok: [localhost] => {
    "msg": {
        "changed": false,
        "msg": "this is false"
    }
}

PLAY RECAP *********************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0
2
  • What do you mean "the first one fails"? How does it fail? Is there an error message?
    – larsks
    May 9, 2019 at 19:49
  • This happens because the task is always registered, no matter if it pass or if it is skipped. So your first trial cannot print the result on a skipped task. May 9, 2019 at 20:13

2 Answers 2

4

To expand upon what @b.enoit.be said:

When you have a task like this:

- name: some task
  debug:
    msg: this is an example
  when: false
  register: result

This will update result even if the task is skipped. This is what permits you, in a subsequent task, to see if this task was skipped or not:

- name: check if task was skipped
  debug:
    msg: previous task was skipped
  when: result is skipped

Consider registering a different variable in each task, and then:

- name: when true
  debug:
    msg: "this is true"
  register: result1
  when: var == "true"

 - name: when false
   debug:
     msg: "this is false"
   register: result2
   when: var == "false"

 - name: print result
   debug:
     msg: "{{ result1.msg if result2 is skipped else result2.msg }}"
2
  • Ah! your is skipped is way nicer than my result.skipped is defined and result.skipped, kuddos May 9, 2019 at 20:35
  • 1
    Glad I stumbled on this. Its strange how this technique of testing task status is not used in any of the blogs or SO answers for ansible !! This lead me to thinking well then there must be a is success/fail check just like is skipped. Sure enough there is one. just that they are "is succeeded" and "is failed" doc --> docs.ansible.com/ansible/latest/user_guide/… Jun 19, 2022 at 23:20
1

The behaviour you see here happens because the tasks are always registered, mostly because you can self reference a task register in itself, and this behaviour would fail if the tasks was not always registering itself.

So what you need to do is to have two different register handle and act upon the result and skipped attributes of them to display your message properly.

Here is the playbook:

---
- hosts: localhost
  tasks:
    - name: when true
      debug:
        msg: "this is true"
      register: result_is_true
      when: var == "true"

    - name: when false
      debug:
        msg: "this is false"
      register: result_is_false
      when: var == "false"

    - name: print result
      debug:
        msg: "{{ result_is_true if result_is_false is skipped else result_is_false }}"

Here is the run when var is true

$ ansible-playbook so.yml -e var=true

PLAY [localhost] *******************************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************
ok: [host1]

TASK [when true] *******************************************************************************************************************************************************************************************
ok: [host1] => {
    "msg": "this is true"
}

TASK [when false] ******************************************************************************************************************************************************************************************
skipping: [host1]

TASK [print result] ****************************************************************************************************************************************************************************************
ok: [host1] => {
    "msg": {
        "changed": false,
        "failed": false,
        "msg": "this is true"
    }
}

PLAY RECAP *************************************************************************************************************************************************************************************************
host1                      : ok=3    changed=0    unreachable=0    failed=0  

And here is the result when var is false

$ ansible-playbook so.yml -e var=false

PLAY [localhost] *******************************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************
ok: [host1]

TASK [when true] *******************************************************************************************************************************************************************************************
skipping: [host1]

TASK [when false] ******************************************************************************************************************************************************************************************
ok: [host1] => {
    "msg": "this is false"
}

TASK [print result] ****************************************************************************************************************************************************************************************
ok: [host1] => {
    "msg": {
        "changed": false,
        "failed": false,
        "msg": "this is false"
    }
}

PLAY RECAP *************************************************************************************************************************************************************************************************
host1                      : ok=3    changed=0    unreachable=0    failed=0  

Needless to say: I would guess you simplified your issue for a MCVE, but your play can actually be as simple as

---
- hosts: localhost
  tasks:
    - name: print result
      debug:
        msg: "{{ 'this is true' if var == true else 'this is false' }}"

Which runs:

$ ansible-playbook so.yml -e var=false

PLAY [localhost] ***********************************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************************************
ok: [host1]


TASK [print result] ********************************************************************************************************************************************************************************
ok: [host1] => {
    "msg": "this is false"
}

PLAY RECAP *****************************************************************************************************************************************************************************************
host1                      : ok=2    changed=0    unreachable=0    failed=0   


$ ansible-playbook so.yml -e var=true

PLAY [localhost] ***********************************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************************************

ok: [host1]

TASK [print result] ********************************************************************************************************************************************************************************
ok: [host1] => {
    "msg": "this is true"
}

PLAY RECAP *****************************************************************************************************************************************************************************************
host1                      : ok=2    changed=0    unreachable=0    failed=0  

And for reference here is a jinja inline if-expression question: https://stackoverflow.com/a/14215034/2123530

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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