|
1 | 1 | --- |
2 | 2 |
|
3 | 3 | # Common playbook preparation. |
| 4 | +# |
| 5 | +# We expect: - |
| 6 | +# |
| 7 | +# - kubeconfig (defined, that might point to a KUBECONFIG file) |
| 8 | +# |
| 9 | +# We process: - |
| 10 | +# |
| 11 | +# - K8S_AUTH_HOST (optional) |
| 12 | +# - K8S_AUTH_API_KEY (optional) |
| 13 | +# - KUBECONFIG (optional) |
| 14 | +# |
| 15 | +# One method of Kubernetes authentication must be provided. |
| 16 | +# On exit the following variables have been set (although some may be blank/None): - |
| 17 | +# |
| 18 | +# - k8s_auth_api_key |
| 19 | +# - k8s_auth_host |
| 20 | +# - k8s_auth_kubeconfig |
| 21 | + |
| 22 | +- name: Assert inputs |
| 23 | + ansible.builtin.assert: |
| 24 | + that: |
| 25 | + - kubeconfig is defined |
4 | 26 |
|
5 | 27 | # Expose ansible version |
6 | 28 | - name: Display Ansible version |
|
20 | 42 |
|
21 | 43 | # Kubernetes credentials ------------------------------------------------------ |
22 | 44 |
|
23 | | -# We don't use the Kubernetes credentials directly, |
24 | | -# but we load them into variables here from their |
25 | | -# expected environment variables so that we can assert they've been set. |
| 45 | +# If a kubeconfig value is set we use that. |
| 46 | +# Otherwise if K8S_AUTH_HOST is defined we use that (AWX). |
| 47 | +# Otherwise if KUBECONFIG is defined we use that. |
26 | 48 |
|
27 | | -- name: Set initial authentication facts |
| 49 | +- name: Load K8S_AUTH_HOST and K8S_AUTH_API_KEY |
28 | 50 | ansible.builtin.set_fact: |
29 | 51 | k8s_auth_host: "{{ lookup('env', 'K8S_AUTH_HOST') }}" |
30 | 52 | k8s_auth_api_key: "{{ lookup('env', 'K8S_AUTH_API_KEY') }}" |
31 | 53 |
|
32 | | -# A kubernetes host and an API key must be set. |
33 | | -# Either environment variables will have been set by the user |
34 | | -# or AWX 'kubernetes' credentials will have injected them. |
35 | | -# Either way the variables 'k8s_auth_host' and |
36 | | -# 'k8s_auth_api_key' must have been set. |
37 | | -- name: Assert kubernetes authentication (no kubeconfig) |
| 54 | +- name: Use kubernetes authentication (kubeconfig) |
| 55 | + ansible.builtin.set_fact: |
| 56 | + k8s_auth_kubeconfig: "{{ kubeconfig }}" |
| 57 | + when: kubeconfig | string | length > 0 |
| 58 | + |
| 59 | +- name: Use kubernetes authentication (k8s_auth_host) |
38 | 60 | ansible.builtin.assert: |
39 | 61 | that: |
40 | | - - k8s_auth_host|string|length > 0 |
41 | | - - k8s_auth_api_key|string|length > 0 |
42 | | - when: jo_kubeconfig == 'SetMe' |
| 62 | + - k8s_auth_host | string | length > 0 |
| 63 | + - k8s_auth_api_key | string | length > 0 |
| 64 | + when: |
| 65 | + - kubeconfig | string | length == 0 |
| 66 | + - k8s_auth_host | string | length > 0 |
| 67 | + |
| 68 | +- name: Use kubernetes authentication (KUBECONFIG) |
| 69 | + ansible.builtin.set_fact: |
| 70 | + k8s_auth_kubeconfig: "{{ lookup('env', 'KUBECONFIG') }}" |
| 71 | + when: |
| 72 | + - kubeconfig | string | length == 0 |
| 73 | + - k8s_auth_host | string | length == 0 |
43 | 74 |
|
44 | | -- name: Assert kubeconfig defined (kubeconfig) |
| 75 | +- name: Kubernetes authentication must be set |
45 | 76 | ansible.builtin.assert: |
46 | 77 | that: |
47 | | - - jo_kubeconfig|length > 0 |
48 | | - when: jo_kubeconfig != 'SetMe' |
| 78 | + - k8s_auth_kubeconfig is defined or k8s_auth_host is defined |
| 79 | + msg: "You must provide a means to authenticate against Kubernetes" |
| 80 | + |
| 81 | +# We 'set' all the expected variables now (even to None) |
| 82 | +# to avoid the following playbooks having to apply 'default(none)'. |
| 83 | +# Basically we 'define' all three variables here, |
| 84 | +# whether they have a value or not, so any following playbook |
| 85 | +# won't encounter a 'variable not defined error'. |
| 86 | + |
| 87 | +- name: Set variables (with defaults) |
| 88 | + ansible.builtin.set_fact: |
| 89 | + k8s_auth_api_key: "{{ k8s_auth_api_key | default(None) }}" |
| 90 | + k8s_auth_host: "{{ k8s_auth_host | default(None) }}" |
| 91 | + k8s_auth_kubeconfig: "{{ k8s_auth_kubeconfig | default(None) }}" |
| 92 | + |
| 93 | +- name: Display Host |
| 94 | + ansible.builtin.debug: |
| 95 | + var: k8s_auth_host |
| 96 | + |
| 97 | +- name: Display KUBECONFIG |
| 98 | + ansible.builtin.debug: |
| 99 | + var: k8s_auth_kubeconfig |
0 commit comments