json_query 帮助-未产生预期的输出

json_query 帮助-未产生预期的输出

我正在尝试构建一个数组来使用with_nested但是我无法理解为什么 json_query 没有输出分配给每个集群的实际主机:

玩:

- name: Index clusters.json
  shell: cat {{ tower_var_path }}/clusters.json
  register: result_clusters

- name: Save the Json data to a Variable as a Fact
  set_fact: 
    clusters_jsondata: "{{ result_clusters.stdout | from_json }}"

- debug:
    msg: " {{ clusters_jsondata }}"

- name: Set cluster facts
  set_fact:
    cluster: "{{ cluster|default([]) + [ {
      'name': item,
      'hosts': item | json_query('*.hosts[*].name')
       } ] }}"  
  with_items: "{{ clusters_jsondata.clusters }}"      

- debug: 
    msg: "{{ cluster }}"

clusters.json 数据:

    {
    "clusters": {
        "Cluster_1": {
            "hosts": [
                {
                    "folder": "/path",
                    "name": "host1.domain.com"
                },
                {
                    "folder": "/path",
                    "name": "host2.domain.com"
                }
            ],

        },
        "Cluster_2": {
            "hosts": [
                {
                    "folder": "/path",
                    "name": "host3.domain.com"
                },
                {
                    "folder": "/path",
                    "name": "host4.domain.com"
                }
            ],

        },
    }

预期成绩:

TASK [debug] *********************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        {
            "hosts": "host1.domain.com, host2.domain.com",
            "name": "Cluster_1"
        },
        {
            "hosts": "host3.domain.com, host4.domain.com",
            "name": "Cluster_2"
        }
    ]
}

实际结果:

    TASK [debug] *********************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        {
            "hosts": null,
            "name": "Cluster_1"
        },
        {
            "hosts": null,
            "name": "Cluster_2"
        }
    ]
}

答案1

简而言之,您只需一个表达式即可获得结果,无需任何循环。

以下剧本:

---
- hosts: localhost
  gather_facts: false

  vars:
    # Your original data on a single line for legibility
    result_clusters:
      stdout: >-
        {"clusters":{"Cluster_1":{"hosts":[{"folder":"/path","name":"host1.domain.com"},{"folder":"/path","name":"host2.domain.com"}]},"Cluster_2":{"hosts":[{"folder":"/path","name":"host3.domain.com"},{"folder":"/path","name":"host4.domain.com"}]}}}

  tasks:
    - name: get my expected output from json data in a single task
      vars:
        query: >-
          [].{name: key, hosts: join(', ', value.hosts[].name)}
        clusters: "{{ (result_clusters.stdout | from_json).clusters }}"
      debug:
        msg: "{{ clusters | dict2items | json_query(query) }}"

给出:

PLAY [localhost] ******************************************************

TASK [get my expected output from json data in a single task] *********
ok: [localhost] => {
    "msg": [
        {
            "hosts": "host1.domain.com, host2.domain.com",
            "name": "Cluster_1"
        },
        {
            "hosts": "host3.domain.com, host4.domain.com",
            "name": "Cluster_2"
        }
    ]
}

PLAY RECAP ************************************************************
localhost: ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

注意:尽管上面的结果非常精确地给出了您要求的输出,但我强烈怀疑您正在寻找略有不同的结果。将上述查询替换为:

        query: >-
          [].{name: key, hosts: value.hosts[].name}

将给出如下结果:

TASK [get my expected output from json data in a single task] **************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        {
            "hosts": [
                "host1.domain.com",
                "host2.domain.com"
            ],
            "name": "Cluster_1"
        },
        {
            "hosts": [
                "host3.domain.com",
                "host4.domain.com"
            ],
            "name": "Cluster_2"
        }
    ]
}

相关内容