0

Team, I have below output but i can't read its values.. I cannot use K8s module due to environmental limitations and need to use shell or command module only.

any hint how to tackle this? I would like to fetch values like gpu_class_list.stdout_lines.Labels.nodeType

 - name: "SHELL Fetch all CPU nodes from clusters using K8s beta.kubernetes.io/instance-type"

        shell: kubectl describe nodes -l beta.kubernetes.io/instance-type=2xlarge | grep -A 7 Labels
        register: gpu_class_list
        failed_when: not gpu_class_list
      - name: debug shell command register var for pull by label
        debug:
          #var: gpu_class_list.stdout_lines
          var: gpu_class_list.stdout_lines.Labels
Monday 16 December 2019  19:57:06 +0000 (0:00:01.064)       0:00:03.111 *******
ok: [localhost] => {
    "gpu_class_list.stdout_lines": [
        "Labels:             ace.ngc.nvidia.com/last-preflight-failure-time=2019-08-03T16.42.00Z",
        "                    beta.kubernetes.io/arch=amd64",
        "                    beta.kubernetes.io/instance-type=2xlarge",
        "                    beta.kubernetes.io/os=linux",
        "                    dummyType=dummy",
        "                    kubernetes.io/hostname=node2",
        "                    kubernetes.io/role=node",
        "                    nodeGroup=gpu",
        "--",
        "Labels:             beta.kubernetes.io/arch=amd64",
        "                    beta.kubernetes.io/instance-type=2xlarge",
        "                    beta.kubernetes.io/os=linux",
        "                    kubernetes.io/hostname=node1",
        "                    kubernetes.io/role=node",
        "                    nodeGroup=gpu",
        "                    nodeType=gpu",
        "Annotations:        csi.volume.kubernetes.io/nodeid: {\"com.nvidia.csi.vdisk\":\"node1\",\"csi-vdiskplugin\":\"node1\"}"
    ]
}
      - name: debug shell command register var for pull by label
        debug:
          var: gpu_class_list.stdout_lines.Labels

output

 “gpu_class_list.stdout_lines.Labels”: “VARIABLE IS NOT DEFINED!“
AhmFM
  • 1,552
  • 3
  • 23
  • 53

1 Answers1

0

You have a list of strings but are trying to reference them like a dictionary (key/value pairs) - this won't work. To avoid lower-level string processing, I suggest looking at the k8s_info module. For example:

- name: Fetch all CPU nodes from clusters using K8s beta.kubernetes.io/instance-type"
  k8s_info:
    kind: Node
    label_selectors:
      - beta.kubernetes.io/instance-type=2xlarge
  register: nodes

You will then have an object (under the variable nodes) that can be referenced in the way (or a similar way) that you are trying. You'll find the labels under the metadata section of each node

Matt P
  • 2,452
  • 1
  • 12
  • 15
  • Due to some environmental limitations, I cannot use K8s module and need to use pure shell or command module only. Hence challenge. – AhmFM Dec 16 '19 at 21:14
  • Not sure - your question looks well written to me. I would like to have a go at the string parsing approach but don't have time at the moment. Hope to revisit it soon. – Matt P Dec 17 '19 at 00:40