ansible json filter list

∥☆過路亽.° 提交于 2019-12-11 17:25:06

问题


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')}}"

...but I think I'm somewhere near. I guess something is missing in this json_query :(


回答1:


The jmespath expression returning a list with the name of running services is this :

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


来源:https://stackoverflow.com/questions/49887399/ansible-json-filter-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!