handler executing when it shouldn't be (variables)

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)]

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

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

`