解析json命令时遇到问题

时间:2020-10-01 19:40:36

标签: json ansible

我不确定我在这里做错了什么,但是我已经多次解析这样的json了,以至于我数不清了,不知道为什么这会给我带来麻烦。

这是我的命令正在返回的有效 JSON。

{
    "apiVersion": "v1",
    "items": [
        {
            "apiVersion": "v1",
            "conditions": [
                {
                    "message": "ok",
                    "status": "True",
                    "type": "Healthy"
                }
            ],
            "metadata": {
                "creationTimestamp": null,
                "name": "controller-manager",
                "selfLink": "/api/v1/componentstatuses/controller-manager"
            }
        },
        {
            "apiVersion": "v1",
            "conditions": [
                {
                    "message": "ok",
                    "status": "True",
                    "type": "Healthy"
                }
            ],
            "metadata": {
                "creationTimestamp": null,
                "name": "scheduler",
                "selfLink": "/api/v1/componentstatuses/scheduler"
            }
        }
     ]
}

这是我的代码:

- name: "validating controlplane is operational"
  block:
    - name: "querying kube component status"
      command: kubectl --kubeconfig={{ remote_cert_dir }}/admin.kubeconfig get componentstatuses -o json
      register: controlplane_check

    - name: "registering fact: 'kube_status'"
      set_fact:
        kube_status: "{{ controlplane_check.stdout|from_json }}"

    - name: "verifying all controlplane components are healthy"
     assert:
        that:
          - item.conditions[0].status == "True"
        fail_msg: "ERROR: {{ item.metadata.name }} is unhealthy"
        success_msg: " {{ item.meteadata.name }} is healthy"
      with_items:
        - "{{ kube_status.items }}"

这是我得到的错误:

TASK [../../roles/validate-controlplane : verifying all controlplane components are healthy] *********************************************************************************************************************************************************************************************************************
Thursday 01 October 2020  12:48:55 -0700 (0:00:00.074)       0:00:02.055 ******
fatal: [st53p01nt-quju11200101.example.com]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'metadata'\n\nThe error appears to be in 'kubernetes/on-prem/automation/roles/validate-controlplane/tasks/main.yml': line 12, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: \"verifying all controlplane components are healthy\"\n      ^ here\n"}

当我在成功/失败语句中删除变量时,它会吠叫着:

TASK [../../roles/validate-controlplane : verifying all controlplane components are healthy] *********************************************************************************************************************************************************************************************************************
Thursday 01 October 2020  12:47:23 -0700 (0:00:00.070)       0:00:02.105 ******
fatal: [st53p01nt-quju11200101.example.com]: FAILED! => {"msg": "The conditional check 'item.conditions[0].status == \"True\"' failed. The error was: error while evaluating conditional (item.conditions[0].status == \"True\"): 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'conditions'"}

通常,我什至不使用from_json中介步骤,但我在进行故障排除时在此处添加了该步骤。也许是睡眠不足,但我看不到我在哪里做错了。

0 个答案:

没有答案