0
votes

I have a playbook which calls multiple roles I have written. This playbook has each role tagged so than I can call them separately or run the entire procedure from start to end. Within the roles additional tags may be provided to break up the works within those as well. Some of the role tasks are tagged with "always" because I want them to execute whenever the role is ran, regardless of any role specific tags.

The problem I am running into is that these "always" tags execute whether the role specific tag is called or not. If I don't call the tag for a role, I don't want to role to execute at all, including any "always" tasks specific to that role.

Example:

# Playbook
- hosts: localhost
  roles:
    - role: roleA
      tags: do_roleA

    - role: roleB
      tags: do_roleB


# Role A: 
- name: Always do this when doing role A
  debug: msg="test"
  tags: always

- name: Task1
  debug: msg="task1"
  tags: do_task1

- name: Task2
  debug: msg="task2"
  tags: do_task2 

Example call:

ansibile-playbook my-playbook.yml --tags "do_roleB"

But this causes the debug in Role A to occur as well.

Some requirements I have:

  • Can not skip "always" as roleB may have some tasks that require it.
  • Do not want to change the tag from always and add a list of tags for every sub task. i.e. [do_task1,do_task2] as the number of tags would become large and easy to forget to add one.

Basically I am looking for a way to tell Ansible, if I call a "playbook tag" execute only roles from the playbook where the tag matches; do not execute any of the tasks in roles that do not match, even if they are tagged with "always". But if I call a "role tag" execute all the tasks in that role which either have tags of "always" or the tag I called.

Does Ansible have a feature like this? If it helps I am using Ansible 2.0.1.0

1
Can you give a try with: ansibile-playbook my-playbook.yml –skip-tags "do_roleA"perbellinio

1 Answers

1
votes

Keep in mind that applying tag do_roleA to a role adds do_roleA tag to every task in that role.
So your example role actually becomes:

# Role A:
- name: Always do this when doing role A
  debug: msg="test"
  tags:
    - always
    - do_roleA

- name: Task1
  debug: msg="task1"
  tags:
    - do_task1
    - do_roleA

- name: Task2
  debug: msg="task2"
  tags:
    - do_task2
    - do_roleA

You can remove always tag from that task, so after tagging a role with do_roleA it will be the only tag for that task.
So calling playbook with -t do_roleB will not execute that task.