This repository has been archived by the owner on Jun 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfra.yml
232 lines (211 loc) · 7.47 KB
/
infra.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
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
223
224
225
226
227
228
229
230
231
232
---
- name: Create session
hosts: localhost
gather_facts: no
vars_files:
- "{{ training_vars_file }}"
tags:
- create
tasks:
- name: Check current session
stat:
path: "{{ current_session_folder }}"
register: current_session_check
- block:
- name: Set session date
set_fact:
session_date: "{{ now(fmt='%Y-%m-%d') }}"
- name: Prompt session client
pause:
prompt: "Please enter session client (leave empty for inter)"
register: session_client_answer
- name: Set session client
set_fact:
session_client: "{{ session_client_answer.user_input | default('zenika', True) }}"
- name: Set session name
set_fact:
session_name: "{{ session_date }}-{{ training_name }}-{{ session_client }}"
- name: Create session folder
file:
path: "{{ training_folder }}/sessions/{{ session_name }}"
state: directory
- name: Create current session link
file:
path: "{{ current_session_folder }}"
src: "{{ session_name }}"
state: link
- name: Create session vars folder
file:
path: "{{ current_session_vars_folder }}"
state: directory
- name: Store session info
template:
src: session.yml
dest: "{{ current_session_vars_folder }}/main.yml"
when: not current_session_check.stat.exists
- block:
- name: Bootstrap trainees list
template:
src: trainees.yml
dest: "{{ current_session_vars_folder }}"
- name: Pause while listing trainees
pause:
prompt: "Please fill trainees list in file '{{ current_session_vars_folder }}/trainees.yml'"
when: trainees is not defined
- name: Create session extra config file
file:
path: "{{ current_session_vars_folder }}/extra.yml"
state: touch
access_time: preserve
modification_time: preserve
register: session_extra_config
- name: Pause while setting session extra config
pause:
prompt: "Please fill session extra config in file '{{ current_session_vars_folder }}/extra.yml'"
when: session_extra_config is changed
- include_vars:
dir: "{{ current_session_vars_folder }}"
ignore_unknown_extensions: yes
- name: Create VMs
hosts: localhost
gather_facts: no
vars_files:
- "{{ training_vars_file }}"
tags:
- create
tasks:
- name: Create session terraform folder
file:
path: "{{ current_session_folder }}/terraform"
state: directory
- name: Generate terraform plan file
template:
src: main.tf
dest: "{{ current_session_folder }}/terraform/main.tf"
trim_blocks: no
- name: Create VMs
terraform:
project_path: "{{ current_session_folder }}/terraform/"
state: present
force_init: yes
register: terraform
- name: Convert key for PuTTY
command: |
puttygen {{ current_session_folder }}/users/{{ item }}/{{ item }}.pem -o {{ current_session_folder }}/users/{{ item }}/{{ item }}.ppk
args:
creates: "{{ current_session_folder }}/users/{{ item }}/{{ item }}.ppk"
loop: "{{ users }}"
- name: Create session inventory
template:
src: session_hosts.aws_ec2.yml
dest: "{{ current_session_folder }}/hosts.aws_ec2.yml"
- name: Reload inventory
meta: refresh_inventory
- name: Reload group_vars
include_vars:
dir: "{{ current_session_vars_folder }}"
ignore_unknown_extensions: yes
delegate_to: "{{ item }}"
delegate_facts: true
loop: "{{ groups.all }}"
- name: Create trainees configs
template:
src: "{{ item.0 }}"
dest: "{{ current_session_folder }}/users/{{ item.1 }}/{{ item.0 }}"
loop: "{{ ['ssh_config', 'putty.reg.ext', 'hosts'] | product(users) | list }}"
- name: Setup VMs
hosts: all:!localhost
gather_facts: yes
become: yes
vars_files:
- "{{ training_vars_file }}"
tags:
- setup
tasks:
- name: Install base on hosts
include_role:
name: base
- name: Execute roles on hosts
include_role:
name: "{{ role.name }}"
vars:
role_vars: "{{ role.vars | default({}) }}"
when: group_names | union(['all']) | intersect(role.target)
loop: "{{ roles | default([]) }}"
loop_control:
loop_var: role
- name: Send hosts info
hosts: localhost
gather_facts: yes
vars_files:
- "{{ training_vars_file }}"
tags:
- email
tasks:
- name: Load trainer info
include_vars:
file: "{{ trainer_vars_file }}"
failed_when: False
- block:
- name: Prompt trainer name
pause:
prompt: "Please enter your full name (in the form 'Firstname Lastname')"
register: trainer_fullname_answer
- name: Prompt trainer email
pause:
prompt: "Please enter your email (in the form '[email protected]')"
register: trainer_email_answer
- name: Set trainer info
set_fact:
trainer:
fullname: "{{ trainer_fullname_answer.user_input }}"
email: "{{ trainer_email_answer.user_input }}"
when: trainer is not defined or 'fullname' not in trainer or 'email' not in trainer
- name: Store trainer info
template:
src: trainer.yml
dest: "{{ trainer_vars_file }}"
- block:
- name: Send hosts info
aws_ses_mail:
from: "{{ trainer.fullname | default('Zenika trainer') }} <{{ trainer.email }}>"
to: "{{ trainee.full_name }} <{{ trainee.email }}>"
subject: "Zenika - Training {{ session_name }} - Connection data"
body: "{{ lookup('template', './templates/mail_body.html') }}"
subtype: html
attach:
- "{{ current_session_folder }}/users/{{ trainee.username }}/{{ trainee.username }}.pem"
- "{{ current_session_folder }}/users/{{ trainee.username }}/{{ trainee.username }}.ppk"
- "{{ current_session_folder }}/users/{{ trainee.username }}/ssh_config"
- "{{ current_session_folder }}/users/{{ trainee.username }}/putty.reg.ext" # '.reg' is an illegal attachment extension in AWS SES
- "{{ current_session_folder }}/users/{{ trainee.username }}/hosts"
region: "{{ aws_ses_region }}"
loop: "{{ [{'username': 'trainer', 'full_name': trainer.fullname | default('Zenika trainer'), 'email': trainer.email}] | union(trainees | default([])) | list }}"
loop_control:
loop_var: trainee
when: (terraform is defined and terraform is changed) or 'email' in ansible_run_tags
- name: Destroy session
hosts: localhost
gather_facts: no
tags:
- never
- destroy
tasks:
- name: Check current session
stat:
path: "{{ current_session_folder }}"
register: current_session_check
- block:
- name: Destroy VMs
terraform:
project_path: "{{ current_session_folder }}/terraform/"
state: absent
- name: Delete session folder
file:
path: "{{ current_session_check.stat.lnk_source }}"
state: absent
- name: Delete current session
file:
path: "{{ current_session_folder }}"
state: absent
when: current_session_check.stat.exists