Ansible task to list all pods from any k8s namespace

0 votes
I need an ansible task that lists all pods from any k8s namespace. Can somebody give me an example? Thank you
Jun 11, 2019 in Ansible by Tanvi
844 views

2 answers to this question.

0 votes

This should be pretty simple. Use the k8s_facts module for this purpose. Have a look at the docs to get more details about this module.

- name: list of all pods from namespace
      k8s_facts:
        api_key: "{{ k8s_auth_results.k8s_auth.api_key }}"
        kind: Pod
      register: pod-list
answered Jun 11, 2019 by Gred
+1 vote
Here is a more complete answer for anyone Googling - this deletes all pods in the metallb-system namespace. We need to use json_query ( jq ) to extract the pod names

- name: Get the pods in the metallb-system namespace
  k8s_info:
    kind: Pod
    namespace: metallb-system
  register: pod_list

- name: Print pod names
  debug:
    msg: "pod_list: {{ pod_list | json_query('resources[*].metadata.name')  }} "

- name:  Delete pods
  k8s:
    state: absent
    api_version: v1
    kind: Pod
    namespace: metallb-system
    name: "{{ item }}"
  loop: "{{ pod_list | json_query('resources[*].metadata.name') }}"
answered Feb 6 by scatat

Related Questions In Ansible

0 votes
1 answer
0 votes
1 answer

Ansible: List all installed Apps in RedHat

Try this to get a list of ...READ MORE

answered Jul 13, 2018 in Ansible by ajs3033
• 7,280 points

edited Jul 13, 2018 by ajs3033 276 views
0 votes
1 answer
0 votes
1 answer

Ansible playbbok to copy files from one node to another remote node

You can create a playbook for copying ...READ MORE

answered Jan 16, 2019 in Ansible by Danica
1,129 views
0 votes
1 answer

Ansible playbook for moving files from one node to another remote node

The best way to do this is: copy ...READ MORE

answered Jan 16, 2019 in Ansible by Nigya

edited Jan 18, 2019 by Vardhan 3,305 views
0 votes
1 answer
0 votes
3 answers

Error while joining cluster with node

Hi Kalgi after following above steps it ...READ MORE

answered Jan 17, 2019 in Others by anonymous
7,064 views
+4 votes
1 answer

Installing Web UI (Dashboard):kubernetes-dashboard on main Ubuntu 16.04.6 LTS (Xenial Xerus) server

Follow these steps: $ kubeadm reset $ kubeadm init ...READ MORE

answered Apr 11, 2019 in Kubernetes by Kalgi
• 51,890 points

reshown Apr 12, 2019 by Kalgi 2,086 views