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

Tags are ignored on plays that are included that use with_items #19115

Closed
jasonmichaellehman opened this issue Dec 9, 2016 · 7 comments
Closed
Labels
affects_2.2 This issue/PR affects Ansible v2.2 bug This issue/PR relates to a bug. deprecated This issue/PR relates to a deprecated module. module This issue/PR relates to a module. support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@jasonmichaellehman
Copy link

jasonmichaellehman commented Dec 9, 2016

ISSUE TYPE
  • Bug Report
COMPONENT NAME

Conditional Include

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

CONFIGURATION

N/A

OS / ENVIRONMENT

RHEL 7

SUMMARY

TAGS are ignored when including a playbook that uses with_items

STEPS TO REPRODUCE

run ansible-playbook tagtest.yml --tags dev

tagtest.yml

- hosts: localhost
  tags:
    - prod
  tasks:
    - set_fact:
        taskname: "foo"

    - include: tasks/{{taskname}}{{item}}.yml
      with_items: "bar"

    - debug: msg="hello prod tags"

- hosts: localhost
  tags:
    - dev
  tasks:
    - debug: msg="hello dev tags"

tasks/foobar.yml

- debug: msg="I ran foobar"

EXPECTED RESULTS

Expectation is that the first play on localhost with tags Prod would have been ignored and only "hello dev tags" would have been outputted.

ACTUAL RESULTS

The play attempted to run the include task and failed because it did NOT include the first set_fact and taskname was coming through as undefined. In prior version of ansible the complete host section would be skipped when tags were used in this fashion.

PLAY [localhost] ***************************************************************

TASK [setup] *******************************************************************
ok: [localhost]

TASK [include] *****************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "'taskname' is undefined"}
        to retry, use: --limit @/opt/jml/appstack/ansible/testtag.retry

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

@ansibot ansibot added affects_2.2 This issue/PR affects Ansible v2.2 bug_report labels Dec 13, 2016
@ansibot ansibot added the support:core This issue/PR relates to code supported by the Ansible Engineering Team. label Jun 29, 2017
@ansibot
Copy link
Contributor

ansibot commented Nov 18, 2017

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 deprecated This issue/PR relates to a deprecated module. module This issue/PR relates to a module. labels Nov 18, 2017
@ansibot
Copy link
Contributor

ansibot commented Feb 14, 2018

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 bug This issue/PR relates to a bug. and removed bug_report labels Mar 1, 2018
@mickaelperrin
Copy link

mickaelperrin commented Jun 15, 2018

Had a really near problem and reported it first on stack overflow.

STEPS TO REPRODUCE

role/tasks/main.yml

---
- include: test.yml
  with_items:
    - A
    - B
  tags:
    - my_role_test

role/tasks/test.yml

---
- debug:
    msg: "It works"

When I run my playbook with --tags=my_role_test, I can see the expected output twice:

[20:18:52] test : debug | server | SUCCESS | 593ms
{
  - msg: It works
}

However, I got this output:

[20:15:41] test : include
[20:15:41]  ➥ system | included: /test/tasks/test.yml for server
[20:15:41]  ➥ system | included: /test/tasks/test.yml for server
[20:15:41]  ➥ system | -- Play recap --

So the tasks are not being executed.

WORKAROUND

Put tags on each task of the included file:

role/tasks/main.yml

---
- include: test.yml
  with_items:
    - A
    - B

role/tasks/test.yml

---
- debug:
    msg: "It works"
  tags:
    - my_role_test

@sivel sivel added this to Triage in include and import issues via automation Jun 15, 2018
@sivel
Copy link
Member

sivel commented Jun 15, 2018

@jasonmichaellehman this issue is resolved in recent ansible versions, but will require a change from include to utilize include_tasks instead.

static includes cannot utilize host variables, such as from set_fact to indicate the file to be included.

Changing to include_tasks produces the following output for me:

PLAY [localhost] *****************************************************************************************************************************************************************************************************************************

PLAY [localhost] *****************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [debug] *********************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "hello dev tags"
}

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

@sivel sivel closed this as completed Jun 15, 2018
include and import issues automation moved this from Triage to Done Jun 15, 2018
@mickaelperrin
Copy link

@sivel unfortunately, for my test case, this doesn't work either.

I have the same output when using include_tasks.

18:53:24] test : include_tasks | server | SUCCESS | 1882ms
[18:53:25] system | included: /test/tasks/test.yml for server
[18:53:25] system | included: /test/tasks/test.yml for server
[18:53:25] system | -- Play recap --
server : ok=4    changed=0    unreachable=0    failed=0

@sivel
Copy link
Member

sivel commented Jun 18, 2018

@mickaelperrin your issue is different from the one described here.

What you are encountering is expected, and explained at https://docs.ansible.com/ansible/devel/porting_guides/porting_guide_2.5.html#dynamic-includes-and-attribute-inheritance

A tag applied to a dynamic include only affects the include, and is not inherited by the tasks within.

@mickaelperrin
Copy link

@sivel Thanks for taking time to answer me and to point me in the right direction.

@ansible ansible locked and limited conversation to collaborators Jun 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.2 This issue/PR affects Ansible v2.2 bug This issue/PR relates to a bug. deprecated This issue/PR relates to a deprecated module. module This issue/PR relates to a module. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
Development

No branches or pull requests

4 participants