Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 720
Michael Hoeller Avatar asked Nov 21 '17 14:11

Michael Hoeller


People also ask

How do you run task only once in Ansible?

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.

How do you run a role in playbook?

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.

How do I control Ansible playbook only on specific 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.


1 Answers

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

like image 198
techraf Avatar answered Sep 18 '22 02:09

techraf