Skip to content

Commit 7bcf273

Browse files
committed
wip: unify caas/normal secrets
1 parent b99b1e9 commit 7bcf273

File tree

6 files changed

+117
-11
lines changed

6 files changed

+117
-11
lines changed

ansible/roles/passwords/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
This is pretty subtle.
2+
3+
- secrets always need to be idempotent
4+
- caas cannot run a pre-task to create files in inventory, so secrets
5+
need to be loaded as part of site
6+
- caas cannot write inventory, b/c that is in a container which is new each
7+
run
8+
9+
10+
TODO:
11+
- remove the adhoc generate-passwords from non-caas
12+
- remove and the other role/hook from caas and the secret overrides
13+
- work out how we'd migrate secrets for caas????
14+
- test it properly
15+
- doc this role properly
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
---
22

3-
slurm_appliance_secrets:
3+
# TODO: should we make these "'{{}}'" to ensure templating is OK??
4+
passwords_defaults:
45
vault_grafana_admin_password: "{{ secrets_openhpc_grafana_admin_password | default(vault_grafana_admin_password | default(lookup('password', '/dev/null'))) }}"
56
vault_elasticsearch_admin_password: "{{ secrets_openhpc_elasticsearch_admin_password | default(vault_elasticsearch_admin_password | default(lookup('password', '/dev/null'))) }}"
67
vault_mysql_root_password: "{{ secrets_openhpc_mysql_root_password | default(vault_mysql_root_password | default(lookup('password', '/dev/null'))) }}"
78
vault_mysql_slurm_password: "{{ secrets_openhpc_mysql_slurm_password | default(vault_mysql_slurm_password | default(lookup('password', '/dev/null'))) }}"
8-
vault_openhpc_mungekey: "{{ secrets_openhpc_mungekey | default(vault_openhpc_mungekey | default(secrets_openhpc_mungekey_default)) }}"
9+
vault_openhpc_mungekey: "{{ secrets_openhpc_mungekey | default(vault_openhpc_mungekey | default(passwords_openhpc_mungekey_default)) }}"
910
vault_freeipa_ds_password: "{{ vault_freeipa_ds_password | default(lookup('password', '/dev/null')) }}"
1011
vault_freeipa_admin_password: "{{ vault_freeipa_admin_password | default(lookup('password', '/dev/null')) }}"
1112
vault_k3s_node_password: "{{ vault_k3s_node_password | default(lookup('ansible.builtin.password', '/dev/null', length=64)) }}"
1213
vault_pulp_admin_password: "{{ vault_pulp_admin_password | default(lookup('password', '/dev/null', chars=['ascii_letters', 'digits'])) }}"
1314
vault_demo_user_password: "{{ vault_demo_user_password | default(lookup('password', '/dev/null')) }}"
1415
vault_alertmanager_admin_password: "{{ vault_alertmanager_admin_password | default(lookup('password', '/dev/null')) }}"
16+
# vault_newthing: "{{ vault_newthing | default(lookup('password', '/dev/null')) }}"
1517

16-
secrets_openhpc_mungekey_default:
18+
passwords_openhpc_mungekey_default:
1719
content: "{{ lookup('pipe', 'dd if=/dev/urandom bs=1 count=1024 2>/dev/null | base64') }}"
1820

19-
openhpc_passwords_output_path: "{{ lookup('env', 'APPLIANCES_ENVIRONMENT_ROOT') | default(undefined, true) | mandatory('You must define the APPLIANCES_ENVIRONMENT_ROOT environment variable') }}/inventory/group_vars/all/secrets.yml"
21+
passwords_output_path: "{{ lookup('env', 'APPLIANCES_ENVIRONMENT_ROOT') | default(undefined, true) | mandatory('You must define the APPLIANCES_ENVIRONMENT_ROOT environment variable') }}/inventory/group_vars/all/secrets.yml"
22+
23+
passwords_host: localhost
24+
passwords_owner: "{{ ansible_user }}"
25+
passwords_group: "{{ passwords_owner }}"
26+
Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,82 @@
11
---
22

