65 lines
2.2 KiB
YAML
65 lines
2.2 KiB
YAML
---
|
|
- name: "Checkmk | Cleanup agent"
|
|
hosts: all
|
|
gather_facts: true
|
|
|
|
tasks:
|
|
- name: "Windows | Remove installed Checkmk agent"
|
|
when: ansible_facts['os_family'] == 'Windows'
|
|
ansible.windows.win_powershell:
|
|
script: |
|
|
$Ansible.Changed = $false
|
|
$uninstallRoots = @(
|
|
'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',
|
|
'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
|
|
)
|
|
|
|
$products = foreach ($root in $uninstallRoots) {
|
|
if (Test-Path -LiteralPath $root) {
|
|
Get-ChildItem -LiteralPath $root -ErrorAction SilentlyContinue |
|
|
ForEach-Object {
|
|
$product = Get-ItemProperty -LiteralPath $_.PSPath -ErrorAction SilentlyContinue
|
|
if ($product.DisplayName -like 'Check MK Agent*' -or
|
|
$product.DisplayName -like 'Checkmk Agent*') {
|
|
[PSCustomObject]@{
|
|
ProductCode = $_.PSChildName
|
|
DisplayName = $product.DisplayName
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach ($product in $products) {
|
|
if ($product.ProductCode -match '^\{[0-9A-Fa-f-]+\}$') {
|
|
$process = Start-Process -FilePath 'msiexec.exe' `
|
|
-ArgumentList "/x $($product.ProductCode) /qn /norestart" `
|
|
-Wait -PassThru
|
|
|
|
if ($process.ExitCode -notin @(0, 1605, 1614, 3010)) {
|
|
throw "Failed to uninstall $($product.DisplayName). MSI exit code: $($process.ExitCode)"
|
|
}
|
|
|
|
if ($process.ExitCode -in @(0, 3010)) {
|
|
$Ansible.Changed = $true
|
|
}
|
|
}
|
|
}
|
|
|
|
$Ansible.Result = @{
|
|
removed_products = @($products.DisplayName)
|
|
}
|
|
|
|
- name: "Debian | Remove Checkmk agent"
|
|
when: ansible_facts['os_family'] == 'Debian'
|
|
ansible.builtin.apt:
|
|
name: check-mk-agent
|
|
state: absent
|
|
purge: true
|
|
|
|
- name: "RedHat | Remove Checkmk agent"
|
|
when: ansible_facts['os_family'] == 'RedHat'
|
|
ansible.builtin.dnf:
|
|
name: check-mk-agent
|
|
state: absent
|