1

I'm testing the ansible k8s_info module and having a challenge parsing the result in a register variable.

The playbook gets pods from all namespaces. My objective is to print only the spec.containers information from each pod returned in the registered variable.

---
- name: kubenetes demo
  hosts: control
  gather_facts: true

  tasks:

    - name: get a list of all pods
      k8s_info:
        kind: Pod
      register: pod_list

    - debug:
        msg:
        - "Containers in use {{ item.spec.containers }}"
      loop: "{{ pod_list.resources }}"

The result I get back is (truncated for brevity):

ok: [10.0.0.110] => (item={'metadata': {'name': 'coredns-66bff467f8-22f6v', 'generateName': 'coredns-66bff467f8-', 'namespace': 'kube-system', 'selfLink': '/api/v1/namespaces/kube-system/pods/coredns-66bff467f8-22f6v'


"msg": [
        "Containers in use [{'name': 'coredns', 'image': 'k8s.gcr.io/coredns:1.6.7', 'args': ['-conf', '/etc/coredns/Corefile'], 'ports': [{'name': 'dns', 'containerPort': 53, 'protocol': 'UDP'}, {'name': 'dns-tcp', 'containerPort': 53, 'protocol': 'TCP'}, {'name': 'metrics', 'containerPort': 9153, 'protocol': 'TCP'}], 'resources': {'limits': {'memory': '170Mi'}, 'requests': {'cpu': '100m', 'memory': '70Mi'}}, 'volumeMounts': [{'name': 'config-volume', 'readOnly': True, 'mountPath': '/etc/coredns'}, {'name': 'coredns-token-hdddf', 'readOnly': True, 'mountPath': '/var/run/secrets/kubernetes.io/serviceaccount'}], 'livenessProbe': {'httpGet': {'path': '/health', 'port': 8080, 'scheme': 'HTTP'}, 'initialDelaySeconds': 60, 'timeoutSeconds': 5, 'periodSeconds': 10, 'successThreshold': 1, 'failureThreshold': 5}, 'readinessProbe': {'httpGet': {'path': '/ready', 'port': 8181, 'scheme': 'HTTP'}, 'timeoutSeconds': 1, 'periodSeconds': 10, 'successThreshold': 1, 'failureThreshold': 3}, 'terminationMessagePath': '/dev/termination-log', 'terminationMessagePolicy': 'File', 'imagePullPolicy': 'IfNotPresent', 'securityContext': {'capabilities': {'add': ['NET_BIND_SERVICE'], 'drop': ['all']}, 'readOnlyRootFilesystem': True, 'allowPrivilegeEscalation': False}}]"

I only want the msg to be displayed on the terminal. How should I structure this playbook to display only the msg? Do I need a jinja2 template?

dcrearer
  • 133
  • 5

1 Answers1

1

That isn't possible. Ansible is a CLI tool that you use to run some tasks and do some jobs. It isn't a tool for creating nice lookup terminal/shell output. So, the ok, failed, changed etc is an information that is hardcoded written to stdout.

If you only need the output for other reasons than you can call the playbook and write the output of the var to a template and stop the playbook. The next script than only outputs the data of the template. Easy example:

- name: "Write response to template"
  template:
    src: my_template.txt.j2
    dest: result.txt

And the template itself

{% for item in pod_list.resources %}
Containers in use {{ item.spec.containers }}
{% endfor %}

The response is in response.txt and can be viewed via a script like

#!/bin/sh
ansible-playbook my-play.yml > /dev/null 2>&1
cat response.txt
TRW
  • 438
  • 3
  • 14