Ansible handler runs twice at the end of a play

Viewed 658

I have a problem that I have not seen before. I run Ansible 2.7.6 in a CI environment. My host is a Windows 10 on OpenStack from a cloud image i created using Packer and VirtualBox. Its been working OK so far. I introduced a second task among many, where I wanted to notify my reboot handler, and now I get multiple reboots.

This is my play, and tasks, somewhat cleaned for readability. The required error handling to achieve idempotency with win_shell aint pretty:

- hosts: workstations
  tasks:

   ...

    - name: Enable Windows feature
      include_role: name=win-config tasks_from=feature

    - name: Install language pack
      include_role: name=win-config tasks_from=language-pack

   ...

tasks/feature.yml:

- name: Inspect feature
  win_shell: DISM /Online /Get-FeatureInfo /FeatureName:Microsoft-Hyper-V
  register: check

- name: Enable Windows feature
  win_shell: DISM /Online /Enable-Feature /All /FeatureName:Microsft-Hyper-V /NoRestart
  notify: Reboot
  when: '"Disabled" in check.stdout_lines[12]'

tasks/language-pack.yml:

- name: Check language pack
  win_shell: |
    Get-WindowsPackage -Online `
                       -PackagePath 'C:\Programdata\cab\Microsoft-Windows-Client-Language-Pack_x64_sv-se.cab' `
                       | Select-Object `
                       -ExpandProperty PackageState
  register: check

- name: Install language pack
  win_shell: |
    Add-WindowsPackage -Online `
                       -PackagePath 'C:\Programdata\cab\Microsoft-Windows-Client-Language-Pack_x64_sv-se.cab' `
                       -NoRestart
  when: '"Installed" not in check.stdout'
  notify: Reboot

handlers/main.yml:

- name: Reboot
  win_reboot:
    post_reboot_delay: 60

result:

tasks run with status `changed` and the end of play unfolds...
...
RUNNING HANDLER [win-config : Reboot] ******************************************
Sunday 20 October 2019  18:48:55 +0200 (0:00:00.057)       0:46:55.167 ******** 
changed: [node1.example.com]

RUNNING HANDLER [win-config : Reboot] ******************************************
Sunday 20 October 2019  18:51:11 +0200 (0:02:16.527)       0:49:11.695 ******** 
fatal: [node1.example.com]: FAILED! => {
    "changed": false,
    "reboot": false
}

MSG:

The WS-Management service cannot process the request because the request contained invalid selectors for the resource.  (extended fault data: {'transport_message': 'Bad HTTP response returned from server. Code 500', 'http_status_code': 500, 'wsmanfault_code': '2150858843', 'fault_code': 's:Sender', 'fault_subcode': 'w:InvalidSelectors'})


NO MORE HOSTS LEFT *************************************************************

PLAY RECAP *********************************************************************
...

I have been led to believe that unless called for, the automatic flushing of handlers should only trigger each handler exactly once, even if its been notified more times during play.

Anyone seen this behavior? I had a second role, with handlers, with the exact same name. My first thought was that Ansible somehow called that handler as well, but excluding the other role, or renaming handlers have not solved it for me.

Can handlers be finicky about anything regarding WHAT calls them? Tasks from specific files, task/main.yml for example? Or should all notifications from within ONE play only trigger handlers ONCE?

1 Answers
Related