ansible json 过滤器列表

ansible json filter list

我正在尝试从 scan_services 模块(或 >= 2.5 中的 service_facts)的输出中过滤掉 运行 服务。这个模块的输出是这样的:

    "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"
        },

现在,我要做的是仅列出状态为:运行... 这段代码似乎根本不起作用:/

---
- 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 中缺少某些东西 :(

返回名为 运行 服务的列表的 jmespath 表达式是这样的:

ansible_facts.services.* | [?state == `running`].name

你可以试试这个:

- name: Get Service Facts
  service_facts:

- name: Get running services
  set_fact:
    services_runnig: "{{ ansible_facts | json_query('services.* | [?state == `running`].name') }}"

- name: Debug
  debug:
    msg: "{{ services_running }}"