How to run only one role of an Ansible playbook?

Michael Hoeller picture Michael Hoeller · Nov 21, 2017 · Viewed 21.9k times · Source

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?

Answer

techraf picture techraf · Nov 21, 2017

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