17

I have a site.yml which imports several playbooks.

- import_playbook: webservers.yml
- ....

Every playbook "calls" several roles:

- name: apply the webserver configuration 
  hosts: webservers

  roles:
  - javajdk
  - tomcat
  - apache

How can I run only the javajdk role ?

This would run all roles... ansible-playbook -i inventory webservers.yml

I know that there are tags, but how do I assign them to a role in general?

techraf
  • 64,883
  • 27
  • 193
  • 198
Michael Hoeller
  • 22,018
  • 10
  • 39
  • 66

1 Answers1

22

Tags are natural way to go. Three ways of specifying them for roles below:

- name: apply the webserver configuration 
  hosts: webservers

  roles:
    - role: javajdk
      tags: java_tag
    - { role: tomcat,  tags: tomcat_tag }

  tasks:
    - include_role:
        name: apache
      tags: apache_tag

You can explictly specify the tags to run:

ansible-playbook example.yml --tags "java_tag"

Reference to docs

alyberty
  • 112
  • 1
  • 7
techraf
  • 64,883
  • 27
  • 193
  • 198
  • 1
    This will either define a tag on the task level, what is to granular or at the place where a role is *used* but this will not generally assign the tag to the role. In case the role is used in an other playbook it will *not* have the tag. Of cause this are two separate approaches, just hope that the genearl one is also possible – Michael Hoeller Nov 21 '17 at 16:11
  • 1
    Frankly speaking, I have no clue what you wanted to say in your comment. – techraf Nov 21 '17 at 22:59
  • Sorry for my unclearness, frankly, I will try it again. Your answer is works fine when I look at the above example. Now step back and imagine a second playbook which also uses - role: javajdk (no tag) Logically for this second playbook will not work with a tag-filter. -- The question is now can a tag be assigned to a role (only one time, one place) so that a filter on tag will work. The only advantage would be to avoid to assign tags to a role every time it is used. – Michael Hoeller Nov 22 '17 at 06:17
  • I understand now what you say. Fortunately (!) you can't achieve it easily. – techraf Nov 22 '17 at 06:25
  • 3
    An example of how to run `ansible-playbook` with tags would make this a better answer. – J.W.F. Apr 19 '19 at 18:36
  • @JustinW.Flory is this what you are referring to? ansible-playbook example.yml --tags "configuration,packages" – Rakim Feb 19 '20 at 00:16