1

Team, My task is running fine with json output but I just want to look for pod name and namespace instead of outputting the whole pod json output. so, i am using debug to pull the pod name but not sure how to pull all pods names along side namespace..

any hint? I am not able to understand from here: 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 }}"

output:

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

sample output snippet that looks for a pod is below: similarly it continues for other pods in pod_lsit


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"
                },

expected output:

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

AhmFM
  • 1,552
  • 3
  • 23
  • 53

1 Answers1

3

I think you would need a loop to get the exact output you are looking for, but that means it won't be in a single "message", but rather one message per pod, for example:

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

The other option is to create a new object with the data you need. I'll give 2 examples below but there's a lot of different options. These examples use debug to show the output, but you would probably want to use 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}'

Edit: more examples

To limit output in loops, check out the loop control documentation. Here's an example using the pod name:

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

To assign output to a new variable use set_fact. Be wary that if combined with a loop, your result will be a list of multiple objects. Here's an example using one of the debug tasks above:

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

- debug:
    var: pods
Matt P
  • 2,452
  • 1
  • 12
  • 15
  • you made my day by God. Thanks a lot. works! would you mind also listing set_fact solution. – AhmFM Oct 21 '19 at 22:31
  • in loop example: feedback is am gettting the node names but along with that its still outputting all the remaining huge output. not sure how to ignore that and just list name. the debug example works – AhmFM Oct 21 '19 at 22:47
  • No worries - I've added some examples for `set_fact`, as well as limiting the amount of output in loops – Matt P Oct 22 '19 at 01:58