简体   繁体   English

使用 Ansible json_query 检查 Json Kubectl 命令的 Output

[英]Using Ansible json_query to Check Output of Json Kubectl command

I am trying to use Ansible to put a pause in my playbook, since I am installing an operator from the Operator Hub and don't want to continue, until I know the CRDs I require in the following steps are installed.我正在尝试使用 Ansible 暂停我的剧本,因为我正在从 Operator Hub 安装操作员并且不想继续,直到我知道我在以下步骤中需要的 CRD 已安装。 I have the following task but can't get it working yet.我有以下任务,但还不能让它工作。

- name: Wait for CRDs to be available
  command: kubectl get sub my-operator -n openshift-operators -o json
  register: cmd
  retries: 10
  delay: 5
  until: cmd.stdout | json_query('status.conditions[0].status') == true

Sample JSON样品 JSON

{
  "apiVersion": "operators.coreos.com/v1alpha1",
  "kind": "Subscription",
  "metadata": {
    "creationTimestamp": "2021-12-13T04:23:58Z",
    "generation": 1,
    "labels": {
      "operators.coreos.com/argocd-operator.openshift-operators": ""
    },
    "name": "argocd-operator",
    "namespace": "openshift-operators",
    "resourceVersion": "58122",
    "uid": "6eaad3c1-8329-4d00-90b8-1ab635b3b370"
  },
  "spec": {
    "channel": "alpha",
    "config": {
      "env": [
        {
          "name": "ARGOCD_CLUSTER_CONFIG_NAMESPACES",
          "value": "argocd"
        }
      ]
    },
    "installPlanApproval": "Automatic",
    "name": "argocd-operator",
    "source": "community-operators",
    "sourceNamespace": "openshift-marketplace",
    "startingCSV": "argocd-operator.v0.1.0"
  },
  "status": {
    "catalogHealth": [
      {
        "catalogSourceRef": {
          "apiVersion": "operators.coreos.com/v1alpha1",
          "kind": "CatalogSource",
          "name": "operatorhubio-catalog",
          "namespace": "olm",
          "resourceVersion": "57924",
          "uid": "95871859-edbc-45ad-885c-3edaad2a1df6"
        },
        "healthy": true,
        "lastUpdated": "2021-12-13T04:23:59Z"
      }
    ],
    "conditions": [
      {
        "lastTransitionTime": "2021-12-13T04:23:59Z",
        "message": "targeted catalogsource openshift-marketplace/community-operators missing",
        "reason": "UnhealthyCatalogSourceFound",
        "status": "True",
        "type": "CatalogSourcesUnhealthy"
      }
    ],
    "lastUpdated": "2021-12-13T04:23:59Z"
  }
}

There is a small detail that is tripping up the condition.有一个小细节会导致这种情况。 In the JSON output, the status is a string "True" and not a boolean which we are comparing.在 JSON output 中,状态是字符串"True" ,而不是我们正在比较的 boolean。

Note: "status": "True"注意: "status": "True"

Changing the condition to match the string True ...更改条件以匹配字符串True ...

until: cmd.stdout | json_query('status.conditions[0].status') == "True"

Or, applying the | bool或者,应用| bool | bool filter... | bool过滤器...

until: stdout | json_query('status.conditions[0].status') | bool

The complete task:完成任务:

- name: Wait for CRDs to be available
  command: kubectl get sub my-operator -n openshift-operators -o json
  register: cmd
  retries: 10
  delay: 5
  until: cmd.stdout | json_query('status.conditions[0].status') | bool

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM