Ansible: when conditional is ignored if with_items contains undefined value

Created on 10 Jan 2016  路  1Comment  路  Source: ansible/ansible

Issue Type:

Bug Report

Ansible Version:
ansible 2.1.0
  config file =
  configured module search path = Default w/o overrides
Environment:

Ubuntu 14.04

Summary:

A task's with_items seem to be evaluated before its when clause.
If a variable used within an item is undefined, the task will fail even if the when clause says it should only run when that same variable is defined.

Steps To Reproduce:
- hosts: localhost
  tasks:
  - name: Copy test file
    copy:
      src: "{{ item.src }}"
      dest: "{{ item.dest }}"
      owner: "root"
      group: "root"
      mode: "{{ item.mode }}"
    with_items:
      - { src: "{{ test_src }}", dest: "/tmp/test", mode: "0644" }
    when: test_src is defined
Expected Results:

Task is skipped since test_src is undefined

Worked as expected in Ansible 1.9.4

ansible 1.9.4
  configured module search path = None

TASK: [Copy test file] ********************************************************
skipping: [localhost] => (item={'dest': '/tmp/test', 'src': u'{# test_src #}', 'mode': '0644'})
Actual Results:
TASK [Copy test file] **********************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "ERROR! 'test_src' is undefined"}

Most helpful comment

Possible Misunderstanding

Hi!

Thanks very much for your submission to Ansible. It sincerely means a lot to us.

We believe the ticket you have filed is being somewhat misunderstood, as one thing works a little differently than stated.

with_ clauses ALWAYS evaluate before the when clause, otherwise we cannot use item and condition loop iterations. So this is not a bug, just how ansible works. The default and ternary filters can be used to avoid the undefined error.

In the future, this might be a topic more well suited for the user list, which you can also post here if you'd like some more help with the above.

Thank you once again for this and your interest in Ansible!

>All comments

Possible Misunderstanding

Hi!

Thanks very much for your submission to Ansible. It sincerely means a lot to us.

We believe the ticket you have filed is being somewhat misunderstood, as one thing works a little differently than stated.

with_ clauses ALWAYS evaluate before the when clause, otherwise we cannot use item and condition loop iterations. So this is not a bug, just how ansible works. The default and ternary filters can be used to avoid the undefined error.

In the future, this might be a topic more well suited for the user list, which you can also post here if you'd like some more help with the above.

Thank you once again for this and your interest in Ansible!

Was this page helpful?
0 / 5 - 0 ratings