0
votes

I have several packages (package-A, package-B, ...) installed through various hosts (host1, host2, ...).

I need to check if package-A with version 1.2.3, for example, is installed in host1, host2 and host3, need to check if package-B with version 3.2.1 is installed in host1, host3 and host9, ..., informing the user that they are installed or not with the specified version.

I have a playbook (validatorPlaybook.yml) who's purpose is to check and inform the user about installed versions in certain hosts defined in a dictionary in an outside playbook varsPlayBook1.yml:

#validation dictionary
packsDict:
pck1:
    hosts: host1, host2, host3
    version: 1.2.3
    package_name: package-A
pck2:
    hosts: host1, host3, host9
    version: 3.2.1
    package_name: package-B

(...)

This is the script I'm using in validatorPlaybook.yml:

---
- name: check if packages in dict are with the correct version
yum:
    list="{{ item.package_name }}-{{ item.version }}"
loop: "{{ lookup('dict', packsDict) }}"
...

As you can see some packages are installed in one host and others are in another. How can I run that yum list command by host? Is it possible?

1
Your goal is unclear to me. Can perhaps improve your question and also provide an example of the outcome?Kevin C
@KevinC edited the question as requestedEunito

1 Answers

0
votes

Note: I will not discuss here the overall structure and goal of your current implementation. I think it can be greatly improved (by defining packages to check in inventory for example...)


  1. The easiest solution I see from the existing script requires to transform your validation data a bit. Put the hosts in a list like so:

    packsDict:
      pck1:
        hosts:
          - host1
          - host2
          - host3
        version: 1.2.3
        package_name: package-A
      pck2:
        hosts:
          - host1
          - host3
          - host9
        version: 3.2.1
        package_name: package-B
    
  2. I take for granted that your actual playbook already runs on all needed hosts and that the names inside your validation dict are aligned to the names of hosts in your inventory. Modifying your task like the following should meet your requirement.

    - name: check if packages in dict are with the correct version
      yum:
        list: "{{ item.package_name }}-{{ item.version }}"
      loop: "{{ lookup('dict', packsDict) }}"
      when: inventory_hostname in item.hosts