3+
# STATUS:
4+
5+
# looks like it works for caas, but not for normal now
6+
7+
# can't use ansible.builtin.include_vars - that only looks on the deploy host
8+
# (even with delegate)
9+
10+
# For caas where passwords_output_path isn't actually in inventory we need
11+
# to load them first, so that templating is idempotent
12+
13+
- name: Slurp passwords if defined
14+
ansible.builtin.slurp:
15+
src: "{{ passwords_output_path }}"
16+
delegate_to: "{{ passwords_host }}"
17+
register: _passwords_slurp_first
18+
failed_when:
19+
- _passwords_slurp_first.failed
20+
- "'file not found' not in _passwords_slurp_first.msg"
21+
22+
- name: Set facts for passwords
23+
set_fact:
24+
"{{ item.key }}": "{{ item.value }}"
25+
when: "'content' in _passwords_slurp_first"
26+
loop: "{{ _passwords_slurp_first.content | b64decode | from_yaml | dict2items }}"
27+
no_log: "{{ no_log | default(true) }}"
28+
29+
# - name: Set facts for passwords
30+
# set_fact:
31+
# # nah can't template yaml keys so not sure we can do this!
32+
# when: not _passwords_slurp.failed
33+
# loop: "{{ _passwords_slurp.content | b64decode | from_yaml }}"
34+
35+
- name: Ensure secrets directory exists
36+
file:
37+
path: "{{ passwords_output_path | dirname }}"
38+
owner: "{{ passwords_owner }}"
39+
group: "{{ passwords_group }}"
40+
state: directory
41+
#mode: ug=rwX,o=rX # non-caas for caas we want u=rwx,go=
42+
delegate_to: "{{ passwords_host }}"
43+
become: "{{ passwords_owner != ansible_user }}" # not sure about this in the general case but seems ok here
44+
run_once: true
45+
346
- name: Template passwords
447
template:
548
src: passwords.yml
6-
dest: "{{ openhpc_passwords_output_path }}"
7-
delegate_to: localhost
49+
dest: "{{ passwords_output_path }}"
50+
owner: "{{ passwords_owner }}"
51+
group: "{{ passwords_group }}"
52+
become: "{{ passwords_owner != ansible_user }}"
53+
delegate_to: "{{ passwords_host }}"
854
run_once: true
55+
register: _passwords_template
56+
57+
# even if the files are in inventory, even meta: inventory_reload doesn't
58+
# get the new variables, so we need to set them as facts:
59+
- name: Slurp passwords if changed
60+
ansible.builtin.slurp:
61+
src: "{{ passwords_output_path }}"
62+
delegate_to: "{{ passwords_host }}"
63+
register: _passwords_slurp_second
64+
when: _passwords_template.changed
65+
66+
- name: Set facts for passwords
67+
set_fact:
68+
"{{ item.key }}": "{{ item.value }}"
69+
when: not _passwords_slurp_second.skipped | default(false)
70+
loop: "{{ _passwords_slurp_second.content | b64decode | from_yaml | dict2items }}"
71+
no_log: "{{ no_log | default(true) }}"
72+
73+
74+
# we do see passwords end up in the templated config for slurm-controlled rebuild!
75+
76+
77+
# oh man maybe this doesn't work b/c things are accessed through hostvars[*] ...
78+
79+
# also; does this work for caas?? Because the vars won't be in inventory to
80+
# start with, so then they won't exist, so they'll be re-templated despite
81+
# the fact the file exists. Maybe we need to load them first, if the file
82+
# exists??
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
- name: Assert secrets created
2-
assert:
3-
that: (hostvars[inventory_hostname].keys() | select('contains', 'vault_') | length) > 1 # 1 as may have vault_demo_user_password defined in dev
4-
fail_msg: "No inventory variables 'vault_*' found: Has ansible/adhoc/generate-passwords.yml been run?"
1+
# - name: Assert secrets created
2+
# assert:
3+
# that: (hostvars[inventory_hostname].keys() | select('contains', 'vault_') | length) > 1 # 1 as may have vault_demo_user_password defined in dev
4+
# fail_msg: "No inventory variables 'vault_*' found: Has ansible/adhoc/generate-passwords.yml been run?"
5+
# TODO: should maybe remove this, it isn't needed now??
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
---
22
# {{ ansible_managed }}
3-
{{ slurm_appliance_secrets | to_nice_yaml }}
3+
{{ passwords_defaults | to_nice_yaml }}

ansible/site.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
---
22

3+
- name: Template passwords
4+
hosts: cluster # TODO: maybe we need to run this
5+
gather_facts: false
6+
tasks:
7+
- name: Include password generation role
8+
include_role:
9+
name: passwords
10+
- meta: end_here
11+
312
- name: Run pre.yml hook
413
vars:
514
# hostvars not available here, so have to recalculate environment root:

0 commit comments

Comments
 (0)