Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable handlers from running

Tags:

ansible

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

like image 792
Dawid Gosławski Avatar asked Feb 07 '17 15:02

Dawid Gosławski


People also ask

What is the purpose of handlers in Ansible?

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.

Can a handler notify another handler?

You can add ' notify ' to any handler. If that handler causes a changed state, it notifies the next handler in the chain.

What is difference between tasks and handlers in Ansible?

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.

What is handlers and notifiers in Ansible?

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.


1 Answers

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
like image 57
Paddy Newman Avatar answered Sep 17 '22 17:09

Paddy Newman