--- - name: Start the Windows Event Collector service and keep it on ansible.windows.win_service: name: Wecsvc start_mode: delayed state: started # Sources deliver to WinRM over HTTP on 5985. Kerberos encrypts the messages # inside, so HTTP here still means an encrypted channel. - name: Make sure WinRM listens on HTTP ansible.windows.win_powershell: script: | [CmdletBinding(SupportsShouldProcess)] param() $http = Get-ChildItem WSMan:\localhost\Listener | Where-Object { $_.Keys -contains 'Transport=HTTP' } if ($http) { $Ansible.Changed = $false; return } if ($PSCmdlet.ShouldProcess('WinRM', 'add an HTTP listener')) { New-Item -Path WSMan:\localhost\Listener -Transport HTTP -Address * -Force | Out-Null } # Windows Server 2016 and 2019 can run the collector service in its own # process, which is then refused the WinRM URLs, and nothing ever arrives. # Microsoft's fix, from "Event collector doesn't forward events", grants the # WinRM and Wecsvc service identities on both URLs. - name: Let the collector service use the WinRM URLs ansible.windows.win_powershell: script: | [CmdletBinding(SupportsShouldProcess)] param() $winrm = 'S-1-5-80-569256582-2953403351-2909559716-1301513147-412116970' $wec = 'S-1-5-80-4059739203-877974739-1245631912-527174227-2996563517' $Ansible.Changed = $false foreach ($url in 'http://+:5985/wsman/', 'https://+:5986/wsman/') { $now = (netsh http show urlacl url=$url) -join "`n" # No reservation means no listener on that URL, so nothing to fix. if ($now -notmatch 'Reserved URL' -or $now -match $wec) { continue } $Ansible.Changed = $true if ($PSCmdlet.ShouldProcess($url, 'grant WinRM and Wecsvc')) { netsh http delete urlacl url=$url | Out-Null netsh http add urlacl url=$url "sddl=D:(A;;GX;;;$winrm)(A;;GX;;;$wec)" | Out-Null if ($LASTEXITCODE -ne 0) { throw "netsh could not set the URL ACL for $url" } } } - name: Make the ForwardedEvents log big enough to ride out an outage downstream ansible.windows.win_powershell: parameters: Bytes: "{{ wef_collector_log_max_bytes }}" script: | [CmdletBinding(SupportsShouldProcess)] param([long]$Bytes) $now = [long](((wevtutil gl ForwardedEvents | Where-Object { $_ -like '*maxSize:*' }) -split ':')[1].Trim()) if ($now -ge $Bytes) { $Ansible.Changed = $false; return } if ($PSCmdlet.ShouldProcess('ForwardedEvents', "raise maximum size to $Bytes bytes")) { wevtutil sl ForwardedEvents "/ms:$Bytes" if ($LASTEXITCODE -ne 0) { throw 'wevtutil could not resize ForwardedEvents' } } - name: Make a folder for the subscription file ansible.windows.win_file: path: "{{ wef_collector_dir }}" state: directory - name: Write the subscription ansible.windows.win_template: src: subscription.xml.j2 dest: "{{ wef_collector_dir }}\\{{ wef_collector_subscription_name }}.xml" register: wef_collector_file # wecutil cannot update a subscription from a file, so a changed file means # delete and create. Sources reconnect at their next refresh. - name: Create the subscription, or replace it when the file changed ansible.windows.win_powershell: parameters: Name: "{{ wef_collector_subscription_name }}" File: "{{ wef_collector_dir }}\\{{ wef_collector_subscription_name }}.xml" Replace: "{{ wef_collector_file.changed }}" script: | [CmdletBinding(SupportsShouldProcess)] param([string]$Name, [string]$File, [bool]$Replace) wecutil gs $Name *> $null $exists = $LASTEXITCODE -eq 0 if ($exists -and -not $Replace) { $Ansible.Changed = $false; return } if ($PSCmdlet.ShouldProcess($Name, 'create the subscription')) { if ($exists) { wecutil ds $Name | Out-Null } wecutil cs $File if ($LASTEXITCODE -ne 0) { throw "wecutil could not create $Name from $File" } } - name: Read the state of every source ansible.windows.win_command: wecutil gr {{ wef_collector_subscription_name }} changed_when: false register: wef_collector_runtime when: not ansible_check_mode - name: Show it ansible.builtin.debug: msg: "{{ wef_collector_runtime.stdout_lines }}" when: not ansible_check_mode