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

[v2] conditional include fails and not skipped if --tags is used #12843

Closed
resmo opened this issue Oct 20, 2015 · 3 comments
Closed

[v2] conditional include fails and not skipped if --tags is used #12843

resmo opened this issue Oct 20, 2015 · 3 comments
Labels
bug This issue/PR relates to a bug.
Milestone

Comments

@resmo
Copy link
Contributor

resmo commented Oct 20, 2015

Issue Type:
  • Bug Report
Ansible Version:

ansible 2.0.0 (devel b46ce47) last updated 2015/10/20 20:10:50 (GMT +200)

Summary:

When using --tag and a task include: has a condition, the condition is going to be validated even the tag is not on that task.

Steps To Reproduce:
$ cat test.yml 

---
- hosts: localhost
  tasks:
  - shell: echo ok
    register: result

  # this should not fail if --tag my_tag is used right?
  - include: somefile.yml
    when: '"ok" in result.stdout'

  - shell: echo task with tag
    tags: my_tag
$ ansible-playbook test.yml --tags my_tag
Expected Results:
PLAY [localhost] ************************************************************** 

TASK: [shell echo task with tag] ********************************************** 
changed: [localhost]

PLAY RECAP ******************************************************************** 
localhost                  : ok=1    changed=1    unreachable=0    failed=0 
Actual Results:
PLAY ***************************************************************************

TASK [include] *****************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "ERROR! The conditional check '\"ok\" in result.stdout' failed. The error was: ERROR! error while evaluating conditional: \"ok\" in result.stdout ({% if \"ok\" in result.stdout %} True {% else %} False {% endif %})"}
@jimi-c jimi-c added this to the v2 milestone Oct 20, 2015
@mcsalgado
Copy link
Contributor

As I understand it this is intended behavior because there may be tagged tasks inside your include.

@bcoca
Copy link
Member

bcoca commented Oct 22, 2015

@resmo try the following:

diff --git a/lib/ansible/executor/task_executor.py b/lib/ansible/executor/task_executor.py
index 0ce9564..ed8d74b 100644
--- a/lib/ansible/executor/task_executor.py
+++ b/lib/ansible/executor/task_executor.py
@@ -275,9 +275,14 @@ class TaskExecutor:
         # the final task post-validation. We do this before the post validation due to
         # the fact that the conditional may specify that the task be skipped due to a
         # variable not being present which would otherwise cause validation to fail
-        if not self._task.evaluate_conditional(templar, variables):
-            self._display.debug("when evaluation failed, skipping this task")
-            return dict(changed=False, skipped=True, skip_reason='Conditional check failed', _ansible_no_log=self._play_context.no_log)
+        try:
+            if not self._task.evaluate_conditional(templar, variables):
+                self._display.debug("when evaluation failed, skipping this task")
+                return dict(changed=False, skipped=True, skip_reason='Conditional check failed', _ansible_no_log=self._play_context.no_log)
+        except AnsibleError:
+            # skip conditional exception in the case of includes as the vars needed might not be avaiable except in the included tasks
+            if self._task.action != 'include':
+                raise

@resmo
Copy link
Contributor Author

resmo commented Oct 22, 2015

@bcoca well done, works as expected! did various tests with the tag and it works like a charm

@bcoca bcoca closed this as completed in fc3a444 Oct 22, 2015
photoninger pushed a commit to photoninger/ansible that referenced this issue Oct 26, 2015
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 7, 2018
@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
bug This issue/PR relates to a bug.
Projects
None yet
Development

No branches or pull requests

5 participants