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

when conditional is ignored if with_items contains undefined value #13791

Closed
jimmymccrory opened this issue Jan 10, 2016 · 1 comment
Closed

Comments

@jimmymccrory
Copy link
Contributor

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"}
@bcoca
Copy link
Member

bcoca commented Jan 11, 2016

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!

@bcoca bcoca closed this as completed Jan 11, 2016
evrardjp added a commit to evrardjp/ansible-haproxy that referenced this issue Feb 11, 2016
Issues were listed here:
ansible/ansible#14383
and here:
ansible/ansible#13791

But at the end, it's normal ansible behaviour and ansible 1.9
was too kind with that.

This should avoid all the problems.
orviz pushed a commit to egi-qc/ansible-release-candidate that referenced this issue Jun 13, 2017
@ansible ansible locked and limited conversation to collaborators Apr 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants