1

I have following play book:

test2.yml:

- hosts: localhost
  connection: local
  gather_facts: false

  vars:
    dict1:
      v1:
        - 111
        - 222
        - 333
      v2:

    ver: "{{ ver }}"

  tasks:
    - name: Gather list
      set_fact: rblist="{{ pitem }}"
      with_dict: "{{ dict1 }}"
      when: "pitem.key in ver"
      loop_control:
       loop_var: pitem
      register: plist

    - name: lets include the task if the value is not empty
      include: test3.yml
      when: rblist.value

test3.yml:

---
  - name: display value if not empty
    debug: msg={{ rblist.value }}

I'd like to invoke test3.yml only when rblist.value is not empty, but its ignoring that and invokes the task even when the value is empty.

When the value is not empty: it works fine

ansible-playbook test2.yml -e "ver=v1"

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

TASK [Gather list] **********************************************************************************************************************************************************************************************************************
ok: [localhost] => (item={u'key': u'v1', u'value': [111, 222, 333]})

TASK [display value if not empty] *******************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        111, 
        222, 
        333
    ]
}

PLAY RECAP ******************************************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

When the value is empty: seems to behave same as when the list

ansible-playbook test2.yml -e "ver=v2"

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

TASK [Gather list] **********************************************************************************************************************************************************************************************************************
ok: [localhost] => (item={u'key': u'v2', u'value': None})

TASK [display value if not empty] *******************************************************************************************************************************************************************************************************

PLAY RECAP ******************************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   

Following How to test that a registered variable is not empty? and other solutions, I have tried the following

when: rblist.value | length > 0

I get this:

TASK [display value if not empty] *******************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The conditional check 'rblist.value | length > 0' failed. The error was: Unexpected templating type error occurred on ({% if rblist.value | length > 0 %} True {% else %} False {% endif %}): object of type 'NoneType' has no len()\n\nThe error appears to be in '/stage/ap/ansible/test/test3.yml': line 2, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n  - name: display value if not empty\n    ^ here\n"}

When I try this:

when: rblist.value != None

I get this:

ansible-playbook test2.yml -e "ver=v2"

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

TASK [Gather list] **********************************************************************************************************************************************************************************************************************
ok: [localhost] => (item={u'key': u'v2', u'value': None})

TASK [display value if not empty] *******************************************************************************************************************************************************************************************************

PLAY RECAP ******************************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   

I Tried this:

when: rblist.value != ""

Same results as above that is when used != None

0

1 Answer 1

1

You probably have

display_skipped_hosts = false

in your ansible.cfg file.

Where display_skipped_hosts:

Toggle to control displaying skipped task/host entries in a task in the default callback

Source: https://docs.ansible.com/ansible/2.9/reference_appendices/config.html#display-skipped-hosts

With this option set this way, you will not see skipped host in the play itself, but you can see them in your recap:

PLAY RECAP ********************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0 

Where the important part is skipped=1.

You will thus still see the task description, but nothing under it, confirming that the task was skipped.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you! I believe the issue is not about showing a skipped task, but that is getting executed where it is not supposed to at all in the first place.
Well that's how Ansible is working, even if all the hosts are skipped for a task, it will still display the task has run, which is true, the task has run but skipped all hosts.
Ok. I think my understanding may be wrong or I am expecting something different. For me the fact that a task which is a part of the yml which is being included is executed hence its has already executed for all practical purposes is my thought. Again I may be thinking wrong. When the list is empty I dont want this to get executed in the first place: TASK [display value if not empty]
The output tells you your playbook reached TASK [display value if not empty]. But since all hosts are skipped, it does not show any results. So practically, it was not executed, exactly as you requested. Now if you don't want ansible to display the task title at all when it is not executed for any host.... if ever possible (which I don't know), this will probably require you to use a different callback plugin or even write your own. What exactly are you trying to solve by not displaying this line ?
You are right. as @β.εηοιτ.βε said I need to be looking at skipped. Thanks once again for looking into this.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.