Add key to a List of dicts depending of a simple Array - Ansible Facts (package_facts)

Hello everybody,

I’m currently working on generating report for each inventory host thanks to ansible-cmdb. This tool is great but I have a problem to modify some facts:

I use the package_facts module to register installed packages. They are automatically registered in hostvars as a list of dicts like this (ansible_facts.packages):

“packages”: {
“accountsservice”: [
{
“arch”: “amd64”,
“name”: “accountsservice”,
“source”: “apt”,
“version”: “0.6.40-2ubuntu11.3”
}
],
“acl”: [
{
“arch”: “amd64”,
“name”: “acl”,
“source”: “apt”,
“version”: “2.2.52-3”
}
],
“acpid”: [
{
“arch”: “amd64”,
“name”: “acpid”,
“source”: “apt”,
“version”: “1:2.0.26-1ubuntu2”
}
]

}

On the other side, I have a remote bash script which outputs a simple list of package name manually installed on the host. For each host, I want to compare the two list, if a package name is also in ansible_facts.packages, then add a key in packages fact for the desired package “manual”: true. Otherwise, and if possible, add “manual”: false. So, after comparison, facts should be set like this:

“packages”: {
“accountsservice”: [
{
“arch”: “amd64”,
“name”: “accountsservice”,
“source”: “apt”,
“version”: “0.6.40-2ubuntu11.3”,
“manual”: true

}
],
“acl”: [
{
“arch”: “amd64”,
“name”: “acl”,
“source”: “apt”,
“version”: “2.2.52-3”,
“manual”: false

}
],

}

Do you think it is possible in a proper way? I’m a newbie concerning handling JSON datastructures with Ansible, that is why I will do a workaround with a Python script since I’m working since some days on a proper way (with no success).

It would be a big help/step if I know how to do it properly.
Thanks in advance !

Have a nice day,

Lucas