Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

k8s_auth: use group module defaults in example #52712

Merged
merged 1 commit into from
Mar 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 28 additions & 33 deletions lib/ansible/modules/clustering/k8s/k8s_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,42 +74,37 @@
'''

EXAMPLES = '''
- block:
# It's good practice to store login credentials in a secure vault and not
# directly in playbooks.
- include_vars: k8s_passwords.yml

- name: Log in (obtain access token)
k8s_auth:
- hosts: localhost
module_defaults:
group/k8s:
host: https://k8s.example.com/
ssl_ca_cert: ca.pem
username: admin
password: "{{ k8s_admin_password }}"
register: k8s_auth_results

- name: Preserve auth info as both a fact and a yaml anchor for easy access later
# Both the fact and the anchor are called 'k8s_auth_params'
set_fact:
k8s_auth_params: &k8s_auth_params
host: "{{ k8s_auth_results.k8s_auth.host }}"
ssl_ca_cert: "{{ k8s_auth_results.k8s_auth.ssl_ca_cert }}"
verify_ssl: "{{ k8s_auth_results.k8s_auth.verify_ssl }}"
tasks:
- block:
# It's good practice to store login credentials in a secure vault and not
# directly in playbooks.
- include_vars: k8s_passwords.yml

- name: Log in (obtain access token)
k8s_auth:
username: admin
password: "{{ k8s_admin_password }}"
register: k8s_auth_results

# Previous task provides the token/api_key, while all other parameters
# are taken from module_defaults
- name: Get a list of all pods from any namespace
k8s_facts:
api_key: "{{ k8s_auth_results.k8s_auth.api_key }}"
kind: Pod
register: pod_list

always:
- name: If login succeeded, try to log out (revoke access token)
when: k8s_auth_results.k8s_auth.api_key is defined
k8s_auth:
state: absent
api_key: "{{ k8s_auth_results.k8s_auth.api_key }}"

# Previous task generated I(k8s_auth) fact, which you can then use
# in k8s modules like this:
- name: Get a list of all pods from any namespace
k8s_facts:
<<: *k8s_auth_params
kind: Pod
register: pod_list

always:
- name: If login succeeded, try to log out (revoke access token)
when: k8s_auth_params is defined
k8s_auth:
state: absent
<<: *k8s_auth_params
'''

# Returned value names need to match k8s modules parameter names, to make it
Expand Down