Need help with tags

18 views
Skip to first unread message

Mouton Noir

unread,
Feb 24, 2021, 11:15:48 AM2/24/21
to Ansible Project
Hi,

I'd like to use tags to select tasks I launch.
I found documentations about how to limit playbook, role... execution to the tagged tasks l choose from adhoc command (-t option).

But I need to do the same from another playbook... and I can't find anything about that

ex : 
extract from the main.yml from role "myrole" :
************************************************************
- name: Installation Nutrilog
  import_tasks: nutrilog.yml
  tags:
    - fst-nutrilog

- name: Installation Openshot
  import_tasks: /etc/ansible/roles/fst-win/tasks/win_install_freelog_task_fst.yml
  vars:
    logiciel: "Openshot"
  tags:
    - fst-openshot
************************************************************

I'd like to be able to get choose the tagged tasks when including/importing it in a task file, using something like :
include_roles :
  name: myrole
  tags: fst-openshot

What did I miss ?

Matt Martz

unread,
Feb 24, 2021, 11:24:04 AM2/24/21
to ansible...@googlegroups.com
There is no capability to limit tags from within a playbook.  Tags can only be limited from the CLI via -t/--tags

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/e3a72e51-ecf0-41eb-8ca0-96e42f677371n%40googlegroups.com.


--
Matt Martz
@sivel
sivel.net

Mouton Noir

unread,
Feb 24, 2021, 11:39:12 AM2/24/21
to Ansible Project
Thanks Matt for your quick answer... Even if it doesn't arrange me... ;-)
It's weird this is not possible. I'm surprised. I guess I'm not the first one to consider doing this. Is there a reason why ?
Did you (or someone else) hear about a "trick" to get a similar behaviour using something else ?

Regards

Mouton Noir

unread,
Feb 24, 2021, 12:05:27 PM2/24/21
to Ansible Project
I finally found a "trick" that made it for me : 
Every task in the main.yml from "myrole" are conditioned with a different value for a variable myowntags
ex:
when: myowntags == "fst-openshot" 

Then, in a taskfile or playbook, I can select the tasks I want with something like : 
- name: test
  include_role:
    name: myrole
      vars:
        myowntags: "fst-openshot"

As this role is only dedicated to pick up some tasks when needed (not applied to any group as itself), it makes it for me.

Vladimir Botka

unread,
Feb 24, 2021, 12:29:45 PM2/24/21
to Mouton Noir, ansible...@googlegroups.com
On Wed, 24 Feb 2021 09:05:27 -0800 (PST)
Mouton Noir <the.m0u...@gmail.com> wrote:

> Every task in the main.yml from "myrole" are conditioned with a different
> value for a variable myowntags
> ex:
> when: myowntags == "fst-openshot"
>
> Then, in a taskfile or playbook, I can select the tasks I want with
> something like :
> - name: test
> include_role:
> name: myrole
> vars:
> myowntags: "fst-openshot"
>
> As this role is only dedicated to pick up some tasks when needed (not
> applied to any group as itself), it makes it for me.

Next option would be to fragment main.yml and put the tasks into
the separate files, e.g

shell> cat main.yml
- import_tasks: tasks01.yml
- import_tasks: tasks02.yml
...

This would keep the original functionality untouched. Now, create
files with the logical blocks you'd like to use, e.g.

shell: cat fst-openshot.yml
- import_tasks: tasks07.yml
- import_tasks: tasks25.yml
- import_tasks: tasks33.yml

Then, it would be possible to include such blocks selectively, e.g.

- name: test
include_role:
name: myrole
tasks_from: "{{ myowntags }}.yml"

--
Vladimir Botka

Mouton Noir

unread,
Feb 25, 2021, 6:12:05 AM2/25/21
to Ansible Project
Thanks Vladimir
I didn't heard about tasks_from.
I'll test it too

Brian Coca

unread,
Feb 25, 2021, 11:30:47 AM2/25/21
to Ansible Project
We've seen this requested several times, but it is normally an
indicator of people making plays/roles way too big.

So we recommend actually splitting the playbook/roles into the more
manageable units instead of implementing this kind of functionality or
using a workaround via conditionals.

--
----------
Brian Coca

Reply all
Reply to author
Forward
0 new messages