Tagging all imported tasks

25 views
Skip to first unread message

Andy Smith

unread,
Nov 29, 2018, 12:22:00 AM11/29/18
to ansible...@googlegroups.com
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
roles:
- {
role: some_server,
tags: some_server,
}
- {
role: other_role,
tags: other_role,
}
# 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

Kai Stian Olstad

unread,
Nov 29, 2018, 1:17:18 AM11/29/18
to ansible...@googlegroups.com
On 29.11.2018 06:21, Andy Smith wrote:
> 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
> roles:
> - {
> role: some_server,
> tags: some_server,
> }

That's what you're do here, add tag some_server on all task in the role
role_server.


> - {
> role: other_role,
> tags: other_role,
> }

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.


--
Kai Stian Olstad

Andy Smith

unread,
Nov 29, 2018, 1:41:17 AM11/29/18
to ansible...@googlegroups.com
Hi Kai,
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

Andy Smith

unread,
Nov 29, 2018, 1:47:13 AM11/29/18
to ansible...@googlegroups.com
Hi Kai,

On Thu, Nov 29, 2018 at 06:41:03AM +0000, Andy Smith wrote:
> On Thu, Nov 29, 2018 at 07:17:06AM +0100, Kai Stian Olstad wrote:
> > 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).

I'm sorry, you are correct. I just realised that I used the wrong
inventory file, and this gave different behaviour which confused me.

Apologies!
Andy

Brian Coca

unread,
Nov 30, 2018, 9:52:20 AM11/30/18
to ansible...@googlegroups.com
import tasks does not allow for 'inline keywords', you need to set
them at task level:

- import_tasks: other_config.yml
tags:
- some_server


--
----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages