使用 Ansible 列出已安装包的通用方法 [英] Generic way to list installed packages using Ansible

查看:45
本文介绍了使用 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 s?

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?

推荐答案

Q: 使用 Ansible 列出已安装包的通用方法"

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

A:使用模块 package_facts.该模块在 ansible_facts.packages

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

<小时>

问:"有一个通用的包管理器叫做 package ... 安装/卸载包,无需担心底层主机"


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

A:如何不担心底层主机"的选项完全是在变量中包含特定于操作系统的包.例如:

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'

这篇关于使用 Ansible 列出已安装包的通用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