DataDog / ansible-datadog

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

Install will not work if deb package was removed from apt prior #519

Closed sosafe-bastian-bretagne closed 6 months ago

sosafe-bastian-bretagne commented 11 months ago

I faced an issue where in the role code dpkg -s is used to find if the package is installed. Although if the package was installed using apt first, then uninstalled, dpkg will keep the agent in the cache. As the role will skip the install because it only looks for the output of the version and not the status it will still think that the agent is installed:

step to reproduce:

1. install the package (add repo and apt install ...)

root@bastian-test:~# apt install -y datadog-agent
...
root@bastian-test:~# dpkg -s datadog-agent |grep status -i
Status: install ok installed

root@bastian-test:~# apt list --installed |grep datadog-agent
datadog-agent/unknown,now 1:7.48.0-1 amd64 [installed]

2. uninstall the package using apt:

root@bastian-test:~# apt remove -y datadog-agent
The following packages will be REMOVED:
  datadog-agent
0 upgraded, 0 newly installed, 1 to remove and 5 not upgraded.
After this operation, 1,288 MB disk space will be freed.
(Reading database ... 138517 files and directories currently installed.)
Removing datadog-agent (1:7.48.0-1) ...
Removed /etc/systemd/system/multi-user.target.wants/datadog-agent-process.service.
Removed /etc/systemd/system/multi-user.target.wants/datadog-agent-trace.service.
Removed /etc/systemd/system/multi-user.target.wants/datadog-agent-security.service.
Removed /etc/systemd/system/multi-user.target.wants/datadog-agent.service.
Removing integrations installed with the 'agent integration' command
Removing version history file
find: ‘/opt/datadog-agent/run/runtime-security/profiles’: No such file or directory
Removing remote configuration config database

root@bastian-test:~# apt list --installed |grep datadog-agent
root@bastian-test:~#

3. verify that dpkg still show the version despite package being uninstalled:

this time you can see that its uninstalled

root@bastian-test:~# dpkg -s datadog-agent | grep '^Version:' | awk '{print $2}'
1:7.48.0-1

root@bastian-test:~# dpkg -s datadog-agent |grep status -i
Status: deinstall ok config-files

as a result when running the role the following will happen:

ansible@47ff6daab7c9:~/ansible$ ansible-playbook -i ...

# [many other stuff....]

TASK [datadog-4.20.1 : Set skip install flag if version already installed (Linux)] *********************************************************************************************
ok: [api-bastian.my-domain]

# [many other stuff....]

TASK [datadog-4.20.1 : Ensure datadog-agent is running] ************************************************************************************************************************
fatal: [api-bastian.my-domain]: FAILED! => {"changed": false, "msg": "Could not find the requested service datadog-agent: host"}

Proposed solution: instead of checking only the version it could check the status too?

bkabrda commented 6 months ago

Hi :wave: thanks a lot for the very detailed issue report. I hadn't realized that dpkg did this. I think this should be fairly simple to fix by using something like dpkg-query --showformat '${Status} ${Version}\n' --show datadog-agent | grep installed | awk '{print $NF}'. I'll do some testing and submit a PR when I have something solid.