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. The goal is to build a utility role with common tasks (mount a filesystem, add specific users, etc.). So far it’s not working.
I’ve tried:
Inside my utility role main.yml:
include: task1.yml
tags: utility_task1
include: task2.yml
tags: utility_task2
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”
However, it does not work when called from a playbook like this:
roles:
- { role: utility, tags: “utility_task2” }
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.
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?
Thanks in advance.
Kwan
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:
Thank you for your response.
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 suspected as much…
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.”
OK, that clarifies things a bunch.
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.
Let me elaborate on my goal:
I have a library of roles. Each of these roles has common tasks across the library. For example, both my install_nginx and install_elk roles have a common task to create a filesystem.
E.g.:
roles/install_nginx/tasks/create_webdir.yml
roles/install_elk/tasks/create_datadir.yml
To simplify filesystem creation I created an adhoc_fs role and call it from within the playbook with an include
- name: “Create datadir”
include_role:
name: adhoc_fs
vars:
adhoc_filesystems:
{ vol_group: ‘data_vg’, lv_mount: ‘/mnt/webdata1’, lv_name: ‘webdata1_lv’, lv_size: ‘40G’, owner: apache }
{ vol_group: ‘data_vg’, lv_mount: ‘/mnt/webdata2’, lv_name: ‘webdata2_lv’, lv_size: ‘40G’, owner: apache }
This works fine, but the problem is now that my library contains many of these utility roles. I would prefer to have them all in a single role, “utility” for example, and call them as needed. Typical tasks would be user creation, iptables rules, etc…The point of this is to avoid copying/pasting from role to role which will ease maintenance.
Know it understand what you are trying to achieve.
Do something like this when is you only option I think, but the problem was all the skipped tasks.
In Ansible 2.4 include is depreciated in favor of import_tasks and include_tasks.
The nice thing with include task is that it's dynamic, so you when is check only on the include it self and not on all the task in the included file.
import_tasks works like include, when is added to all task before all task is executed.
So with include_task and Ansible 2.4 you will reduce the number of skipped task in the output considerably.