OpenShift - Log into OpenShift using Ansible community.okd.openshift_auth

by
Jeremy Canfield |
Updated: October 21 2024
| OpenShift articles
The community.okd.openshift_auth module can be used, as the name suggest, to log into OpenShift.
---
- hosts: localhost
module_defaults:
group/community.okd.okd:
host: https://api.openshift.example.com:6443
validate_certs: False
tasks:
- block:
- name: login
community.okd.openshift_auth:
username: john.doe
password: itsasecret
register: openshift_auth_results
always:
- name: logout
community.okd.openshift_auth:
state: absent
api_key: "{{ openshift_auth_results.openshift_auth.api_key }}"
...
Taking this a step further, here is something a bit more practical, to return the list of all pods in all projects / namespaces.
---
- 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 }}"
...
Did you find this article helpful?
If so, consider buying me a coffee over at