Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 如何在ansible剧本中使用JMESPath进行排序?_Json_Ansible_Ansible Facts_Jmespath - Fatal编程技术网

Json 如何在ansible剧本中使用JMESPath进行排序?

Json 如何在ansible剧本中使用JMESPath进行排序?,json,ansible,ansible-facts,jmespath,Json,Ansible,Ansible Facts,Jmespath,在玩ansible playbook时,我有一个curl从领事处返回的数组: [ { "Node": { "Node": "test-eu-west-2-staging-0" }, "Node": { "Node": "test-nyc1-staging-0" }, "Node": { "Node": "test-sfo1-s

在玩ansible playbook时,我有一个curl从领事处返回的数组:

[
       { 
        "Node": {
            "Node": "test-eu-west-2-staging-0"
        },  
        "Node": {
            "Node": "test-nyc1-staging-0"
        },
        "Node": {
            "Node": "test-sfo1-staging-0"
        }
       }
]
我想让它在ansible playbook中按
“节点”:{“节点”:“主机名”}
排序

- name: Set TEST vars, servers info and number
    set_fact:
      TEST_SERVERS="{{ test.json | json_query('SOME SORTING QUERY') }}"
      TEST_SRV_NUM="{{ test.json | length }}"

我读了一整天JMESPath和ansible文档,但仍然不知道如何实现它(顺便说一句,在jq
sort\u by(.Node.Node)
中很容易,但这个技巧不适用于ansible)

正如@techraf所指出的,您的示例JSON格式不正确。我可以猜测您有一个包含
Node.Node
的对象列表,在这种情况下,您可以使用Jinja2过滤器:

---
- hosts: localhost
  gather_facts: no
  vars:
    myvar: [
       { "Node": { "Node": "test-c" } },
       { "Node": { "Node": "test-b" } },
       { "Node": { "Node": "test-a" } }
      ]
  tasks:
    - debug:
        msg: "{{ myvar | sort(attribute='Node.Node') }}"

首先,您发布的JSON格式不正确。