ansible - 如何在多个字段上使用 with_items 从 ansible k8s 模块检索/解析结果

标签 ansible ansible-2.x ansible-facts

团队, 我的任务使用 json 输出运行良好,但我只想查找 pod 名称和命名空间,而不是输出整个 pod json 输出。因此,我正在使用 debug 来提取 pod 名称,但不确定如何沿着命名空间提取所有 pod 名称..

有什么提示吗?我无法从这里理解:extracting a variable from json output then debug and register the outout with ansible

      - name: "Get a list of all pods from any namespace"
        k8s_facts:
          kind: Pod
          namespace: webhook
          kubeconfig: $WORKSPACE
          verify_ssl: no
        register: pod_list
      - debug:
          var: pod_list

      - name: list names and namespaces
        debug:
          msg: "{{ pod_list.resources[0].metadata.name }}"

输出:

TASK [3_validations_on_ssh : list names and namespaces] *******************************************************************************************************************************
ok: [target1] => {
    "msg": "k8s-webhook-auth-xxxx1"
}

查找 pod 的示例输出片段如下:类似地,它会继续查找 pod_lsit 中的其他 pod


TASK [3_validations_on_ssh : debug] *****************************************************
ok: [target1] => {
    "pod_list": {
        "changed": false,
        "failed": false,
        "resources": [
            {
                "apiVersion": "v1",
                "kind": "Pod",
                "metadata": {
                    "creationTimestamp": "2019-10-11T18:44:04Z",
                    "generateName": "k8s-webhook-auth-",
                    "labels": {
                        "app": "k8s-webhook-auth",
                        "controller-revision-hash": "666c6cb69d",
                        "pod-template-generation": "20",
                        "release": "k8s-webhook-auth"
                    },
                    "name": "k8s-webhook-auth-xxxx1",
                    "namespace": "webhook",
                    "ownerReferences": [
                        {
                            "apiVersion": "apps/v1",
                            "blockOwnerDeletion": true,
                            "controller": true,
                            "kind": "DaemonSet",
                            "name": "k8s-webhook-auth",
                            "uid": "1e9-8e9b-ac1f6b4ea082"
                        }
                    ],
                    "resourceVersion": "47592900",
                    "selfLink": "/api/v1/namespaces/webhook/pods/k8s-webhook-auth-5jx6w",
                    "uid": "1e9-8e9b-ac1f6b4ea082"
                },

预期输出:

k8s-webhook-auth-xxxx1 webhook
k8s-webhook-auth-xxxx2 webhook
k8s-webhook-auth-xxxx3 webhook

最佳答案

我认为您需要一个循环来获取您正在寻找的确切输出,但这意味着它不会出现在单个“消息”中,而是每个 Pod 中的一条消息,例如:

 - debug:
    msg: "{{ item.metadata.name }} {{ item.metadata.namespace }}"
  loop: "{{ pod_list.resources }}"

另一个选项是使用您需要的数据创建一个新对象。我将在下面给出两个例子,但有很多不同的选择。这些示例使用 debug 显示输出,但您可能希望使用 set_fact:

 - debug:
    var: pod_list | json_query('resources[].[metadata.name, metadata.namespace]')
 - debug:
    var: pod_list | json_query(query)
  vars:
    query: 'resources[].{name: metadata.name, namespace: metadata.namespace}'

编辑:更多示例

要限制循环中的输出,请查看 loop control文档。以下是使用 pod 名称的示例:

- debug:
    msg: "{{ item.metadata.name }} {{ item.metadata.namespace }}"
  loop: "{{ pod_list.resources }}"
  loop_control:
    label: "{{ item.metadata.name }}"

要将输出分配给新变量,请使用set_fact。请注意,如果与循环结合使用,您的结果将是多个对象的列表。以下是使用上述调试任务之一的示例:

- set_fact:
    pods: "{{ pod_list | json_query(query) }}"
  vars:
    query: 'resources[].{name: metadata.name, namespace: metadata.namespace}'

- debug:
    var: pods

关于ansible - 如何在多个字段上使用 with_items 从 ansible k8s 模块检索/解析结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58492929/

相关文章:

caching - 将事实收集到事实缓存的最快方法

Ansible:从字典中删除项目

ansible - 从文件夹列表中删除所有超过两天的文件夹

docker - 在ansible中使用SCP在服务器之间复制文件?

typescript - Nginx:上次重新加载配置是什么时候?

ansible - 无法使用项目列表(数组)读取自定义事实

linux - 指定直接主机的 Ansible ad-hoc 命令 - 没有匹配的主机

url - Ansible-从Github存储库下载最新版本的二进制文件

ansible - 从 ansible 变量中过滤匹配模式的子字符串并将匹配的子字符串分配给另一个变量

ansible - 如何使用 ansible_facts 仅列出正在运行的服务