Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tasks inside a block that use a loop are not skipped even if the block's when condition evaluates to false #64883

Closed
lpugoy opened this issue Nov 15, 2019 · 4 comments
Labels
affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@lpugoy
Copy link

lpugoy commented Nov 15, 2019

SUMMARY

Tasks inside a block that use a loop are not skipped even if the block's when condition evaluates to false. Tasks that don't use a loop are skipped.

ISSUE TYPE
  • Bug Report
COMPONENT NAME

Loops

ANSIBLE VERSION
ansible 2.9.1
  config file = /<scrubbed>/src/github.com/<scrubbed>/ansible.cfg
  configured module search path = ['/<scrubbed>/src/github.com/<scrubbed>/library']
  ansible python module location = /<scrubbed>/.virtualenvs/ansible/lib/python3.7/site-packages/ansible
  executable location = /<scrubbed>/.virtualenvs/ansible/bin/ansible
  python version = 3.7.5 (default, Nov  1 2019, 02:16:32) [Clang 11.0.0 (clang-1100.0.33.8)]
CONFIGURATION
DEFAULT_MODULE_PATH(/<scrubbed>/src/github.com/<scrubbed>/ansible.cfg) = ['/<scrubbed>/src/github.com/<scrubbed>/library']
DEFAULT_ROLES_PATH(/<scrubbed>/src/github.com/<scrubbed>/ansible.cfg) = ['/<scrubbed>/src/github.com/<scrubbed>']
INTERPRETER_PYTHON(/<scrubbed>/src/github.com/<scrubbed>/ansible.cfg) = /usr/bin/python3
OS / ENVIRONMENT

OS version: macOS Mojave 10.14.6

STEPS TO REPRODUCE
- hosts: all
  gather_facts: no
  tasks:
  - name: Initialize variable
    set_fact:
      foo:

  - name: Print result of "foo is not none"
    debug:
      msg: "{{ foo is not none }}"

  - block:
    - name: This task is skipped
      debug:
        msg: I'm skipped :)

    - name: This task that uses loop is not skipped
      debug:
        msg: I'm not skipped :(
      loop: "{{ foo | dict2items }}"
    when: foo is not none
EXPECTED RESULTS
$ ansible-playbook -i localhost, /tmp/loop_bug.yml

PLAY [all] *************************************************************************************************************************************************************************************************

TASK [Initialize variable] *********************************************************************************************************************************************************************************
ok: [localhost]

TASK [Print result of "foo is not none"] *******************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": false
}

TASK [This task is skipped] ********************************************************************************************************************************************************************************
skipping: [localhost]

TASK [This task that uses loop is not skipped] *************************************************************************************************************************************************************
skipping: [localhost]

PLAY RECAP *************************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0
ACTUAL RESULTS
$ ansible-playbook -i localhost, /tmp/loop_bug.yml

PLAY [all] *************************************************************************************************************************************************************************************************

TASK [Initialize variable] *********************************************************************************************************************************************************************************
ok: [localhost]

TASK [Print result of "foo is not none"] *******************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": false
}

TASK [This task is skipped] ********************************************************************************************************************************************************************************
skipping: [localhost]

TASK [This task that uses loop is not skipped] *************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "dict2items requires a dictionary, got <class 'NoneType'> instead."}

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

@ansibot
Copy link
Contributor

ansibot commented Nov 15, 2019

Files identified in the description:

If these files are inaccurate, please update the component name section of the description or use the !component bot command.

click here for bot help

@ansibot ansibot added affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. python3 support:community This issue/PR relates to code supported by the Ansible community. labels Nov 15, 2019
@Akasurde
Copy link
Member

!component =lib/ansible/executor/task_executor.py

@ansibot
Copy link
Contributor

ansibot commented Nov 15, 2019

Files identified in the description:

If these files are inaccurate, please update the component name section of the description or use the !component bot command.

click here for bot help

@ansibot ansibot added support:core This issue/PR relates to code supported by the Ansible Engineering Team. and removed support:community This issue/PR relates to code supported by the Ansible community. labels Nov 15, 2019
@sivel
Copy link
Member

sivel commented Nov 15, 2019

This is expected. A when statement on a block does not prevent the block from being evaluated, instead, think of any keyword that is applied to a block, being merged down onto the underlying tasks.

So this:

  - block:
    - name: This task is skipped
      debug:
        msg: I'm skipped :)

    - name: This task that uses loop is not skipped
      debug:
        msg: I'm not skipped :(
      loop: "{{ foo | dict2items }}"
    when: foo is not none

Is equivalent to:

  - name: This task is skipped
    debug:
      msg: I'm skipped :)
    when: foo is not none

  - name: This task that uses loop is not skipped
    debug:
      msg: I'm not skipped :(
    loop: "{{ foo | dict2items }}"
    when: foo is not none

Per the documentation at https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#loops-and-conditionals :

Combining when with loops (see Loops), be aware that the when statement is processed separately for each item. This is by design.

The meaning of that, is that the when statement is evaluated for each iteration of the loop, and not before the loop. As a result, your loop must always evaluate as an iterable, regardless of the when statement.

As such, you would need to change your loop to read something like the following:

    loop: "{{ foo | default({}) | dict2items }}"

If you have further questions please stop by IRC or the mailing list:

@sivel sivel closed this as completed Nov 15, 2019
@sivel sivel removed the needs_triage Needs a first human triage before being processed. label Nov 18, 2019
@ansible ansible locked and limited conversation to collaborators Dec 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. python3 support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

No branches or pull requests

4 participants