I’m looking for a playbook which would scan all servers and if a particular package (ex: kernel-3.10.0-1062 or nginx-1.12.2-2) is installed would send an email with the hostname.
Did anyone do something alike?
I'm looking for a playbook which would scan all servers and if a particular
package (ex: kernel-3.10.0-1062 or nginx-1.12.2-2) is installed would send
an email with the hostname.
Did anyone do something alike?
Collect "pkg_facts" and "intersect" the lists. For example
vars:
- pkg_list:
- linux-image-5.4.0-42-generic
- nginx-1.12.2
tasks:
- package_facts:
- mail:
body: "{{ send_pkg_list }}"
when: send_pkg_list|length > 0
vars:
send_pkg_list: "{{ ansible_facts.packages.keys()|
intersect(pkg_list) }}"
Thank you Vladimir!
This looks almost good, it finds the package only if no specific version is specified.
For example it will find nginx and send email but won’t find nginx-1.12.2
Laci,
... it finds the package only if no specific version is
specified. For example it will find nginx and send email but won't
find nginx-1.12.2
Try this. Loop "with_together" lists of packages' names and versions.
In the task's vars create list of the versions "pkgv". The
attribute "version" of the dictionary "ansible_facts.packages" is a
list because there may be more versions installed. In the condition
"when" test the searched version "item.1" is in the list "pkgv".
vars:
- pkg_list:
- nginx-1.12.2
- zip-3.0
tasks:
- package_facts:
- debug:
msg: "Found {{ item.0 }}-{{ item.1 }}"
with_together:
- "{{ pkg_list|
map('regex_replace','^(.*)-(.*)$', '\\1')|list }}"
- "{{ pkg_list|
map('regex_replace','^(.*)-(.*)$', '\\2')|list }}"
when: pkgv|map('regex_search', '^' ~ item.1)|list|length > 0
vars:
pkgn: "{{ ansible_facts.packages[item.0]|default() }}"
pkgv: "{{ pkgn|map(attribute='version')|list }}"
Fit the condition to your needs. It's trivial to create a list of
found packages in the loop test it and send an email.
Awesome, thank you very much!