Using tags in a playbook

I’m still pretty new to Ansible, and I’m trying to figure out the proper method for setting up a role, and then calling only a couple of tasks from that role from a playbook.

The reason I’m doing so, is I have a group of nodes that need two tomcat instances running on top of a base instance, but I also have another group that only needs one tomcat instance. So I’ve created a very simple role:


  • name: install tomcat7
    sudo: True
    apt: name=tomcat7 update_cache=yes cache_valid_time=3600
    tags:
  • wowza
  • name: create directory structure for tomcat_1
    sudo: True
    file: path=/var/opt/tomcat_1/{{ item }} state=directory owner=tomcat7 group=nogroup
    with_items:
  • bin
  • conf
  • logs
  • temp
  • webapps
  • work
    tags:
  • java_nodes
  • name: create lib link
    file: src=/usr/share/tomcat7/lib dest=/var/opt/tomcat_1/lib state=link
    tags:
  • java_nodes
  • name: create deeper directory structure for tomcat_1
    sudo: True
    file: path=/var/opt/tomcat_1/conf/Catalina/localhost state=directory owner=tomcat7 group=nogroup
    tags:
  • java_nodes
  • name: create symlink under tomcat_1/bin
    sudo: True
    file: src=/usr/share/tomcat7/bin/bootstrap.jar dest=/var/opt/tomcat_1/bin/bootstrap.jar owner=tomcat7 group=nogroup state=link
    tags:
  • java_nodes
  • name: create file structure for tomcat_2
    sudo: True
    file: path=/var/opt/tomcat_2/{{ item }} state=directory owner=tomcat7 group=nogroup
    with_items:
  • bin
  • conf
  • logs
  • temp
  • webapps
  • work
    tags:
  • java_nodes
  • name: create lib link
    file: src=/usr/share/tomcat7/lib dest=/var/opt/tomcat_2/lib state=link
    tags:
  • java_nodes
  • name: create deeper directory structure for tomcat_2
    sudo: True
    file: path=/var/opt/tomcat_2/conf/Catalina/localhost state=directory owner=tomcat7 group=nogroup
    tags:
  • java_nodes
  • name: create symlink under tomcat_2/bin
    sudo: True
    file: src=/usr/share/tomcat7/bin/bootstrap.jar dest=/var/opt/tomcat_2/bin/bootstrap.jar owner=tomcat7 group=nogroup state=link
    tags:
  • java_nodes

And I’m executing it from this playbook:


  • name: Install Oracle Java 7 and tomcat
    hosts: basic_java
    sudo: True
    roles:
  • java
  • { role: tomcat, tags: ‘wowza’ }

According to the documentation, that should be possible:

http://docs.ansible.com/playbooks_tags.html

You may also apply tags to roles:

roles:
  - { role: webserver, port: 5000, tags: [ 'web', 'foo' ] }

I have tried this a couple of different ways:

{ role: tomcat, tags: ‘wowza’ }

{ role: tomcat, tags: ‘wowza’, skip-tags ‘java_nodes’ }

{ role: tomcat, skip-tags: ‘java_nodes’ }

But they all execute the entire role.

I’m using Ansible 1.8.2, and running it from a mac, on Ubuntu nodes in Vagrant.

Am I incorrect in my interpretation of the documentation? Is this not an allowable way to use tags? And if so, is there a best practices way to achieve what I’m going for, here? I’d hate to have to create a second tomcat role - that seems inelegant.

Thank you, and I’m looking forward to your thoughts.

Did you try with the brackets, as indicated in the documentation you pasted in?

when you apply tags to roles, you actually ADD that tag to each task
in the role, there is no way to use tags in a role declaration to
select a subset of tasks.