On Wednesday, 15 November 2017 15.58.47 CET DigitalHermit wrote:
> Hello all:
> I've been Googling for a couple hours trying to find a way to pass tags
> to a role from within a playbook.
That's not possible.
> I've tried:
>
> Inside my utility role main.yml:
>
> - name: "Task 1"
> include: task1.yml
> tags: utility_task1
>
> - name: "Task 2"
> include: task2.yml
> tags: utility_task2
>
> - name: "Task 3"
> include: task3.yml
> tags: utility_task3
>
>
> This works fine if I pass the tag from the command line: ansible-playbook
> playbooks/foo.yml -t "utility_task2"
This is the only
>
> However, it does not work when called from a playbook like this:
>
> roles:
> - { role: utility, tags: "utility_task2" }
What you do here is adding the tag utility_task2 to all task in the role utility.
As the documentation[1] say
"Adding “tags:” in any part of a play (including roles) adds those tags to the contained tasks."
> Google searches show there is an ansible.cfg setting for "gathering=smart"
> or "gathering=implicit" that can change the behaviour but so far neither
> seems to make a difference.
These setting has nothing to do with tags, they are for configuring facts gathering.
> My workaround is currently to set an extra variable (e.g., --extra-vars
> "utility_task=task1" ) then use a when: on each separate task. This works
> but the output is very messy: I.e., it prints a "skipping:" message for
> every other task in the role. As the number of separate tasks in a role
> grow, these skips will rise exponentially.
>
> Is there a cleaner, Ansible way to do this?
Maybe, there's always many ways to tackle a challenge.
Since you haven't shared the specifics it impossible to be specific.
[1]
https://docs.ansible.com/ansible/latest/playbooks_tags.html
--
Kai Stian Olstad