Bootstrap FreeKB - OpenShift - List pods using Ansible
OpenShift - List pods using Ansible

Updated:   |  OpenShift articles

This assumes you are already familiar with logging in and logging out of OpenShift using Ansible. If not, check out my article FreeKB - OpenShift - Log into OpenShift using Ansible community.okd.openshift_auth.

Here is an example of how to return a list of all pods in all projects / namespace using Ansible kubernetes.core.k8s_info.

---
- hosts: localhost
  module_defaults:
    group/community.okd.okd:
      host: https://api.lab001.op.thrivent.com:6443
      validate_certs: False
  tasks:
  - block:

    - name: login
      community.okd.openshift_auth:
        username: john.doe
        password: itsasecret
      register: openshift_auth_results

    - name: store all pods in all projects/namespaces in a dictionary named 'pods'
      kubernetes.core.k8s_info:
        api_key: "{{ openshift_auth_results.openshift_auth.api_key }}"
        kind: Pod
      register: pods

    - ansible.builtin.debug:
        var: pods

    always:

    - name: logout
      community.okd.openshift_auth:
        state: absent
        api_key: "{{ openshift_auth_results.openshift_auth.api_key }}"
...

 

namespace can be used to return the list of pods in a specific project / namespace.

- name: store pods in my_project in the dictionary named 'pods'
  kubernetes.core.k8s_info:
    api_key: "{{ openshift_auth_results.openshift_auth.api_key }}"
    kind: Pod
    namespace: my_project
  register: pods
  
- ansible.builtin.debug:
    msg: "{{ item.metadata.name }}"
  with_items: "{{ pods.resources }}"
  loop_control:
    label: "{{ item.metadata.name }}"  

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 6e13d1 in the box below so that we can be sure you are a human.