# Ansible handler with when condition

**URL:** <https://forum.ansible.com/t/ansible-handler-with-when-condition/2366>\
**Category:** Get Help\
**Created:** [November 14, 2023, 2:56pm UTC](https://forum.ansible.com/t/ansible-handler-with-when-condition/2366 "2023-11-14T14:56:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![erichymowitz](https://avatars.discourse-cdn.com/v4/letter/e/9d8465/32.png) [@erichymowitz](https://forum.ansible.com/u/erichymowitz)\
**Post date:** [November 14, 2023, 2:56pm UTC](https://forum.ansible.com/t/ansible-handler-with-when-condition/2366/1 "2023-11-14T14:56:01Z")

</div>

So I have this playbook

```auto
- hosts: localhost
  gather_facts: no
  vars:
    this_thing_is_true: true
    use_handler: false

  tasks:
    - debug:
        msg: 'Notifying handlers'
      changed_when: this_thing_is_true
      notify:
        - me

  handlers:
    - name: me
      debug:
        msg: 'I have been notified'
      when: use_handler is true

```

And when I run it, as expected, the handler doesn’t run.

```auto
# ansible-playbook handler.yml

PLAY [localhost] ***************************************************************

TASK [debug] *******************************************************************
changed: [localhost] => {
    "msg": "Notifying handlers"
}

RUNNING HANDLER [me] ***********************************************************
skipping: [localhost]

```

I can activate the handler by changing the `use_handler` variable in the playbook.

```auto
 # ansible-playbook handler.yml  
PLAY [localhost] ***************************************************************

TASK [debug] *******************************************************************
changed: [localhost] => {
    "msg": "Notifying handlers"
}

RUNNING HANDLER [me] ***********************************************************
ok: [localhost] => {
    "msg": "I have been notified"
}

```

However, I thought this would also activate the handler … but it doesn’t.

```auto
# ansible-playbook -e use_handler=true handler.yml

PLAY [localhost] ***************************************************************

TASK [debug] *******************************************************************
changed: [localhost] => {
    "msg": "Notifying handlers"
}

RUNNING HANDLER [me] ***********************************************************
skipping: [localhost]

```

Am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [November 14, 2023, 3:31pm UTC](https://forum.ansible.com/t/ansible-handler-with-when-condition/2366/2 "2023-11-14T15:31:15Z")

</div>

The problem is that it’s impractical to pass a bool into a run with `key=value` style extra variables on the command line. To set a bool, you’ll need to use JSON. Behold:

```txt
[utoddl@tango ansible]$ ansible localhost -e use_handler=true -m debug -a '{"msg": "{{ use_handler | type_debug }}"}'
localhost | SUCCESS => {
    "msg": "str"
}
[utoddl@tango ansible]$ ansible localhost -e '{"use_handler": true}' -m debug -a '{"msg": "{{ use_handler | type_debug }}"}'
localhost | SUCCESS => {
    "msg": "bool"
}

```

See [this](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_variables.html#key-value-format):

> Use the JSON format if you need to pass non-string values such as Booleans, integers, floats, lists, and so on.

---

<div class="post-metadata">

**Author:** ![erichymowitz](https://avatars.discourse-cdn.com/v4/letter/e/9d8465/32.png) [@erichymowitz](https://forum.ansible.com/u/erichymowitz)\
**Post date:** [November 14, 2023, 6:59pm UTC](https://forum.ansible.com/t/ansible-handler-with-when-condition/2366/3 "2023-11-14T18:59:34Z")

</div>

Thank you very much.

I will also note that this construct also worked, since it uses a string and not a boolean

```
  when: use_handler == 'true'

```

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [November 14, 2023, 7:27pm UTC](https://forum.ansible.com/t/ansible-handler-with-when-condition/2366/4 "2023-11-14T19:27:53Z")

</div>

While that does work, if you plan to use strings, I’d recommend you make them look like strings rather than Booleans. We spent _days_ one time disentangling a rat’s nest of strings, bools, and strings-as-bools. The amazing thing is that it worked as long as we didn’t fix any single obvious error!

Anyway, consider something like

```auto
when: handler_state | default('disabled') == 'enabled'

```

It’s easy to get right on the command line in either `key=value` or JSON form, and doesn’t tax your brain to read later.

Cheers.

---

<div class="post-metadata">

**Author:** ![laurent-indermuehle](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/laurent-indermuehle/32/289_2.png) [@laurent-indermuehle](https://forum.ansible.com/u/laurent-indermuehle)\
**Post date:** [November 14, 2023, 8:00pm UTC](https://forum.ansible.com/t/ansible-handler-with-when-condition/2366/5 "2023-11-14T20:00:55Z")

</div>

I want to add (because I learned that the hard way) that you should always transform your “bool” variable with the bool filter in the playbook. If one day you decide to use AWX/AAP, your variable will always be a string even if you thought you send a bool.

So the correct way to write your line:

```yml
when: use_handler is true

```

is:

```yml
when: use_handler | bool is true

```

Or even better (and supported since Ansible 2.10):

```yml
when: use_handler is truthy(convert_bool=True)

```

From the documentation: [Tests — Ansible Documentation](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_tests.html#testing-truthiness)

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [November 14, 2023, 9:44pm UTC](https://forum.ansible.com/t/ansible-handler-with-when-condition/2366/6 "2023-11-14T21:44:32Z")

</div>

> [@laurent-indermuehle](#):
>
> ```yaml
> when: use_handler | bool is true
> 
> ```

Is that practically any different from:

```yaml
when: use_handler | bool

```

I’ve been using this form, I’ll add `is true` if that might make a difference in some circumstances?

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [November 15, 2023, 6:58am UTC](https://forum.ansible.com/t/ansible-handler-with-when-condition/2366/7 "2023-11-15T06:58:15Z")

</div>

They are not functionally different.

`ansible-lint` will berate you mercilessly for the redundant `is true` bit.

If you stick with `bool`s rather than strings, use [Laurent’s suggestion](http://forum.ansible.com/t/ansible-handler-with-when-condition/2366/5) and use the `truthy` test.

(In fact, consider marking that post as the solution. It makes the admins happy, and @laurent-indermuehle could use the quatloos.)

---

<div class="post-metadata">

**Author:** ![laurent-indermuehle](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/laurent-indermuehle/32/289_2.png) [@laurent-indermuehle](https://forum.ansible.com/u/laurent-indermuehle)\
**Post date:** [November 15, 2023, 8:18am UTC](https://forum.ansible.com/t/ansible-handler-with-when-condition/2366/8 "2023-11-15T08:18:15Z")

</div>

> [@chris](#):
>
> Is that practically any different

As one Python poem taught me: Explicit is better than implicit.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 10, 2024, 12:17pm UTC](https://forum.ansible.com/t/ansible-handler-with-when-condition/2366/9 "2024-07-10T12:17:34Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
