Generic way to list installed packages using Ansible

Viewed 1825

Right now in my current setup I am using Ansible with CentOs hosts. One of the tasks contains the following line:

command: yum list installed somepackagename

As seen above the task will only work for hosts which support yum , but what if I want to run it on Ubuntu or some other Linux distribution?
After researching a bit online I found out that there is a generic package manager called package. With this I install/uninstall packages without worrying about the underlying hosts but not do something like list them as seen above. Is there any module or any way I could achieve this in a generic way without creating multiple whens?

1 Answers

Q: "Generic way to list installed packages using Ansible"

A: Use module package_facts. The module returns facts in ansible_facts.packages

- name: Gather the package facts
  package_facts:
- name: Print the package facts
  debug:
    var: ansible_facts.packages

Q: "There is a generic package manager called package ... install/uninstall packages without worrying about the underlying hosts"

A: An option of how not to "worry about the underlying hosts" completely is to include OS-specific packages in the variables. For example:

    - name: vars
      include_vars:
        file: "{{ item }}"
      with_first_found:
        - files:
            - "{{ ansible_distribution }}-{{ ansible_distribution_release }}.yml"
            - "{{ ansible_distribution }}.yml"
            - "{{ ansible_os_family }}.yml"
            - "default.yml"
          paths: '{{ role_path }}/vars'
Related