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

Error in a skipped test because of a non-existing variable #15683

Closed
ogmueller opened this issue May 2, 2016 · 4 comments
Closed

Error in a skipped test because of a non-existing variable #15683

ogmueller opened this issue May 2, 2016 · 4 comments

Comments

@ogmueller
Copy link

ISSUE TYPE
  • Bug Report
ANSIBLE VERSION

ubuntu 16.04 package:

ansible 2.0.0.2
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides

CONFIGURATION

i don't know about any changes

OS / ENVIRONMENT

ubuntu 16.04

SUMMARY

when using an undefined variable in a skipped test, an error will occur when used in "with_items" only.

STEPS TO REPRODUCE

i have a playbook, which includes a role under certain circumstances. for this showcase, i put it to false. so basically this role nor any of its tasks should get executed. the problem is the "with_items" task, which seems to get evaluated even though the task is skipped.

PLAYBOOK:


---
- hosts: all
  strategy: free
  roles:
    - { role: test, when: false }

ROLE


---
- name: variable in when clause
  debug:
    msg: "works"
  when: ansible_local.something is undefined

- name: variable in task
  debug:
    msg: "works {{ ansible_local.something }}"

- name: Test in items
  debug:
    msg: "works {{ item }}"
  with_items:
    - "item with variable {{ ansible_local.something }}"
EXPECTED RESULTS

I would expect, that non of the roles or tasks gets executed or evaluated at all. so no error should occur, no matter if a variable exists or not.

ACTUAL RESULTS
# ansible-playbook /etc/ansible/test.yml -vvvv
Using /etc/ansible/ansible.cfg as config file
Loaded callback default of type stdout, v2.0
1 plays in /etc/ansible/test.yml

PLAY ***************************************************************************

TASK [setup] *******************************************************************
<192.168.1.190> ESTABLISH SSH CONNECTION FOR USER: None
<192.168.1.190> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o Port=1221 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r -tt 192.168.1.190 '( umask 22 && mkdir -p "$( echo $HOME/.ansible/tmp/ansible-tmp-1462190379.89-4854047725607 )" && echo "$( echo $HOME/.ansible/tmp/ansible-tmp-1462190379.89-4854047725607 )" )'
<192.168.1.190> PUT /tmp/tmpQp90Vo TO /root/.ansible/tmp/ansible-tmp-1462190379.89-4854047725607/setup
<192.168.1.190> SSH: EXEC sftp -b - -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o Port=1221 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r '[192.168.1.190]'
<192.168.1.190> ESTABLISH SSH CONNECTION FOR USER: None
<192.168.1.190> SSH: EXEC ssh -C -vvv -o ControlMaster=auto -o ControlPersist=60s -o Port=1221 -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/ansible-ssh-%h-%p-%r -tt 192.168.1.190 'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /usr/bin/python /root/.ansible/tmp/ansible-tmp-1462190379.89-4854047725607/setup; rm -rf "/root/.ansible/tmp/ansible-tmp-1462190379.89-4854047725607/" > /dev/null 2>&1'
ok: [test]

TASK [test : variable in when clause] ******************************************
task path: /etc/ansible/roles/test/tasks/main.yml:3
skipping: [test] => {"changed": false, "skip_reason": "Conditional check failed", "skipped": true}

TASK [test : variable in task] *************************************************
task path: /etc/ansible/roles/test/tasks/main.yml:8
skipping: [test] => {"changed": false, "skip_reason": "Conditional check failed", "skipped": true}

TASK [test : Test in items] ****************************************************
task path: /etc/ansible/roles/test/tasks/main.yml:12
fatal: [test]: FAILED! => {"failed": true, "msg": "ERROR! 'ansible_local' is undefined"}

PLAY RECAP *********************************************************************
test                      : ok=1    changed=0    unreachable=0    failed=1
@bcoca
Copy link
Member

bcoca commented May 2, 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.

Roles CANNOT be made conditional themselves, so the when: clause is there for the included tasks to be inherited. http://docs.ansible.com/ansible/playbooks_conditionals.html#applying-when-to-roles-and-includes

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 May 2, 2016
@ogmueller
Copy link
Author

I guess, i did not explain myself well enough.

the problem is not the "when:" in the task, but that ansible reacts differently on this unknown variable "ansible_local.something". if i use the variable in the task like debug (in the message), no error occurs, BUT when using the same unknown variable in the "with_items" an error occurs within the same circumstances. ansible should either consistently throw an error in both cases or in none of them.

i guess my example was not the best. in the role file only the second and third example are interesting.

@bcoca
Copy link
Member

bcoca commented May 2, 2016

that is because when: is evaluated INSIDE the with_ loop, but the task itself does not get evaluated until after when: decides if it runs or not.

So with_ loops need to ensure that they don't cause undefined errors, normally through the use of the default filter.

@ogmueller
Copy link
Author

sorry for the noise. i thought it is a bug, because it used to work in ansible 1.9.x and it "feels" a bit odd :-)
thanks for the help, though.

@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