如何使用 Ansible 获取已安装的 yum 包? [英] How to get the installed yum packages with Ansible?
问题描述
我正在尝试在 RHEL 机器上获取所有已安装的 yum 包.我可以很容易地通过使用非幂等的 shell 命令来实现它,我想改用 yum 命令.
Shell 命令工作正常:
- name: yum 列出包shell: yum list 已安装 >build_server_info.config
但是当我尝试使用 yum 命令时,它只是执行但不给出任何结果:
- 名称:yum_command行动:yum list=${pkg} list=available
我可以使用非幂等的 shell 命令轻松获得它
当您查询机器的当前状态时,您不能真正谈论幂等性.
幂等"意味着无论您运行某个任务多少次,该任务都会确保机器处于期望状态.
当您查询当前状态时,您没有描述所需的状态.不管你做什么,你用什么方法,幂等"这个词都不适用.
<小时>关于你的例子,它没有给你结果 - 你重复了两次相同的参数 list
并且任务应该失败(它没有,这看起来像 Ansible 怪癖).>
要获取已安装软件包的列表,您应该使用:
- 名称:yum_command嗯:列表=已安装注册:yum_packages- 调试:变量:yum_packages
它将描述每个包的字典列表保存到变量 yum_packages
中.
然后您可以使用 JSON 查询过滤器来获取单个包 (tar
):
- 调试:var=itemwith_items: "{{yum_packages|json_query(jsonquery)}}"变量:jsonquery: "results[?name=='tar']"
得到这样的结果:
项目":{"arch": "x86_64","纪元": "2","name": "焦油","nevra": "2:tar-1.26-31.el7.x86_64","发布": "31.el7","repo": "已安装","版本": "1.26","yumstate": "已安装"}
或者只是它的版本:
- 调试:var=itemwith_items: "{{yum_packages|json_query(jsonquery)}}"变量:jsonquery: "results[?name=='tar'].version"
"item": "1.26"
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 command works fine:
- name: yum list packages
shell: yum list installed > build_server_info.config
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
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" 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.
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
It saves a list of dictionaries describing each package to a variable yum_packages
.
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']"
to get a result like this:
"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"
}
Or only its version:
- debug: var=item
with_items: "{{yum_packages|json_query(jsonquery)}}"
vars:
jsonquery: "results[?name=='tar'].version"
"item": "1.26"
这篇关于如何使用 Ansible 获取已安装的 yum 包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!