-
Notifications
You must be signed in to change notification settings - Fork 1
/
c4b-environment.yml
316 lines (277 loc) · 10.5 KB
/
c4b-environment.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
---
- name: Setup Phase
hosts: localhost
gather_facts: true
vars_prompt:
- name: license_path
prompt: What is the path to your Chocolatey Business license?
default: ./files/chocolatey.license.xml
private: false
- name: certificate_path
prompt: What is the path to your Certificate file?
default: ./files/certificate.pfx
private: false
- name: certificate_password
prompt: What is the password to your Certificate file?
private: true
unsafe: true
tasks:
- name: Ensure credentials Directory
ansible.builtin.file:
path: credentials
state: directory
- name: Ensure a valid Chocolatey for Business License
block:
- name: Get Chocolatey License
ansible.builtin.set_fact:
license_content: "{{ lookup('file', license_path) }}"
- name: Get Chocolatey License Expiration
ansible.builtin.set_fact:
license_expiry: "{{ license_content | regex_search('expiration=\".+?\"') | regex_replace('expiration=\"(.+)\"', '\\1') | trim() }}"
- name: Test License Expiry
ansible.builtin.assert:
that:
- license_expiry is defined
- license_expiry | to_datetime('%Y-%m-%dT%H:%M:%S.0000000')
- license_expiry > ansible_date_time.iso8601
quiet: true
# - name: Ensure valid Certificate(s)
- name: Test Certificate Password
community.crypto.openssl_pkcs12:
action: parse
src: "{{ certificate_path }}"
path: credentials/cert.cer
passphrase: "{{ certificate_password }}"
- name: Check Certificate Values
community.crypto.x509_certificate_info:
path: credentials/cert.cer
register: cert_data
- name: Test Certificate Values
ansible.builtin.assert:
that:
- cert_data.subject.commonName is defined
- cert_data.subject.commonName is not match('^\*\.')
quiet: true
- name: Set Certificate Path
ansible.builtin.set_fact:
certificate_path: "{{ certificate_path }}"
certificate_password: "{{ certificate_password }}"
ccm_fqdn: "{{ cert_data.subject.commonName }}"
- name: Set FQDN Value in File
ansible.builtin.copy:
dest: credentials/ccm_fqdn
content: "{{ cert_data.subject.commonName }}"
- name: Set database_connection_string
ansible.builtin.set_fact:
database_connection_string: "{{ database_connection_string }}"
when: database_connection_string is defined
- name: Find Local Packages
ansible.builtin.find:
paths: files
patterns: '*.nupkg'
register: existing_packages
- name: Find Local Plugins
ansible.builtin.find:
paths: files
patterns: 'JenkinsPlugins.zip'
register: existing_plugins
tags: setup, always
- name: Setup Nexus Repository
hosts: "{{ nexus_server | default('ccm_server') }}"
handlers:
- name: Remove Local Chocolatey Source
chocolatey.chocolatey.win_chocolatey_source:
name: "choco-setup"
state: absent
vars:
local_package_path: C:\choco-setup\
certificate_path: "{{ hostvars['localhost']['certificate_path'] }}"
certificate_password: "{{ hostvars['localhost']['certificate_password'] }}"
tasks:
- name: Ensure Local Package Directory
ansible.windows.win_file:
path: "{{ local_package_path }}"
state: directory
- name: Copy Local Install Files
ansible.windows.win_copy:
src: "{{ item }}"
dest: "{{ local_package_path }}"
_copy_mode: explode
when: hostvars.localhost.existing_packages.files | length > 0
loop: "{{ hostvars.localhost.existing_packages.files | selectattr('path', 'in', (lookup('file', 'chocolatey.json') | from_json | json_query('nexus_server') | list)) | map(attribute='path') | list }}"
- name: Redefine Repository
ansible.builtin.set_fact:
repository:
- name: ChocolateySetup
url: "{{ local_package_path }}"
when: hostvars.localhost.existing_packages.files | length > 0
- name: Ensure Chocolatey
ansible.builtin.include_role:
name: win_chocolateylicensed
vars:
license_content: "{{ hostvars.localhost.license_content }}"
- name: Setup Nexus
ansible.builtin.import_tasks: setup-nexus.yml
- name: Upload Packages to Setup Repository
upload_nupkg_to_nexus:
baseuri: "https://{{ nexus_fqdn }}:{{ nexus_port }}"
repository: ChocolateySetup
file: "{{ item }}"
username: admin
password: "{{ nexus_password }}"
when: hostvars.localhost.existing_packages.files | length > 0
loop: "{{ hostvars.localhost.existing_packages.files | map(attribute='path') | list }}"
delegate_to: localhost
no_log: true
tags: nexus
- name: Setup Servers
hosts:
- "{{ ccm_server | default('ccm_server') }}"
- "{{ database_server | default('ccm_server') }}"
- "{{ jenkins_server | default('ccm_server') }}"
- "{{ nexus_server | default('ccm_server') }}"
tasks:
- name: Redefine Repository
ansible.builtin.set_fact:
repository:
- name: ChocolateySetup
url: "https://{{ nexus_fqdn }}:{{ nexus_port }}/repository/ChocolateySetup/index.json"
user: chocouser
password: "{{ chocouser_password }}"
when: hostvars.localhost.existing_packages.files | length > 0
- name: Ensure Chocolatey
ansible.builtin.include_role:
name: win_chocolateylicensed
vars:
license_content: "{{ hostvars.localhost.license_content }}"
- name: Setup Jenkins
hosts: "{{ jenkins_server | default('ccm_server') }}"
vars:
certificate_path: "{{ hostvars['localhost']['certificate_path'] }}"
certificate_password: "{{ hostvars['localhost']['certificate_password'] }}"
tasks:
- name: Setup Jenkins
ansible.builtin.import_tasks: setup-jenkins.yml
tags: jenkins
- name: Setup Database
hosts: "{{ database_server | default('ccm_server') }}"
tasks:
- name: Install DbaTools Library
chocolatey.chocolatey.win_chocolatey:
name: dbatools-library.powershell
state: present
- name: Install DbaTools
chocolatey.chocolatey.win_chocolatey:
name: dbatools
state: present
- name: Setup Database
ansible.builtin.import_tasks: setup-database.yml
when: hostvars['localhost']['database_connection_string'] is not defined
- name: Set Connection String
ansible.builtin.set_fact:
database_connection_string: "{{ hostvars['localhost']['database_connection_string'] }}"
when: hostvars['localhost']['database_connection_string'] is defined
tags: database
- name: Setup Chocolatey Central Management
hosts: ccm_server
vars:
certificate_path: "{{ hostvars['localhost']['certificate_path'] }}"
certificate_password: "{{ hostvars['localhost']['certificate_password'] }}"
database_connection_string: "{{ hostvars['database_server']['database_connection_string'] }}"
tasks:
- name: Setup CCM
ansible.builtin.import_tasks: setup-ccm.yml
tags: ccm
- name: Output Connection File
hosts: localhost
tasks:
- name: Output Environment Configuration Document
ansible.builtin.template:
src: templates/Passwords.html.j2
dest: credentials/CCM.html
mode: '600'
tags: creds
- name: Create License Package
hosts: ccm_server
tasks:
- name: Ensure Package Directory
ansible.windows.win_file:
path: "{{ local_package_path }}\\chocolatey-license"
state: directory
- name: Drop Files
ansible.windows.win_template:
src: "{{ item }}"
dest: "{{ local_package_path }}\\chocolatey-license\\{{ item | basename | regex_replace('\\.j2$', '') }}"
with_fileglob:
- "../templates/LicensePackage/*.j2"
register: license_files
- name: Pack and Push License Package
ansible.windows.win_powershell:
parameters:
PackagePath: "{{ local_package_path }}\\chocolatey-license\\"
LicenseExpiry: "{{ hostvars.localhost.license_expiry }}"
script: |
param(
[string]$PackagePath,
[string]$LicenseExpiry
)
$Version = Get-Date $LicenseExpiry -f "yyyy.MM.dd"
Get-ChildItem $PackagePath -Recurse -Filter "chocolatey-license.nuspec" | ForEach-Object {
choco pack $_.FullName --version $Version --output-directory $PackagePath
}
if ($LastExitCode -eq 0) {
Get-ChildItem $PackagePath -Filter *.nupkg | ForEach-Object {
choco push $_.FullName --source "https://{{ nexus_fqdn }}:{{ nexus_port }}/repository/ChocolateyInternal/" --api-key "{{ lookup('file', 'credentials/nexus_apikey') }}"
if ($LastExitCode -ne 0) {
$Ansible.Failed = $true
}
}
}
when: license_files.changed
vars:
local_package_path: "C:\\choco-setup\\packages"
tags: licensepackage
- name: Internalize Packages
hosts: localhost
tasks:
- name: Run Internalization Job
when: internalize_packages is defined
trigger_jenkins_build:
baseurl: "https://{{ jenkins_fqdn }}:{{ jenkins_port }}"
username: "admin"
password: "{{ jenkins_password }}"
name: Internalize packages from the Chocolatey Community and Licensed Repositories
parameters:
P_PKG_LIST: "{{ internalize_packages | join(',') }}"
no_log: true
tags: internalize
- name: Final Setup
hosts:
- "{{ ccm_server | default('ccm_server') }}"
- "{{ database_server | default('ccm_server') }}"
- "{{ jenkins_server | default('ccm_server') }}"
- "{{ nexus_server | default('ccm_server') }}"
tasks:
- name: Add Agent to Server
ansible.builtin.include_role:
name: win_chocolateyagent
- name: Ensure ChocolateySetup is Removed
chocolatey.chocolatey.win_chocolatey_source:
name: ChocolateySetup
state: absent
- name: Add README.html
ansible.windows.win_template:
src: templates/Passwords.html.j2
dest: C:\\choco-setup\\README.html
mode: '600'
vars:
ccm_hostname: "{{ ccm_fqdn }}"
repository:
- name: ChocolateyInternal
url: "https://{{ nexus_fqdn }}:{{ nexus_port }}/repository/ChocolateyInternal/index.json"
user: chocouser
password: "{{ chocouser_password }}"
client_salt: "{{ ccm_client_salt }}"
service_salt: "{{ ccm_service_salt }}"
tags: chocolatey, agent
...