ansible json 过滤器列表 [英] ansible json filter list

查看:37
本文介绍了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屋!

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