--- - name: Stop if no collector was set ansible.builtin.assert: that: - wef_source_collector_fqdn | length > 0 fail_msg: Set wef_source_collector_fqdn in group_vars. There is no sensible default for this. # Without this the subscription shows the source as active and the Security # and Sysmon logs never arrive, which looks like a quiet machine. - name: Let the forwarder read the logs it sends ansible.windows.win_powershell: parameters: Logs: "{{ wef_source_readable_logs }}" script: | [CmdletBinding(SupportsShouldProcess)] param([string[]]$Logs) $Ansible.Changed = $false foreach ($log in $Logs) { $line = wevtutil gl $log 2>$null | Where-Object { $_ -like 'channelAccess:*' } if (-not $line) { # In a preview of a first run, Sysmon and its log do not exist yet. if ($WhatIfPreference) { $Ansible.Changed = $true; continue } throw "$log does not exist on this machine" } $sddl = ($line -replace '^channelAccess:\s*', '').Trim() if ($sddl -like '*(A;;0x1;;;NS)*') { continue } $Ansible.Changed = $true if ($PSCmdlet.ShouldProcess($log, 'grant NETWORK SERVICE read')) { wevtutil sl $log "/ca:$sddl(A;;0x1;;;NS)" if ($LASTEXITCODE -ne 0) { throw "wevtutil could not update access on $log" } } } - name: Point the source at the collector ansible.windows.win_regedit: path: HKLM:\SOFTWARE\Policies\Microsoft\Windows\EventLog\EventForwarding\SubscriptionManager name: "1" data: "Server=http://{{ wef_source_collector_fqdn }}:5985/wsman/SubscriptionManager/WEC,Refresh={{ wef_source_refresh_seconds }}" type: string notify: Tell the forwarder - name: Keep WinRM running, since it does the forwarding ansible.windows.win_service: name: WinRM start_mode: auto state: started - name: Flush handlers so the check below sees the new setting ansible.builtin.meta: flush_handlers # Event 104 or 105 in this log means the source cannot reach or authenticate # to the collector. Reporting the latest entry turns a silent gap into a line # in the run output. - name: Report what the forwarder last said ansible.windows.win_powershell: script: | $Ansible.Changed = $false $last = Get-WinEvent -LogName 'Microsoft-Windows-Forwarding/Operational' -MaxEvents 1 -ErrorAction SilentlyContinue if ($last) { $Ansible.Result = @{ id = $last.Id; time = $last.TimeCreated.ToString('o'); message = $last.Message } } else { $Ansible.Result = @{ id = 0; message = 'No forwarder events yet. Check again after the refresh interval.' } } register: wef_source_forwarder when: not ansible_check_mode - name: Show the forwarder status ansible.builtin.debug: msg: "{{ wef_source_forwarder.result }}" when: not ansible_check_mode