# Tagging all imported tasks

**URL:** <https://forum.ansible.com/t/tagging-all-imported-tasks/29148>\
**Category:** Ansible Project\
**Created:** [November 29, 2018, 5:21am UTC](https://forum.ansible.com/t/tagging-all-imported-tasks/29148 "2018-11-29T05:21:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andy\_Smith](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@Andy\_Smith](https://forum.ansible.com/u/Andy_Smith)\
**Post date:** [November 29, 2018, 5:21am UTC](https://forum.ansible.com/t/tagging-all-imported-tasks/29148/1 "2018-11-29T05:21:53Z")

</div>

Hi,

Is there a way to apply a tag to all of the import\_tasks statements  
in a role's tasks/main.yml file without stating it on every  
import\_tasks line?

That is, given a directory structure a bit like this:

.  
├── roles  
│ ├── some\_server  
│ │ └── tasks  
│ │ ├── configure\_this.yml  
│ │ ├── create\_that.yml  
│ │ ├── main.yml  
│ │ └── other\_config.yml  
│ ├── other\_role  
. .  
.  
└── site.yml

site.yml:

- hosts: all  
&nbsp;&nbsp;roles:  
&nbsp;&nbsp;&nbsp;&nbsp;- {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;role: some\_server,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: some\_server,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;- {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;role: other\_role,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: other\_role,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
# etc..

And roles/some\_server/tasks/main.yml:

- import\_tasks: configure\_this.yml  
- import\_tasks: create\_that.yml  
- import\_tasks: other\_config.yml

If I tried to conditionally apply only tag "some\_server" then it  
does process roles/some\_server/tasks/main.yml but not any of the  
imports within it.

If I add a tag attribute to each of those imports, i.e.:

- import\_tasks: configure\_this.yml tags=some\_server  
- import\_tasks: create\_that.yml tags=some\_server  
- import\_tasks: other\_config.yml tags=some\_server

then that works as the tasks within each of those imported files do  
inherit the tag. It seems ugly and repetitious though.

Is there a nice way to say that "every following import\_tasks should  
have these tags"?

Thanks,  
Andy

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 29, 2018, 6:17am UTC](https://forum.ansible.com/t/tagging-all-imported-tasks/29148/2 "2018-11-29T06:17:06Z")

</div>

> Is there a way to apply a tag to all of the import\_tasks statements  
> in a role's tasks/main.yml file without stating it on every  
> import\_tasks line?
> 
> That is, given a directory structure a bit like this:
> 
> .  
> ├── roles  
> │ ├── some\_server  
> │ │ └── tasks  
> │ │ ├── configure\_this.yml  
> │ │ ├── create\_that.yml  
> │ │ ├── main.yml  
> │ │ └── other\_config.yml  
> │ ├── other\_role  
> . .  
> .  
> └── site.yml
> 
> site.yml:
> 
> - hosts: all  
> &nbsp;&nbsp;roles:  
> &nbsp;&nbsp;&nbsp;&nbsp;- {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;role: some\_server,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: some\_server,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

That's what you're do here, add tag some\_server on all task in the role role\_server.

> &nbsp;&nbsp;&nbsp;&nbsp;- {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;role: other\_role,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: other\_role,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

And this will add tag other\_role on all tasks in the role.

> And roles/some\_server/tasks/main.yml:
> 
> - import\_tasks: configure\_this.yml  
> - import\_tasks: create\_that.yml  
> - import\_tasks: other\_config.yml
> 
> If I tried to conditionally apply only tag "some\_server" then it  
> does process roles/some\_server/tasks/main.yml but not any of the  
> imports within it.
> 
> If I add a tag attribute to each of those imports, i.e.:
> 
> - import\_tasks: configure\_this.yml tags=some\_server  
> - import\_tasks: create\_that.yml tags=some\_server  
> - import\_tasks: other\_config.yml tags=some\_server

Since you are setting the tag on the role in the play, you don't need to tag it here again.

---

<div class="post-metadata">

**Author:** ![Andy\_Smith](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@Andy\_Smith](https://forum.ansible.com/u/Andy_Smith)\
**Post date:** [November 29, 2018, 6:41am UTC](https://forum.ansible.com/t/tagging-all-imported-tasks/29148/3 "2018-11-29T06:41:03Z")

</div>

Hi Kai,

> \>If I add a tag attribute to each of those imports, i.e.:  
> \>  
> \>- import\_tasks: configure\_this.yml tags=some\_server  
> \>- import\_tasks: create\_that.yml tags=some\_server  
> \>- import\_tasks: other\_config.yml tags=some\_server
> 
> Since you are setting the tag on the role in the play, you don't need to tag  
> it here again.

If I leave out the "tags=some\_server" and then do:

$ ansible-playbook --tags some\_server

none of the imported tasks are executed (or maybe not even  
imported).

If I add "tags=some\_server" on the end of them, then they are.

This is with Ansible version 2.5.1.

Cheers,  
Andy

---

<div class="post-metadata">

**Author:** ![Andy\_Smith](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@Andy\_Smith](https://forum.ansible.com/u/Andy_Smith)\
**Post date:** [November 29, 2018, 6:47am UTC](https://forum.ansible.com/t/tagging-all-imported-tasks/29148/4 "2018-11-29T06:47:03Z")

</div>

Hi Kai,

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 30, 2018, 2:51pm UTC](https://forum.ansible.com/t/tagging-all-imported-tasks/29148/5 "2018-11-30T14:51:55Z")

</div>

import tasks does not allow for 'inline keywords', you need to set  
them at task level:

- import\_tasks: other\_config.yml  
&nbsp;&nbsp;tags:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- some\_server
