Michael Hoeller
Michael Hoeller

Reputation: 24338

How to run only one role of an Ansible playbook?

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?

Upvotes: 20

Views: 44544

Answers (2)

bernard paulus
bernard paulus

Reputation: 1664

To answer your question "How can I run only the javajdk role ?", you can do that, starting from Ansible 2.7, according to this answer:

ansible -i inventory all --module-name import_role --args name=javajdk

Breakdown:

  • -i specifies the inventory, e.g. production.yml
  • all is the pattern, that is, which hosts to match. Here the pattern is the all group - run the role on all hosts from the inventory
  • --module-name import_role, this is key: run the import_role module. This imports a role in a play, e.g. the current execution of Ansible
  • --args name=javajdk: pass the name of the role that you want to execute, here javajdk

Important note: the formatting is not conform to your usual playbook invocation.

To multiple roles, for each role you need to run the command with a single role name as, as of today (Ansible 2.16), ansible only accepts a single --module-name and import_role only accepts a single role name... But at this point you are better off writing a quick playbook for it.

For the other question, "I know that there are tags, but how do I assign them to a role in general?", please check the answer from @techraf

Upvotes: 0

techraf
techraf

Reputation: 68629

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

Upvotes: 25

Related Questions