DataDog / ansible-datadog

Ansible role for Datadog Agent
Apache License 2.0
298 stars 222 forks source link

Improve the version check on Windows and avoid slow idempotency. #526

Closed ldemers777 closed 8 months ago

ldemers777 commented 10 months ago

https://github.com/DataDog/ansible-datadog/blob/f53b1ab1d63c99410b832f22208470d23b30d71a/tasks/parse-version-windows.yml#L7

Maybe I missed something but I think a command like:

# ~1-2 seconds
# Adapt the "Select-Object" to your need (in this case, maybe just 'DisplayVersion')
Get-ItemProperty HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*, HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName,DisplayVersion,InstallDate,InstallLocation,PSChildName -unique | Sort-Object DisplayName | select-string "Datadog Agent"

# Output
@{DisplayName=Datadog Agent; DisplayVersion=7.20.2.1; InstallDate=20231108; InstallLocation=;
PSChildName={5A7DD234-696B-4999-852E-4176B1CF4F97}}

is far more efficient and complete than the actual implementation:

# ~1-2 minutes
Get-WmiObject -query "Select Name,IdentifyingNumber,InstallDate,InstallLocation,ProductID,Version FROM Win32_Product where Name like 'Datadog Agent'"

# Output
__GENUS           : 2
__CLASS           : Win32_Product
__SUPERCLASS      :
__DYNASTY         :
__RELPATH         :
__PROPERTY_COUNT  : 6
__DERIVATION      : {}
__SERVER          :
__NAMESPACE       :
__PATH            :
IdentifyingNumber : {5A7DD234-696B-4999-852E-4176B1CF4F97}
InstallDate       : 20231108
InstallLocation   :
Name              : Datadog Agent
ProductID         :
Version           : 7.20.2.1
PSComputerName    :

On a general note, consider identifying and optimizing slow Ansible steps in an idempotency context (where the target host is already properly installed/configured) because this role is not efficient in that matter (e.g. when this role is declared as a dependency of other roles often called).

ldemers777 commented 9 months ago

Can you do it? It is excruciating, I even noted that in some contexts it can take almost 20 minutes. This is madness.

From AWX (the target host is obfuscated because of sensitivity reasons): image

I am not likely to fork and create a merge request but at least I provide you with a code snippet that you could easily test and use.

# Here is one close to what you expect as output:
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName,DisplayVersion,PSChildName -unique | Where-Object {$_.DisplayName -clike 'Datadog Agent'} | Format-List

# Output
DisplayName    : Datadog Agent
DisplayVersion : 7.46.0.1
PSChildName    : {9E3023E6-5C8C-4284-86FC-ECCD68AB07A3}

Thanks.

KevinFairise2 commented 8 months ago

Hello, Thanks for the issue. We did not realise using this function was that inefficient. We already use Get-ItemProperty elsewhere in the code, the change is quite simple. Just opened this PR to do the change you proposed. https://github.com/DataDog/ansible-datadog/pull/536