如何使用Ansible获取已安装的yum软件包? [英] How to get the installed yum packages with Ansible?

查看:562
本文介绍了如何使用Ansible获取已安装的yum软件包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在RHEL机器上获取所有已安装的yum软件包.我可以使用不是幂等的shell命令轻松获得它,而是想使用yum命令.

I am trying to get all the installed yum package on an RHEL machine. I can easily get it through using shell commands which is not idempotent and would like to use the yum command instead.

Shell命令正常工作:

Shell command works fine:

- name: yum list packages
  shell: yum list installed > build_server_info.config

但是当我尝试使用yum命令时,它只是执行但没有给出任何结果:

But when I try to use the yum command it just executes but do not give any results:

- name: yum_command 
  action: yum list=${pkg} list=available

推荐答案

我可以使用不是幂等的shell命令轻松获得它

I can easily get it through using shell commands which is not idempotent

当您查询计算机的当前状态时,您不能真正谈论幂等.

You can't really talk about idempotence, when you are querying the current state of a machine.

"Idempontent"(临时)意味着无论您执行某项任务多少次,该任务都将确保计算机处于所需状态.

"Idempontent" means that the task will ensure the machine is in the desired state no matter how many times you run a certain task.

查询当前状态时,没有描述所需的状态.无论您做什么,使用什么方法,幂等"一词都是不适用的.

When you query current state, you don't describe the desired state. No matter what you do, what method you use, the term "idempotent" is just not applicable.

关于您的示例,该示例不会给您带来结果-您重复了两次相同的参数list,并且该任务应该失败(它不会,这看起来像Ansible怪癖).

Regarding your example, which does not give you results - you have repeated twice the same argument list and the task should fail (it doesn't, which looks like an Ansible quirk).

要获取已安装软件包的列表,请使用:

To get a list of installed packages, you should use:

- name: yum_command 
  yum:
    list=installed
  register: yum_packages

- debug:
    var: yum_packages

它将描述每个程序包的字典列表保存到变量yum_packages.

It saves a list of dictionaries describing each package to a variable yum_packages.

然后,您可以使用 JSON查询过滤器来获取单个软件包(tar):

You can then use a JSON Query Filter to get a single package (tar):

- debug: var=item
  with_items: "{{yum_packages|json_query(jsonquery)}}"
  vars:
    jsonquery: "results[?name=='tar']"

获得这样的结果:

"item": {
    "arch": "x86_64",
    "epoch": "2",
    "name": "tar",
    "nevra": "2:tar-1.26-31.el7.x86_64",
    "release": "31.el7",
    "repo": "installed",
    "version": "1.26",
    "yumstate": "installed"
}

或仅其版本:

- debug: var=item
  with_items: "{{yum_packages|json_query(jsonquery)}}"
  vars:
    jsonquery: "results[?name=='tar'].version"

"item": "1.26"

这篇关于如何使用Ansible获取已安装的yum软件包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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