Ansible JSON过滤器列表 [英] ansible json filter list
本文介绍了Ansible JSON过滤器列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从scan_services模块的输出(或> = 2.5中的service_facts)的输出中过滤掉正在运行的服务.该模块的输出如下所示:
I'm trying to filter out running services from the output of scan_services module (or service_facts in >= 2.5). The output of this module is something like this:
"ansible_facts": {
"services": {
"NetworkManager-dispatcher.service": {
"name": "NetworkManager-dispatcher.service",
"source": "systemd",
"state": "running"
},
"NetworkManager-wait-online.service": {
"name": "NetworkManager-wait-online.service",
"source": "systemd",
"state": "stopped"
},
"NetworkManager.service": {
"name": "NetworkManager.service",
"source": "systemd",
"state": "running"
},
现在,我要尝试的是仅列出以下状态的服务名称:运行中... 这段代码似乎根本不起作用:/
Now, what I'm trying to do is to list only the service names of the ones that are state: running... This code doesn't seems to work at all :/
---
- hosts: all
tasks:
- name : Scan current services status
scan_services:
register: running_services
- name: print
debug: var=item
with_items: "{{ running_services.|json_query('ansible_facts.services.[?state=='running'].name')}}"
...但是我想我在附近. 我猜这个json_query中缺少一些东西:(
...but I think I'm somewhere near. I guess something is missing in this json_query :(
推荐答案
返回正在运行的服务名称的列表的jmespath表达式是这样的:
The jmespath expression returning a list with the name of running services is this :
ansible_facts.services.* | [?state == `running`].name
这篇关于Ansible JSON过滤器列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文