# handler executing when it shouldn't be (variables)

**URL:** <https://forum.ansible.com/t/handler-executing-when-it-shouldnt-be-variables/25370>\
**Category:** Ansible Project\
**Tags:** rhel\
**Created:** [August 30, 2017, 4:57pm UTC](https://forum.ansible.com/t/handler-executing-when-it-shouldnt-be-variables/25370 "2017-08-30T16:57:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [August 30, 2017, 4:57pm UTC](https://forum.ansible.com/t/handler-executing-when-it-shouldnt-be-variables/25370/1 "2017-08-30T16:57:48Z")

</div>

I have the following in my tasks/main.yml

`  
Enter code here.- name: Check if NetworkManager is running  
shell: “systemctl status NetworkManager”  
register: nm\_status  
changed\_when: false  
ignore\_errors: yes  
when: ansible\_distribution\_major\_version == ‘7’

- name: debug  
debug:  
var: nm\_status.stdout

`

For one particular server (cent7) the stdout is as follows:

`Enter codok: [cent7] => { "nm_status.stdout": "● NetworkManager.service - Network Manager\n Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; disabled; vendor preset: enabled)\n Active: inactive (dead) since Wed 2017-08-30 12:31:32 EDT; 5min ago\n Docs: man:NetworkManager(8)\n Process: 27717 ExecStart=/usr/sbin/NetworkManager --no-daemon (code=exited, status=0/SUCCESS)\n Main PID: 27717 (code=exited, status=0/SUCCESS)\n\nAug 30 12:31:31 cent7 NetworkManager[27717]: <info> [1504110691.5721] device (enp0s3): link connected\nAug 30 12:31:31 cent7 NetworkManager[27717]: <info> [1504110691.5730] manager: (enp0s3): new Ethernet device (/org/freedesktop/NetworkManager/Devices/2)\nAug 30 12:31:31 cent7 NetworkManager[27717]: <info> [1504110691.5734] device (enp0s8): link connected\nAug 30 12:31:31 cent7 NetworkManager[27717]: <info> [1504110691.5743] manager: (enp0s8): new Ethernet device (/org/freedesktop/NetworkManager/Devices/3)\nAug 30 12:31:31 cent7 NetworkManager[27717]: <info> [1504110691.5747] manager: startup complete\nAug 30 12:31:32 cent7 systemd[1]: Stopping Network Manager...\nAug 30 12:31:32 cent7 NetworkManager[27717]: <info> [1504110692.5205] caught SIGTERM, shutting down normally.\nAug 30 12:31:32 cent7 NetworkManager[27717]: <info> [1504110692.5217] device (enp0s3): released from master device bond0\nAug 30 12:31:32 cent7 NetworkManager[27717]: <info> [1504110692.5221] device (enp0s8): released from master device bond0\nAug 30 12:31:32 cent7 systemd[1]: Stopped Network Manager." } e here...`

I call the handler(s) through “Networking”  
My handlers/main.yml currently is as follows:

`  
Enter code here…—

# handlers file for dns\_update

- name: Networking  
debug:  
changed\_when: true  
notify:

- 6 network restart

- 7 network restart

- 7 NetworkManager restart

- name: ensure stopped  
service:  
name: NetworkManager  
state: stopped

- name: 6 network restart  
service:  
name: network  
state: restarted  
when: ansible\_distribution\_major\_version == ‘6’ and not ansible\_distribution\_major\_version == ‘7’

- name: 7 network restart  
service:  
name: network  
state: restarted  
when: (ansible\_distribution\_major\_version == ‘7’) and (‘“NetworkManager.service; disabled” in nm\_status.stdout’)  
notify: ensure stopped

- name: 7 NetworkManager restart  
service:  
name: NetworkManager  
state: restarted  
when: (ansible\_distribution\_major\_version == ‘7’) and (‘“NetworkManager.service; disabled” not in nm\_status.stdout’)  
`

The reason for 3 different network restarts is to handle cent/rhel{6,7} servers. Some of the version 7 boxes have NetworkManager running, and some do not

Results for my cent7 box:

`

RUNNING HANDLER [dns\_update : Networking] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [cent7] =\> {  
“msg”: “Hello world!”  
}

RUNNING HANDLER [dns\_update : 6 network restart] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [cent7]

RUNNING HANDLER [dns\_update : 7 network restart] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [cent7]

RUNNING HANDLER [dns\_update : 7 NetworkManager restart] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [cent7]

RUNNING HANDLER [dns\_update : ensure stopped] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [cent7]

`

I expected it to only run “7 network restart” and “ensure stopped”  
I only have the “ensure stopped” in there because the former seems to be starting NetworkManager despite being disabled. In addition, I don’t know why “7 NetworkManager restart” is executing as the nm\_status.stdout DOES contain “NetworkManager.status disabled;”

I must be missing something, have a syntax error, or am encountering some sort of a bug. I would appreciate any feedback.

Version info:

# ansible --version

ansible 2.3.1.0  
config file = /etc/ansible/ansible.cfg  
configured module search path = Default w/o overrides  
python version = 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [August 30, 2017, 5:25pm UTC](https://forum.ansible.com/t/handler-executing-when-it-shouldnt-be-variables/25370/2 "2017-08-30T17:25:53Z")

</div>

I think I have a syntax error with my when: statement

I believe it is ignoring the second half of the statement and continuing on (hence the reason multiple handlers are executing because only the part that is valid is being enforced). I will tweak it some more and update this if I get a fix

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [August 30, 2017, 5:35pm UTC](https://forum.ansible.com/t/handler-executing-when-it-shouldnt-be-variables/25370/3 "2017-08-30T17:35:20Z")

</div>

Yep, it was a problem with my when: statements. I changed to the following and things are behaving as expected now:

`
