|
5 | 5 | - name: Set variables |
6 | 6 | ansible.builtin.set_fact: |
7 | 7 | remote_path: ~/docker-compose.yml |
8 | | - local_path: docker-compose.yml |
| 8 | + template_file: docker-compose.yml |
9 | 9 | project_path: . |
10 | 10 |
|
11 | 11 | - name: Include update docker-compose |
12 | 12 | ansible.builtin.include_tasks: "{{ tasks_dir }}/docker/update-compose.yaml" |
13 | 13 | vars: |
14 | 14 | update_compose_remote_path: "{{ remote_path }}" |
15 | | - update_compose_local_path: "{{ local_path }}" |
| 15 | + template_file_name: "{{ template_file }}" |
16 | 16 | update_compose_project_path: "{{ project_path }}" |
17 | 17 |
|
18 | 18 | - name: Copy init-vault.sh |
19 | | - ansible.builtin.copy: |
| 19 | + ansible.builtin.template: |
20 | 20 | src: init-vault.sh |
21 | 21 | dest: ~/init-vault.sh |
22 | 22 | mode: '0755' |
|
26 | 26 | path: ~/config/vault-config.json |
27 | 27 | register: remote_config |
28 | 28 |
|
29 | | -- name: Read local vault-config.json |
30 | | - ansible.builtin.stat: |
31 | | - path: vault-config.json |
32 | | - register: local_config |
33 | | - delegate_to: localhost |
| 29 | +- name: Read remote file content |
| 30 | + ansible.builtin.slurp: |
| 31 | + src: ~/config/vault-config.json |
| 32 | + register: remote_config_content |
| 33 | + when: remote_config.stat.exists |
| 34 | + |
| 35 | +- name: Render template content |
| 36 | + ansible.builtin.set_fact: |
| 37 | + template_config_raw: "{{ lookup('template', 'vault-config.json') }}" |
| 38 | + |
| 39 | +- name: Normalize template content to string |
| 40 | + ansible.builtin.set_fact: |
| 41 | + template_config_rendered: "{{ (template_config_raw | string).strip() }}" |
| 42 | + |
| 43 | +- name: Set default remote content when file doesn't exist |
| 44 | + ansible.builtin.set_fact: |
| 45 | + remote_content_decoded: "" |
| 46 | + when: not remote_config.stat.exists |
| 47 | + |
| 48 | +- name: Decode remote content when file exists |
| 49 | + ansible.builtin.set_fact: |
| 50 | + remote_content_decoded: "{{ (remote_config_content.content | b64decode | string).strip() }}" |
| 51 | + when: remote_config.stat.exists |
34 | 52 |
|
35 | 53 | - name: Check file content and set variables |
36 | 54 | ansible.builtin.set_fact: |
37 | | - config_different: "{{ not remote_config.stat.exists or remote_config.stat.checksum != local_config.stat.checksum }}" |
| 55 | + config_different: "{{ not remote_config.stat.exists or (remote_content_decoded != template_config_rendered) }}" |
38 | 56 |
|
39 | 57 | - name: Are they different? |
40 | 58 | ansible.builtin.debug: |
|
50 | 68 | mode: '0755' |
51 | 69 |
|
52 | 70 | - name: Copy vault-config.json |
53 | | - ansible.builtin.copy: |
| 71 | + ansible.builtin.template: |
54 | 72 | src: vault-config.json |
55 | 73 | dest: ~/config/vault-config.json |
56 | 74 | mode: '0644' |
|
76 | 94 | until: vault_health.status in [200, 429, 473, 501] |
77 | 95 | retries: 10 |
78 | 96 | delay: 3 |
79 | | - |
80 | | -- name: Check if Vault is initialized |
81 | | - ansible.builtin.shell: docker exec vault vault status -format=json |
82 | | - register: vault_status |
83 | | - ignore_errors: true |
84 | | - changed_when: false |
85 | | - |
86 | | -- name: Display Vault initialization instructions |
87 | | - ansible.builtin.debug: |
88 | | - msg: | |
89 | | - Vault is not initialized. Please run: |
90 | | - |
91 | | - docker exec -it vault sh |
92 | | - vault operator init |
93 | | - |
94 | | - Save the unseal keys and root token securely! |
95 | | - Then unseal Vault with: |
96 | | - |
97 | | - vault operator unseal <key1> |
98 | | - vault operator unseal <key2> |
99 | | - vault operator unseal <key3> |
100 | | - |
101 | | - After unsealing, configure OIDC by running init-vault.sh with: |
102 | | - |
103 | | - export VAULT_ADDR=http://127.0.0.1:8200 |
104 | | - export VAULT_TOKEN=<your-root-token> |
105 | | - export OIDC_DISCOVERY_URL="https://auth.ildoc.it/application/o/vault/" |
106 | | - export OIDC_CLIENT_ID="<client-id-from-authentik>" |
107 | | - export OIDC_CLIENT_SECRET="<client-secret-from-authentik>" |
108 | | - |
109 | | - ./init-vault.sh |
110 | | - when: vault_status.rc != 0 or (vault_status.stdout | from_json).initialized == false |
0 commit comments