summaryrefslogtreecommitdiffstats
path: root/tools/docker-compose/ansible/roles/vault/tasks/initialize.yml
blob: 8c7230c6d1467a8517cf95a0b399eccc8827af43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
---
- name: Set vault_addr
  include_tasks: set_vault_addr.yml

- block:
    - name: Start the vault
      community.docker.docker_compose_v2:
        state: present
        services: vault
        project_src: "{{ sources_dest }}"
      register: vault_start

    - name: Run the initialization
      community.docker.docker_container_exec:
        command: vault operator init
        container: tools_vault_1
        env:
          VAULT_ADDR: "{{ vault_addr }}"
          VAULT_SKIP_VERIFY: "true"
      register: vault_initialization
      failed_when:
        - vault_initialization.rc != 0
        - vault_initialization.stderr.find("Vault is already initialized") == -1
      changed_when:
        - vault_initialization.rc == 0
      retries: 5
      delay: 5

    - name: Write out initialization file
      copy:
        # lines 1-4 are the keys, 6 is the root token
        content: |
          {{ vault_initialization.stdout_lines[0] | regex_replace('Unseal Key ', 'Unseal_Key_') }}
          {{ vault_initialization.stdout_lines[1] | regex_replace('Unseal Key ', 'Unseal_Key_') }}
          {{ vault_initialization.stdout_lines[2] | regex_replace('Unseal Key ', 'Unseal_Key_') }}
          {{ vault_initialization.stdout_lines[3] | regex_replace('Unseal Key ', 'Unseal_Key_') }}
          {{ vault_initialization.stdout_lines[4] | regex_replace('Unseal Key ', 'Unseal_Key_') }}
          {{ vault_initialization.stdout_lines[6] | regex_replace('Initial Root Token', 'Initial_Root_Token') }}
        dest: "{{ vault_file }}"
      when: (vault_initialization.stdout_lines | length) > 0

    - name: Unlock the vault
      include_role:
        name: vault
        tasks_from: unseal.yml

    - name: Configure the vault with cert auth
      block:
        - name: Create a cert auth mount
          flowerysong.hvault.write:
            path: "sys/auth/cert"
            vault_addr: "{{ vault_addr_from_host }}"
            validate_certs: false
            token: "{{ Initial_Root_Token }}"
            data:
              type: "cert"
          register: vault_auth_cert
          failed_when:
            - vault_auth_cert.result.errors | default([]) | length > 0
            - "'path is already in use at cert/' not in vault_auth_cert.result.errors | default([])"
          changed_when:
            - vault_auth_cert.result.errors | default([]) | length == 0

        - name: Configure client certificate
          flowerysong.hvault.write:
            path: "auth/cert/certs/awx-client"
            vault_addr: "{{ vault_addr_from_host }}"
            validate_certs: false
            token: "{{ Initial_Root_Token }}"
            data:
              name: awx-client
              certificate: "{{ lookup('ansible.builtin.file', '{{ vault_client_cert }}') }}"
              policies:
                - root
      when: vault_tls | bool

    - name: Create an engine
      flowerysong.hvault.engine:
        path: "my_engine"
        type: "kv"
        vault_addr: "{{ vault_addr_from_host }}"
        validate_certs: false
        token: "{{ Initial_Root_Token }}"

    - name: Create a demo secret
      flowerysong.hvault.kv:
        mount_point: "my_engine/my_root"
        key: "my_folder"
        value:
          my_key: "this_is_the_secret_value"
        vault_addr: "{{ vault_addr_from_host }}"
        validate_certs: false
        token: "{{ Initial_Root_Token }}"

    - name: Configure the vault ldap auth
      block:
        - name: Create ldap auth mount
          flowerysong.hvault.write:
            path: "sys/auth/ldap"
            vault_addr: "{{ vault_addr_from_host }}"
            validate_certs: false
            token: "{{ Initial_Root_Token }}"
            data:
              type: "ldap"
          register: vault_auth_ldap
          changed_when: vault_auth_ldap.result.errors | default([]) | length == 0
          failed_when:
            - vault_auth_ldap.result.errors | default([]) | length > 0
            - "'path is already in use at ldap/' not in vault_auth_ldap.result.errors | default([])"

        - name: Create ldap engine
          flowerysong.hvault.engine:
            path: "ldap_engine"
            type: "kv"
            vault_addr: "{{ vault_addr_from_host }}"
            validate_certs: false
            token: "{{ Initial_Root_Token }}"

        - name: Create a ldap secret
          flowerysong.hvault.kv:
            mount_point: "ldap_engine/ldaps_root"
            key: "ldap_secret"
            value:
              my_key: "this_is_the_ldap_secret_value"
            vault_addr: "{{ vault_addr_from_host }}"
            validate_certs: false
            token: "{{ Initial_Root_Token }}"

        - name: Configure ldap auth
          flowerysong.hvault.ldap_config:
            vault_addr: "{{ vault_addr_from_host }}"
            validate_certs: false
            token: "{{ Initial_Root_Token }}"
            url: "ldap://ldap:1389"
            binddn: "cn=awx_ldap_vault,ou=users,dc=example,dc=org"
            bindpass: "vault123"
            userdn: "ou=users,dc=example,dc=org"
            deny_null_bind: "false"
            discoverdn: "true"

        - name: Create ldap access policy
          flowerysong.hvault.policy:
            vault_addr: "{{ vault_addr_from_host }}"
            validate_certs: false
            token: "{{ Initial_Root_Token }}"
            name: "ldap_engine"
            policy:
              ldap_engine/*: [create, read, update, delete, list]
              sys/mounts:/*: [create, read, update, delete, list]
              sys/mounts: [read]

        - name: Add awx_ldap_vault user to auth_method
          flowerysong.hvault.ldap_user:
            vault_addr: "{{ vault_addr_from_host }}"
            validate_certs: false
            token: "{{ Initial_Root_Token }}"
            state: present
            name: "{{ vault_ldap_username }}"
            policies:
              - "ldap_engine"
      when: enable_ldap | bool

    - name: Create userpass engine
      flowerysong.hvault.engine:
        path: "userpass_engine"
        type: "kv"
        vault_addr: "{{ vault_addr_from_host }}"
        validate_certs: false
        token: "{{ Initial_Root_Token }}"

    - name: Create a userpass secret
      flowerysong.hvault.kv:
        mount_point: "userpass_engine/userpass_root"
        key: "userpass_secret"
        value:
          my_key: "this_is_the_userpass_secret_value"
        vault_addr: "{{ vault_addr_from_host }}"
        validate_certs: false
        token: "{{ Initial_Root_Token }}"

    - name: Create userpass access policy
      flowerysong.hvault.policy:
        vault_addr: "{{ vault_addr_from_host }}"
        validate_certs: false
        token: "{{ Initial_Root_Token }}"
        name: "userpass_engine"
        policy:
          userpass_engine/*: [create, read, update, delete, list]
          sys/mounts:/*: [create, read, update, delete, list]
          sys/mounts: [read]

    - name: Create userpass auth mount
      flowerysong.hvault.write:
        path: "sys/auth/userpass"
        vault_addr: "{{ vault_addr_from_host }}"
        validate_certs: false
        token: "{{ Initial_Root_Token }}"
        data:
          type: "userpass"
      register: vault_auth_userpass
      changed_when: vault_auth_userpass.result.errors | default([]) | length == 0
      failed_when:
        - vault_auth_userpass.result.errors | default([]) | length > 0
        - "'path is already in use at userpass/' not in vault_auth_userpass.result.errors | default([])"

    - name: Add awx_userpass_admin user to auth_method
      flowerysong.hvault.write:
        vault_addr: "{{ vault_addr_from_host }}"
        validate_certs: false
        token: "{{ Initial_Root_Token }}"
        path: "auth/userpass/users/{{ vault_userpass_username }}"
        data:
          password: "{{ vault_userpass_password }}"
          policies:
            - "userpass_engine"

  always:
    - name: Stop the vault
      community.docker.docker_compose_v2:
        state: absent
        project_src: "{{ sources_dest }}"
      when: vault_start is defined and vault_start.changed