使用Ansible列出已安装软件包的通用方法

问题描述:

现在,在当前设置中,我正在将Ansible与CentOs主机配合使用. 任务之一包含以下行:

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

如上所示,该任务仅对支持yum的主机有效,但是如果我想在Ubuntu或其他Linux发行版上运行它,该怎么办?
在网上进行了一些研究之后,我发现有一个名为package的通用软件包管理器.这样,我就可以安装/卸载软件包,而不必担心底层主机,但是不能像上面看到的那样对它们进行类似list的操作. 是否有任何模块或任何方式可以以通用方式实现此目标而无需创建多个when?

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?

一个选项是包括特定于OS的变量.例如:

An option is to include OS specific 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'