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
7,028 views

2 answers to this question.

0 votes

This should be pretty simple. Use the k8s_facts module for this purpose. 

- 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, 2020 by scatat

Related Questions In Ansible

0 votes
1 answer
0 votes
1 answer

How to list down all the hosts in Ansible?

Hi@akhtar, You can use the option --list-hosts. It will ...READ MORE

answered Jul 28, 2020 in Ansible by MD
• 95,460 points
48,221 views
0 votes
1 answer

How to see any module details in Ansible from the command line?

Hi@akhtar, Ansible has a very attractive command named ...READ MORE

answered Aug 11, 2020 in Ansible by MD
• 95,460 points
23,752 views
0 votes
1 answer

How to list down all the roles in Ansible?

Hi@akhtar, Ansible role is a part of ansible-galaxy. ...READ MORE

answered Aug 20, 2020 in Ansible by MD
• 95,460 points
8,020 views
0 votes
1 answer

How to run a playbook from a specific task in Ansible?

Hi@akhtar, The --start-at-task option allows you to start ...READ MORE

answered Sep 15, 2020 in Ansible by MD
• 95,460 points
11,164 views
+1 vote
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
15,476 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,034 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP