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?
Ansible run_once parameter is used with a task, which you want to run once on first host. When used, this forces the Ansible controller to attempt execution on first host in the current hosts batch, then the result can be applied to the other remaining hosts in current batch.
There is no way to directly execute a role. Roles have no explicit setting for which host the role will apply to. Top-level playbooks are the bridge holding the hosts from your inventory file to roles that should be applied to those hosts.
Using the --limit parameter of the ansible-playbook command is the easiest option to limit the execution of the code to only one host. The advantage is that you don't need to edit the Ansible Playbook code before executing to only one host.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With