需要从ansible json stdout获取值并将它们放入列表中

时间:2019-04-11 19:56:41

标签: ansible

输入以下内容:

"stdout_lines": [  
            "{",  
            "    \"apiVersion\": \"v1\",",  
            "    \"items\": [",  
            "        {",  
            "            \"apiVersion\": \"v1\",",  
            "            \"kind\": \"Namespace\",",  
            "            \"metadata\": {",  
            "                \"annotations\": {",  
            "                    \"kubectl.kubernetes.io/last-applied-configuration\": \"{\\\"apiVersion\\\":\\\"v1\\\",\\\"kind\\\":\\\"Namespace\\\",\\\"metadata\\\":{\\\"annotations\\\":{},\\\"labels\\\":{\\\"istio-injection\\\":\\\"enabled\\\"},\\\"name\\\":\\\"test-rewards\\\"}}\\n\"",  
            "                },",  
            "                \"creationTimestamp\": \"2018-09-25T23:45:37Z\",",  
            "                \"labels\": {",  
            "                    \"istio-injection\": \"enabled\"",  
            "                },",  
            "                \"name\": \"test-rewards\",",  
            "                \"resourceVersion\": \"7399723\",",  
            "                \"selfLink\": \"/api/v1/namespaces/test-rewards\",",  
            "                \"uid\": \"1a8e3-c11d-11e8-9986-8acd436f34\"",  
            "            },",  
            "            \"spec\": {",  
            "                \"finalizers\": [",  
            "                    \"kubernetes\"",  
            "                ]",  
            "            },",  
            "            \"status\": {",  
            "                \"phase\": \"Active\"",  
            "            }",  
            "        },",  
            "        {",  
            "            \"apiVersion\": \"v1\",",  
            "            \"kind\": \"Namespace\",",  
            "            \"metadata\": {",  
            "                \"creationTimestamp\": \"2018-09-26T17:33:42Z\",",  
            "                \"labels\": {",  
            "                    \"istio-injection\": \"enabled\"",  
            "                },",  
            "                \"name\": \"inventory\",",  
            "                \"resourceVersion\": \"15021\",",  
            "                \"selfLink\": \"/api/v1/namespaces/customer-inventory\",",  
            "                \"uid\": \"4fd5bfb6-c1b2-11e8-9986-8a3fc36f34\"",  
            "            },",  
            "            \"spec\": {",  
            "                \"finalizers\": [",  
            "                    \"kubernetes\"",  
            "                ]",  
            "            },",  
            "            \"status\": {",  
            "                \"phase\": \"Active\"",  
            "            }",  
            "        },",  

我正在尝试遍历循环并获取“名称”的所有值并将其保存在列表中。我有以下任务。但不起作用

例如值:

\"name\": \"test-rewards\",",

\"name\": \"inventory\",",

此任务:

- name: Show object id  
  set_fact:  
    group_object_value: "{{ (namespaces.stdout_lines | from_json).items[].metadata.name }}"   
  with_items: "{{ namespaces.stdout }}" 

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

似乎您在commandshell任务的标准输出中包含JSON数据。您要使用stdout属性,而不要使用stdout_lines;前者是单个文本块,而后者是列表,每行输入一项。 from_json过滤器仅适用于简单的字符串变量,例如stdout属性。

一旦反序列化了JSON数据,就可以使用json_query过滤器来获取所需的内容。例如,此剧本:

---
- hosts: localhost
  gather_facts: false
  vars:
    namespaces:
      stdout: |
        {
          "apiVersion": "v1",
          "items": [
            {
              "apiVersion": "v1",
              "kind": "Namespace",
              "metadata": {
                "annotations": {
        "kubectl.kubernetes.io/last-applied-configuration":"{\"apiVersion\":\"v1\",\"kind\":\"Namespace\",\"metadata\":{\"annotations\":{},\"labels\":{\"istio-injection\":\"enabled\"},\"name\":\"test-rewards\"}}\n"
                },
                "creationTimestamp": "2018-09-25T23:45:37Z",
                "labels": {
                  "istio-injection": "enabled"
                },
                "name": "test-rewards",
                "resourceVersion": "7399723",
                "selfLink": "/api/v1/namespaces/test-rewards",
                "uid": "1a8e3-c11d-11e8-9986-8acd436f34"
              },
              "spec": {
                "finalizers": [
                  "kubernetes"
                ]
              },
              "status": {
                "phase": "Active"
              }
            },
            {
              "apiVersion": "v1",
              "kind": "Namespace",
              "metadata": {
                "creationTimestamp": "2018-09-26T17:33:42Z",
                "labels": {
                  "istio-injection": "enabled"
                },
                "name": "inventory",
                "resourceVersion": "15021",
                "selfLink": "/api/v1/namespaces/customer-inventory",
                "uid": "4fd5bfb6-c1b2-11e8-9986-8a3fc36f34"
              },
              "spec": {
                "finalizers": [
                  "kubernetes"
                ]
              },
              "status": {
                "phase": "Active"
              }
            }
          ]
        }
  tasks:
    - debug:
        var: namespaces.stdout|from_json|json_query('items[].metadata.name')

将产生以下输出:

TASK [debug] *******************************************************************
ok: [localhost] => {
    "namespaces.stdout|from_json|json_query('items[].metadata.name')": [
        "test-rewards", 
        "inventory"
    ]
}

因此,如果您希望变量group_object_value包含这些值,则应编写:

- set_fact:
    group_object_value: "{{ namespaces.stdout|from_json|json_query('items[].metadata.name') }}"