-
Notifications
You must be signed in to change notification settings - Fork 26
/
06_nomad-vault-integration.yml
177 lines (155 loc) · 6.86 KB
/
06_nomad-vault-integration.yml
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
---
- name: "AT Computing - HashiCorp Nomad - Vault integration"
hosts: all
become: true
vars_files:
- vars/general/main.yml
- vars/hashicorp/general.yml
- vars/hashicorp/nomad.yml
- vars/hashicorp/ssl.yml
- vars/hashicorp/vault.yml
tasks:
- name: "HashiCorp - Nomad / Vault : Find all vault-nomad-server- files."
ansible.builtin.find:
paths: "files/hashicorp/vault/"
patterns: "vault-nomad-*"
file_type: "file"
delegate_to: localhost
run_once: true
register: vault_files
- name: "HashiCorp - Nomad / Vault : Ensure vault-nomad-server- files are present."
ansible.builtin.copy:
src: "{{ item.path }}"
dest: "{{ nomad_data_directory }}/{{ item.path | basename }}"
mode: 0644
with_items: "{{ vault_files.files }}"
loop_control:
label: "{{ item.path | basename }}"
when: ansible_hostname in groups['servers']
delegate_to: "{{ groups['servers'] | first }}"
run_once: true
- name: "HashiCorp - Nomad / Vault : Enable integration."
block:
- name: "HashiCorp - Nomad / Vault : Create fact of admin password."
ansible.builtin.set_fact:
admin_password: "{{ lookup('file', vault_admin_local_path) }}"
delegate_to: localhost
run_once: true
- name: "HashiCorp - Nomad / Vault : Retrieve client token."
ansible.builtin.uri:
url: "{{ vault_address }}/v1/auth/userpass/login/{{ vault_admin_username }}"
method: POST
body_format: json
body: '{ "password": "{{ admin_password }}" }'
status_code:
- 200
- 204
validate_certs: false
register: vault_admin_login_response
- name: "HashiCorp - Nomad / Vault : Set fact of client token."
ansible.builtin.set_fact:
admin_token: "{{ vault_admin_login_response.json.auth.client_token }}"
- name: "HashiCorp - Nomad / Vault : Ensure ssl-certificates-policy is present."
ansible.builtin.command: "{{ vault_binary }} policy write ssl-certificates-policy vault-nomad-ssl-certificates-policy.hcl"
args:
chdir: "{{ nomad_data_directory }}"
environment:
VAULT_TOKEN: "{{ admin_token }}"
VAULT_ADDR: "{{ vault_address }}"
VAULT_CACERT: "{{ vault_ssl_ca_dest }}"
VAULT_SKIP_VERIFY: "true"
changed_when: false
- name: "HashiCorp - Nomad / Vault : Ensure nomad-server-policy is present."
ansible.builtin.command: "{{ vault_binary }} policy write nomad-server vault-nomad-server-policy.hcl"
args:
chdir: "{{ nomad_data_directory }}"
environment:
VAULT_TOKEN: "{{ admin_token }}"
VAULT_ADDR: "{{ vault_address }}"
VAULT_CACERT: "{{ vault_ssl_ca_dest }}"
VAULT_SKIP_VERIFY: "true"
changed_when: false
- name: "HashiCorp - Nomad / Vault : Ensure nomad-cluster-role is present."
ansible.builtin.command: "{{ vault_binary }} write /auth/token/roles/nomad-cluster @vault-nomad-cluster-role.json"
args:
chdir: "{{ nomad_data_directory }}"
environment:
VAULT_TOKEN: "{{ admin_token }}"
VAULT_ADDR: "{{ vault_address }}"
VAULT_CACERT: "{{ vault_ssl_ca_dest }}"
VAULT_SKIP_VERIFY: "true"
changed_when: false
- name: "HashiCorp - Nomad / Vault : Ensure nomad token is generated."
ansible.builtin.shell: "{{ vault_binary }} token create -policy nomad-server -period 72h -orphan -format=json | jq -r .auth.client_token"
args:
chdir: "{{ nomad_data_directory }}"
environment:
VAULT_TOKEN: "{{ admin_token }}"
VAULT_ADDR: "{{ vault_address }}"
VAULT_CACERT: "{{ vault_ssl_ca_dest }}"
VAULT_SKIP_VERIFY: "true"
changed_when: false
register: nomad_server_token_raw
when: ansible_hostname in groups['servers']
delegate_to: "{{ groups['servers'] | first }}"
run_once: true
- name: "HashiCorp - Nomad / Vault : Set a fact of the nomad token."
ansible.builtin.set_fact:
nomad_server_vault_token: "{{ nomad_server_token_raw.stdout }}"
- name: "HashiCorp - Nomad / Vault : Ensure vault integration is part of the Nomad config directory."
ansible.builtin.template:
src: templates/hashicorp/vault/vault-nomad-server.hcl.j2
dest: "{{ nomad_etc_directory }}/vault.hcl"
mode: 0644
notify: Restart Nomad
- name: "HashiCorp - Nomad / Vault : Find all vault-nomad-server- files."
ansible.builtin.find:
paths: "{{ nomad_data_directory }}/"
patterns: "vault-nomad-*"
file_type: "file"
when: ansible_hostname in groups['servers']
delegate_to: "{{ groups['servers'] | first }}"
run_once: true
register: vault_files
- name: "HashiCorp - Nomad / Vault : Clean up files."
ansible.builtin.file:
path: "{{ item.path }}"
state: absent
with_items: "{{ vault_files.files }}"
loop_control:
label: "{{ item.path | basename }}"
when: ansible_hostname in groups['servers']
delegate_to: "{{ groups['servers'] | first }}"
run_once: true
- name: "HashiCorp - Nomad / Vault : Certificate collection."
block:
- name: "HashiCorp - Nomad / Vault : Retrieve private key contents."
ansible.builtin.slurp:
src: "{{ ssl_webapp_keyfile_path }}"
register: keyfile
- name: "HashiCorp - Nomad / Vault : Retrieve public key contents."
ansible.builtin.slurp:
src: "{{ ssl_webapp_certfile_path }}"
register: certificate
- name: "HashiCorp - Nomad / Vault : Set the webapp keyfile contents as a fact."
ansible.builtin.set_fact:
webapp_key: "{{ keyfile.content | b64decode }}"
- name: "HashiCorp - Nomad / Vault : Set the webapp certfile contents as a fact."
ansible.builtin.set_fact:
webapp_cert: "{{ certificate.content | b64decode }}"
- name: "HashiCorp - Nomad / Vault : Ensure webapp certificate contents are present."
ansible.builtin.command: '{{ vault_binary }} kv put {{ vault_kv_path }}/ssl-certificates/webapp privatekey="{{ webapp_key }}" certificate="{{ webapp_cert }}"'
environment:
VAULT_TOKEN: "{{ admin_token }}"
VAULT_ADDR: "{{ vault_address }}"
VAULT_CACERT: "{{ vault_ssl_ca_dest }}"
VAULT_SKIP_VERIFY: "true"
changed_when: false
when: ansible_hostname in groups['servers']
delegate_to: "{{ groups['servers'] | first }}"
run_once: true
handlers:
- name: Restart Nomad
ansible.builtin.service:
name: "{{ nomad_service_name }}"
state: restarted