|
| 1 | +--- |
| 2 | +# Sets values_secrets_bootstrap_data when a bootstrap values-secret file exists; otherwise no-op. |
| 3 | +# Expects: pattern_name, and _primary_values_secrets_data_snapshot when restoring after read. |
| 4 | +- name: Clear bootstrap secrets facts from any prior play |
| 5 | + ansible.builtin.set_fact: |
| 6 | + values_secrets_bootstrap_data: '' |
| 7 | + vp_bootstrap_secrets_present: false |
| 8 | + found_bootstrap_file: '' |
| 9 | + vp_bootstrap_loaded_via_values_secret_env: false |
| 10 | + |
| 11 | +- name: Read VALUES_SECRET for optional bootstrap discovery |
| 12 | + ansible.builtin.set_fact: |
| 13 | + bootstrap_custom_env_values_secret: "{{ lookup('ansible.builtin.env', 'VALUES_SECRET') }}" |
| 14 | + |
| 15 | +- name: Decide if VALUES_SECRET names a bootstrap file |
| 16 | + ansible.builtin.set_fact: |
| 17 | + _bootstrap_env_is_bootstrap_named: >- |
| 18 | + {{ |
| 19 | + (bootstrap_custom_env_values_secret | default('') | string | length > 0) |
| 20 | + and (bootstrap_custom_env_values_secret | regex_search('-bootstrap\.ya?ml$') is not none) |
| 21 | + }} |
| 22 | +
|
| 23 | +- name: Check if VALUES_SECRET points to an existing file (bootstrap) |
| 24 | + ansible.builtin.stat: |
| 25 | + path: "{{ bootstrap_custom_env_values_secret }}" |
| 26 | + register: bootstrap_custom_file_values_secret |
| 27 | + when: |
| 28 | + - bootstrap_custom_env_values_secret | default('') | length > 0 |
| 29 | + - _bootstrap_env_is_bootstrap_named | default(false) | bool |
| 30 | + |
| 31 | +- name: Use VALUES_SECRET as bootstrap secrets file |
| 32 | + ansible.builtin.set_fact: |
| 33 | + found_bootstrap_file: "{{ bootstrap_custom_file_values_secret.stat.path }}" |
| 34 | + vp_bootstrap_loaded_via_values_secret_env: true |
| 35 | + when: |
| 36 | + - bootstrap_custom_env_values_secret | default('') | length > 0 |
| 37 | + - _bootstrap_env_is_bootstrap_named | default(false) | bool |
| 38 | + - bootstrap_custom_file_values_secret.stat is defined |
| 39 | + - bootstrap_custom_file_values_secret.stat.exists |
| 40 | + |
| 41 | +- name: Build bootstrap values-secret candidate paths |
| 42 | + ansible.builtin.set_fact: |
| 43 | + _vp_bootstrap_secret_candidates: |
| 44 | + - "~/.config/hybrid-cloud-patterns/values-secret-{{ pattern_name }}-bootstrap.yaml" |
| 45 | + - "~/.config/validated-patterns/values-secret-{{ pattern_name }}-bootstrap.yaml" |
| 46 | + - "~/values-secret-{{ pattern_name }}-bootstrap.yaml" |
| 47 | + - "~/values-secret-bootstrap.yaml" |
| 48 | + when: (found_bootstrap_file | default('') | string | length) == 0 |
| 49 | + |
| 50 | +- name: Stat bootstrap candidate paths |
| 51 | + ansible.builtin.stat: |
| 52 | + path: "{{ item }}" |
| 53 | + loop: "{{ _vp_bootstrap_secret_candidates }}" |
| 54 | + register: _vp_bootstrap_stat_results |
| 55 | + when: (found_bootstrap_file | default('') | string | length) == 0 |
| 56 | + |
| 57 | +- name: Pick first existing bootstrap secrets file from candidates |
| 58 | + ansible.builtin.set_fact: |
| 59 | + found_bootstrap_file: "{{ (_vp_bootstrap_stat_results.results | default([]) | selectattr('stat.exists') | map(attribute='item') | list | first) | default('') }}" |
| 60 | + when: |
| 61 | + - (found_bootstrap_file | default('') | string | length) == 0 |
| 62 | + - _vp_bootstrap_stat_results.results is defined |
| 63 | + |
| 64 | +- name: Read bootstrap secrets when a bootstrap file was found |
| 65 | + when: (found_bootstrap_file | default('') | string | length) > 0 |
| 66 | + block: |
| 67 | + - name: Load bootstrap secrets from file |
| 68 | + ansible.builtin.include_tasks: read_secret_from_path.yml |
| 69 | + vars: |
| 70 | + found_file: "{{ found_bootstrap_file }}" |
| 71 | + |
| 72 | + - name: Publish bootstrap secrets data for display |
| 73 | + ansible.builtin.set_fact: |
| 74 | + values_secrets_bootstrap_data: "{{ values_secrets_data }}" |
| 75 | + vp_bootstrap_secrets_present: true |
| 76 | + |
| 77 | + - name: Restore primary values_secrets_data after bootstrap read |
| 78 | + ansible.builtin.set_fact: |
| 79 | + values_secrets_data: "{{ _primary_values_secrets_data_snapshot }}" |
| 80 | + when: _primary_values_secrets_data_snapshot is defined |
0 commit comments