OpenShift - List Namespaces using Ansible

by
Jeremy Canfield |
Updated: June 10 2025
| 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 namespace using Ansible kubernetes.core.k8s_info.
---
- hosts: localhost
module_defaults:
group/community.okd.okd:
host: https://api.op.example.com:6443
validate_certs: False
tasks:
- block:
- name: login
community.okd.openshift_auth:
username: john.doe
password: itsasecret
register: openshift_auth_results
- name: get all namespaces
kubernetes.core.k8s_info:
api_key: "{{ openshift_auth_results.openshift_auth.api_key }}"
kind: Namespace
register: all_namespaces
- ansible.builtin.debug:
var: all_namespaces
always:
- name: logout
community.okd.openshift_auth:
state: absent
api_key: "{{ openshift_auth_results.openshift_auth.api_key }}"
...
name can be used to return the details of a specific namespace.
- name: get all namespaces
kubernetes.core.k8s_info:
api_key: "{{ openshift_auth_results.openshift_auth.api_key }}"
kind: Namespace
name: my-namespace
register: my_namespace
Did you find this article helpful?
If so, consider buying me a coffee over at