--- - name: Stop if the installer or its checksum was not set ansible.builtin.assert: that: - osquery_msi_url is match('https://.+\.msi$') - osquery_msi_sha256 | length == 64 fail_msg: Set osquery_msi_url to the Windows .msi, and osquery_msi_sha256 to its SHA-256. - name: Download osquery, refusing it if the checksum differs ansible.windows.win_get_url: url: "{{ osquery_msi_url }}" dest: C:\Windows\Temp\osquery.msi checksum: "{{ osquery_msi_sha256 }}" checksum_algorithm: sha256 - name: Install it ansible.windows.win_package: path: C:\Windows\Temp\osquery.msi state: present notify: Restart osqueryd - name: Make the log folder ansible.windows.win_file: path: "{{ osquery_dir }}\\log" state: directory - name: Write the daemon flags ansible.windows.win_copy: dest: "{{ osquery_dir }}\\osquery.flags" content: | --config_path={{ osquery_dir }}\osquery.conf --logger_plugin=filesystem --logger_path={{ osquery_dir }}\log --disable_events=true notify: Restart osqueryd # Written beside the live file, checked by osqueryd, then copied over it, so # a broken change never replaces a working one. - name: Write the scheduled questions next to the live file ansible.windows.win_copy: dest: "{{ osquery_dir }}\\osquery.conf.new" content: "{{ {'schedule': osquery_queries | combine(osquery_extra_queries)} | to_nice_json }}\n" - name: Check them with osquery ansible.windows.win_powershell: parameters: Conf: "{{ osquery_dir }}\\osquery.conf.new" script: | param([string]$Conf) $Ansible.Changed = $false $svc = Get-CimInstance Win32_Service -Filter "Name='osqueryd'" $exe = if ($svc.PathName -match '^"([^"]+)"') { $Matches[1] } else { ($svc.PathName -split ' ')[0] } $out = & $exe --config_check --config_path=$Conf 2>&1 if ($LASTEXITCODE -ne 0) { throw "osquery rejected the configuration: $out" } when: not ansible_check_mode - name: Put them live ansible.windows.win_copy: src: "{{ osquery_dir }}\\osquery.conf.new" dest: "{{ osquery_dir }}\\osquery.conf" remote_src: true when: not ansible_check_mode notify: Restart osqueryd - name: Point the service at the flag file ansible.windows.win_powershell: parameters: Flags: "{{ osquery_dir }}\\osquery.flags" script: | [CmdletBinding(SupportsShouldProcess)] param([string]$Flags) $svc = Get-CimInstance Win32_Service -Filter "Name='osqueryd'" if (-not $svc) { if ($WhatIfPreference) { return }; throw 'The osqueryd service is missing. Did the install succeed?' } $exe = if ($svc.PathName -match '^"([^"]+)"') { $Matches[1] } else { ($svc.PathName -split ' ')[0] } $want = "`"$exe`" --flagfile=`"$Flags`"" if ($svc.PathName -eq $want) { $Ansible.Changed = $false; return } if ($PSCmdlet.ShouldProcess('osqueryd', "run as $want")) { sc.exe config osqueryd binPath= $want | Out-Null if ($LASTEXITCODE -ne 0) { throw 'sc.exe could not change the osqueryd service command line' } } notify: Restart osqueryd - name: Make sure osqueryd is automatic and running ansible.windows.win_service: name: osqueryd start_mode: auto state: started - name: Flush handlers so the check below sees the new configuration ansible.builtin.meta: flush_handlers - name: Confirm the service is running with this flag file ansible.windows.win_powershell: parameters: Flags: "{{ osquery_dir }}\\osquery.flags" script: | param([string]$Flags) $Ansible.Changed = $false $svc = Get-CimInstance Win32_Service -Filter "Name='osqueryd'" if ($svc.State -ne 'Running') { throw "osqueryd is $($svc.State)." } if ($svc.PathName -notlike "*$Flags*") { throw "osqueryd is not reading $Flags." } when: not ansible_check_mode