Is there any way to stop handlers from running ? I was trying to add tag and use "--skip-tags" to it but it does not work.
I could add next role variable reload_service: true
and use it but I've already started using tags and they work great to just re-run part of role.
Handlers are usually used to restart services and I want to run this role without starting service without changing role variables just to cover next case.
I'm using ansible 2.1.2.0
Test case:
mkdir -p test/role/handlers test/role/tasks
cd test
echo -ne '---\n - command: "echo Test"\n notify: restart\n' > role/tasks/main.yml
echo -ne '---\n- name: restart\n command: "echo Handler"\n tags: [handlers]\n' > role/handlers/main.yml
echo -ne '---\n- hosts: localhost\n gather_facts: false\n roles:\n - role\n' > play.yml
ansible-playbook play.yml --skip-tags handlers
Using Ansible Handlers As earlier mentioned, handlers are just like other tasks in a playbook, the difference being that they are triggered using the notify directive, and are only run when there is a change of state.
You can add ' notify ' to any handler. If that handler causes a changed state, it notifies the next handler in the chain.
Handlers are just like regular tasks in an Ansible playbook (see Tasks) but are only run if the Task contains a notify keyword and also indicates that it changed something. For example, if a config file is changed, then the task referencing the config file templating operation may notify a service restart handler.
Ansible provides feature named handlers, which is like a task but will only run when called by a notifier in another task. This feature is important because your requirements for running a task may depend on the state of a service, existence of a file or a follow up tasks when state changed.
Here's an example of how you could use a variable to skip a handler:
$ cat test.yaml
---
- hosts: localhost
tasks:
- copy:
content: "{{ ansible_date_time.epoch }}" # This will always trigger the handler.
dest: /tmp/debug
notify:
- debug
handlers:
- name: debug
debug:
msg: Hello from the debug handler!
when:
- skip_handlers | default("false") == "false"
Normal use would look like this:
$ ansible-playbook test.yaml
And to skip the handlers:
$ ansible-playbook test.yaml -e skip_handlers=true
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