更新时间:2022-12-13 12:00:28
我可以使用不是幂等的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"