Files @ 7cabc17c71c3
Branch filter:

Location: majic-ansible-roles/roles/common/tasks/main.yml - annotation

branko
MAR-218: Quote all octal values in YAML files:

- Fixes linting errors, and ensures there is no ambiguity in case of
YAML specification changes.
  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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
626eadba53b7
626eadba53b7
5f9f8aef3473
5f9f8aef3473
5f9f8aef3473
5f9f8aef3473
5f9f8aef3473
5f9f8aef3473
5f9f8aef3473
5f9f8aef3473
5f9f8aef3473
5f9f8aef3473
5f9f8aef3473
5f9f8aef3473
72af31a420be
72af31a420be
72af31a420be
72af31a420be
72af31a420be
72af31a420be
72af31a420be
72af31a420be
5f9f8aef3473
5f9f8aef3473
5f9f8aef3473
babda105c9cf
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
7cabc17c71c3
babda105c9cf
babda105c9cf
babda105c9cf
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
babda105c9cf
babda105c9cf
626eadba53b7
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
7cabc17c71c3
3a46859c3d4c
2d0a09dc0e00
2d0a09dc0e00
626eadba53b7
a3d247bb2e09
a3d247bb2e09
3a46859c3d4c
3a46859c3d4c
3a46859c3d4c
3a46859c3d4c
3a46859c3d4c
3a46859c3d4c
2d15529786b7
3a46859c3d4c
626eadba53b7
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
6b8b1d4c9061
2d0a09dc0e00
2d0a09dc0e00
626eadba53b7
626eadba53b7
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
6b8b1d4c9061
fefde7a74438
2d0a09dc0e00
626eadba53b7
f4e9cd36dad4
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
7cabc17c71c3
f4e9cd36dad4
e15b53d59517
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
7cabc17c71c3
e15b53d59517
f4e9cd36dad4
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
7cabc17c71c3
f4e9cd36dad4
f4e9cd36dad4
2d0a09dc0e00
f4e9cd36dad4
483065c6c24f
2d0a09dc0e00
2d0a09dc0e00
483065c6c24f
483065c6c24f
483065c6c24f
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
7cabc17c71c3
a0597f1f2578
d0e45c50137a
d0e45c50137a
483065c6c24f
626eadba53b7
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
626eadba53b7
284ed92d40bb
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
284ed92d40bb
626eadba53b7
2d0a09dc0e00
a20ca43cd967
2d0a09dc0e00
626eadba53b7
a717a6ccd782
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
7cabc17c71c3
d0e45c50137a
a717a6ccd782
626eadba53b7
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
922cda0a1834
626eadba53b7
626eadba53b7
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
922cda0a1834
626eadba53b7
626eadba53b7
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
6b8b1d4c9061
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
922cda0a1834
626eadba53b7
626eadba53b7
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
626eadba53b7
fe6cdb2443c7
626eadba53b7
626eadba53b7
626eadba53b7
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
626eadba53b7
626eadba53b7
626eadba53b7
626eadba53b7
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
626eadba53b7
9eca957bb9db
9eca957bb9db
76ed37089b33
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
7cabc17c71c3
922cda0a1834
6c256b0514cf
6c256b0514cf
a3d247bb2e09
a3d247bb2e09
0bc967a67750
0bc967a67750
0bc967a67750
2d0a09dc0e00
6c256b0514cf
2d15529786b7
0bc967a67750
8018788c81c4
b1675b47ba12
8018788c81c4
8018788c81c4
b1675b47ba12
8018788c81c4
8018788c81c4
8018788c81c4
8018788c81c4
8018788c81c4
8018788c81c4
8018788c81c4
8018788c81c4
8018788c81c4
8018788c81c4
7cabc17c71c3
8018788c81c4
8018788c81c4
941f4f372672
941f4f372672
2d0a09dc0e00
2d0a09dc0e00
d08a4bb0d74b
941f4f372672
941f4f372672
2d0a09dc0e00
8018788c81c4
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
7cabc17c71c3
941f4f372672
941f4f372672
941f4f372672
941f4f372672
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
7cabc17c71c3
941f4f372672
941f4f372672
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
7cabc17c71c3
941f4f372672
941f4f372672
941f4f372672
1561f2f0a525
1561f2f0a525
1561f2f0a525
3e6eaaff335e
1561f2f0a525
1561f2f0a525
941f4f372672
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
7cabc17c71c3
941f4f372672
941f4f372672
941f4f372672
aa7b596ef595
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
aa7b596ef595
7387caca37f3
8018788c81c4
8018788c81c4
8018788c81c4
8018788c81c4
8018788c81c4
8018788c81c4
7cabc17c71c3
8018788c81c4
8018788c81c4
8018788c81c4
8018788c81c4
8018788c81c4
8018788c81c4
8018788c81c4
8018788c81c4
aa2802e42d9d
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
7cabc17c71c3
aa2802e42d9d
aa2802e42d9d
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
7cabc17c71c3
aa2802e42d9d
aa2802e42d9d
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
c254c806349c
c254c806349c
0b86d3da5a29
2d0a09dc0e00
2d0a09dc0e00
aa2802e42d9d
bb5be981d963
2d0a09dc0e00
2d0a09dc0e00
d08a4bb0d74b
bb5be981d963
16eaeca46aa3
16eaeca46aa3
16eaeca46aa3
16eaeca46aa3
16eaeca46aa3
76588ecee3af
5de184fba30f
2d0a09dc0e00
5de184fba30f
5de184fba30f
5de184fba30f
d08a4bb0d74b
76588ecee3af
76588ecee3af
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
76588ecee3af
76588ecee3af
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
76588ecee3af
8457949f27d4
8457949f27d4
8457949f27d4
8457949f27d4
8457949f27d4
8457949f27d4
8457949f27d4
8457949f27d4
8457949f27d4
8457949f27d4
8457949f27d4
8457949f27d4
8457949f27d4
8457949f27d4
8457949f27d4
c8b5cf1283e0
c8b5cf1283e0
8457949f27d4
8457949f27d4
8457949f27d4
8457949f27d4
251942f9d332
00a669691076
00a669691076
00a669691076
00a669691076
00a669691076
00a669691076
c8b5cf1283e0
c8b5cf1283e0
00a669691076
00a669691076
00a669691076
00a669691076
00a669691076
251942f9d332
8457949f27d4
251942f9d332
00a669691076
00a669691076
00a669691076
00a669691076
251942f9d332
76588ecee3af
2d0a09dc0e00
6f78f2d06f04
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
7cabc17c71c3
6f78f2d06f04
5f9f8aef3473
6f78f2d06f04
76588ecee3af
76588ecee3af
00a669691076
2d0a09dc0e00
251942f9d332
6b8b1d4c9061
76588ecee3af
251942f9d332
251942f9d332
251942f9d332
a0597f1f2578
251942f9d332
5f9f8aef3473
76588ecee3af
76588ecee3af
2d0a09dc0e00
6f78f2d06f04
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
7cabc17c71c3
6f78f2d06f04
6f78f2d06f04
76588ecee3af
76588ecee3af
2d0a09dc0e00
6f78f2d06f04
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
7cabc17c71c3
6f78f2d06f04
6f78f2d06f04
76588ecee3af
76588ecee3af
d0c6f26ece9f
d0c6f26ece9f
d0c6f26ece9f
2d0a09dc0e00
2d0a09dc0e00
7cabc17c71c3
6f78f2d06f04
d0c6f26ece9f
d0c6f26ece9f
76588ecee3af
76588ecee3af
2d0a09dc0e00
2d0a09dc0e00
180d7b99f777
2d0a09dc0e00
2d0a09dc0e00
7cabc17c71c3
6f78f2d06f04
180d7b99f777
180d7b99f777
76588ecee3af
380556b776cd
2d0a09dc0e00
380556b776cd
380556b776cd
6b8b1d4c9061
380556b776cd
6f78f2d06f04
380556b776cd
380556b776cd
76588ecee3af
5f9f8aef3473
76588ecee3af
76588ecee3af
76588ecee3af
6b8b1d4c9061
2d0a09dc0e00
76588ecee3af
76588ecee3af
76588ecee3af
76588ecee3af
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
7cabc17c71c3
76588ecee3af
76588ecee3af
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
2d0a09dc0e00
7cabc17c71c3
76588ecee3af
5bc6b7fb4cb5
5bc6b7fb4cb5
5bc6b7fb4cb5
72af31a420be
72af31a420be
d08a4bb0d74b
49af212543b0
5bc6b7fb4cb5
fefde7a74438
fefde7a74438
fefde7a74438
fefde7a74438
fefde7a74438
fefde7a74438
fefde7a74438
fefde7a74438
fefde7a74438
5bc6b7fb4cb5
5bc6b7fb4cb5
5bc6b7fb4cb5
72af31a420be
5bc6b7fb4cb5
5bc6b7fb4cb5
7cabc17c71c3
49af212543b0
5bc6b7fb4cb5
5bc6b7fb4cb5
5bc6b7fb4cb5
7387caca37f3
3802c52995e5
fcf5abdd3ad5
7387caca37f3
aa2802e42d9d
---

# Deprecation
# ===========

- name: Drop deprecated directories and files
  file:
    path: "{{ item }}"
    state: absent
  with_items:
    - "/etc/pip_check_requirements_upgrades-py3"
    - "/var/lib/pipreqcheck/virtualenv-py3"
    - "/etc/cron.d/check_pip_requirements-py3"

- name: Drop deprecated packages
  apt:
    name:
      - ntp
      - ntpdate
    state: absent
    purge: true

# Main implementation
# ===================

- name: Enable use of proxy for retrieving system packages via apt
  template:
    src: "apt_proxy.j2"
    dest: "/etc/apt/apt.conf.d/00proxy"
    owner: root
    group: root
    mode: "0644"
  when: apt_proxy is defined

- name: Disable use of proxy for retrieving system packages via apt
  file:
    path: "/etc/apt/apt.conf.d/00proxy"
    state: absent
  when: apt_proxy is undefined

- name: Deploy pam-auth-update configuration file for enabling pam_umask
  copy:
    src: "pam_umask"
    dest: "/usr/share/pam-configs/umask"
    owner: root
    group: root
    mode: "0644"
  register: pam_umask
  notify:
    - Update PAM configuration

- name: Update PAM configuration  # noqa no-handler
  # [no-handler] Tasks that run when changed should likely be handlers
  #   In order to have consistent behaviour during the first and
  #   subsequent playbook runs, make sure the PAM configuration is
  #   updated immediatelly. This way any files created by commands etc
  #   should end-up with correct permissions straight away.
  command: "/usr/sbin/pam-auth-update --package"
  when: pam_umask.changed
  changed_when: true  # Always results in change due to task logic.

- name: Set login UMASK
  lineinfile:
    dest: "/etc/login.defs"
    state: present
    backrefs: true
    regexp: '^UMASK(\s+)'
    line: 'UMASK\g<1>027'

- name: Set home directory mask
  lineinfile:
    dest: "/etc/adduser.conf"
    state: present
    backrefs: true
    regexp: '^\s*#?\s*DIR_MODE='
    line: 'DIR_MODE=0750'

- name: Deploy bash profile configuration for fancier prompts
  template:
    src: "bash_prompt.sh.j2"
    dest: "/etc/profile.d/bash_prompt.sh"
    owner: root
    group: root
    mode: "0644"

- name: Deploy profile configuration that allows for user-specific profile.d files
  copy:
    src: "user_profile_d.sh"
    dest: "/etc/profile.d/z99-user_profile_d.sh"
    owner: root
    group: root
    mode: "0644"

- name: Replace default and skeleton bashrc
  copy:
    src: "{{ item.key }}"
    dest: "{{ item.value }}"
    owner: root
    group: root
    mode: "0644"
  with_dict:
    bashrc: "/etc/bash.bashrc"
    skel_bashrc: "/etc/skel/.bashrc"

- name: Calculate stock checksum for bashrc root account
  stat:
    path: "/root/.bashrc"
  register: root_bashrc_stat

- name: Replace stock bashrc for root account with skeleton one
  copy:
    src: "skel_bashrc"
    dest: "/root/.bashrc"
    owner: root
    group: root
    mode: "0640"
  # Checksums: bookworm
  when: |
    root_bashrc_stat.stat.checksum == "1a422a148ad225aa5ba33f8dafd2b7cfcdbd701f"

- name: Install sudo
  apt:
    name: sudo
    state: present

- name: Install ssl-cert package
  apt:
    name: ssl-cert
    state: present

- name: Install common packages
  apt:
    name: "{{ common_packages }}"
    state: "present"

- name: Disable electric-indent-mode for Emacs by default for all users
  copy:
    src: "01disable-electric-indent-mode.el"
    dest: "/etc/emacs/site-start.d/01disable-electric-indent-mode.el"
    owner: root
    group: root
    mode: "0644"
  when: "['emacs24', 'emacs24-nox', 'emacs25', 'emacs25-nox', 'emacs', 'emacs-nox'] | intersect(common_packages) | length > 0"

- name: Set-up operating system groups
  group:
    name: "{{ item.name }}"
    gid: "{{ item.gid | default(omit) }}"
    state: present
  with_items: "{{ os_groups }}"

- name: Set-up operating system user groups
  group:
    name: "{{ item.name }}"
    gid: "{{ item.uid | default(omit) }}"
    state: present
  with_items: "{{ os_users }}"

- name: Set-up operating system users
  user:
    name: "{{ item.name }}"
    uid: "{{ item.uid | default(omit) }}"
    group: "{{ item.name }}"
    groups: "{{ ','.join(item.additional_groups | default([])) }}"
    append: true
    shell: /bin/bash
    state: present
    password: "{{ item.password | default('!') }}"
    update_password: on_create
  with_items: "{{ os_users }}"

- name: Set-up authorised keys
  authorized_key:
    user: "{{ item.0.name }}"
    key: "{{ item.1 }}"
  with_subelements:
    - "{{ os_users | selectattr('authorized_keys', 'defined') | list }}"
    - authorized_keys

- name: Disable remote logins for root
  lineinfile:
    dest: "/etc/ssh/sshd_config"
    state: present
    regexp: "^PermitRootLogin"
    line: "PermitRootLogin no"
  notify:
    - Restart SSH

- name: Disable remote login authentication via password
  lineinfile:
    dest: "/etc/ssh/sshd_config"
    state: present
    regexp: "^PasswordAuthentication"
    line: "PasswordAuthentication no"
  notify:
    - Restart SSH

- name: Deploy CA certificates
  copy:
    content: "{{ item.value }}"
    dest: "/usr/local/share/ca-certificates/{{ item.key }}.crt"
    owner: root
    group: root
    mode: "0644"
  with_dict: "{{ ca_certificates }}"
  register: deploy_ca_certificates_result

- name: Update CA certificate cache  # noqa no-handler
  # [no-handler] Tasks that run when changed should likely be handlers
  #   CA certificate cache must be updated immediatelly in order for
  #   applications depending on deployed CA certificates can use them to
  #   validate server/client certificates.
  command: "/usr/sbin/update-ca-certificates --fresh"
  when: deploy_ca_certificates_result.changed
  changed_when: true  # Always results in change due to task logic.

- name: Set-up file diversions for custom files that overrride package-provided ones
  command: "dpkg-divert --divert '{{ item }}.original' --rename '{{ item }}'"
  register: "dpkg_divert"
  changed_when: "'Adding' in dpkg_divert.stdout"
  with_items:
    - "/usr/sbin/ferm"
  notify:
    - Restart ferm

- name: Deploy the patched ferm binary that disables use of legacy iptables
  copy:
    src: ferm_binary
    dest: /usr/sbin/ferm
    owner: root
    group: root
    mode: "0755"
  notify:
    - Restart ferm

- name: Install ferm (for firewall management)
  apt:
    name: ferm
    state: present

- name: Configure ferm init script coniguration file
  copy:
    src: "ferm_default"
    dest: "/etc/default/ferm"
    owner: root
    group: root
    mode: "0644"
  notify:
    - Restart ferm

- name: Create directory for storing ferm configuration files
  file:
    dest: "/etc/ferm/conf.d/"
    state: directory
    owner: root
    group: root
    mode: "0750"

- name: Deploy main ferm configuration file
  copy:
    src: "ferm.conf"
    dest: "/etc/ferm/ferm.conf"
    owner: root
    group: root
    mode: "0640"
  notify:
    - Restart ferm

- name: Verify maintenance_allowed_sources parameter
  fail:
    msg: "Items in maintenance_allowed_sources must IPv4/IPv6 addresses or subnets: {{ item }}"
  when: "not (item is ansible.utils.ipv4_address or item is ansible.utils.ipv6_address)"
  with_items: "{{ maintenance_allowed_sources }}"

- name: Deploy ferm base rules
  template:
    src: "00-base.conf.j2"
    dest: "/etc/ferm/conf.d/00-base.conf"
    owner: root
    group: root
    mode: "0640"
  notify:
    - Restart ferm

- name: Enable and start ferm
  service:
    name: ferm
    state: started
    enabled: true

- name: Deploy script for flushing legacy iptables rules
  copy:
    src: "legacy_iptables_rules.sh"
    dest: "/usr/local/sbin/drop_legacy_iptables_rules.sh"
    owner: root
    group: root
    mode: "0755"

- name: Drop legacy iptables rules
  command: "/usr/local/sbin/drop_legacy_iptables_rules.sh remove"
  register: legacy_iptables_rules
  changed_when: "'Removed legacy iptables for families' in legacy_iptables_rules.stdout"
  notify:
    - Restart ferm

- name: Deploy script for validating server certificates
  copy:
    src: "check_certificate.sh"
    dest: "/usr/local/bin/check_certificate.sh"
    owner: root
    group: root
    mode: "0755"

- name: Set-up directory for holding configuration for certificate validation script
  file:
    path: "/etc/check_certificate"
    state: "directory"
    owner: root
    group: root
    mode: "0755"

- name: Deploy crontab entry for checking certificates
  cron:
    name: "check_certificate"
    cron_file: "check_certificate"
    hour: "0"
    minute: "0"
    job: "/usr/local/bin/check_certificate.sh -q expiration"
    state: present
    user: nobody

- name: Install apticron (for checking available upgrades)
  apt:
    name: apticron
    state: present

# It would be too much hassle to detect changed state, so just ignore it.
- name: Preventively run apticron to avoid issues with locking
  command: /usr/sbin/apticron --cron
  changed_when: false

# Implementation for checking pip requirements files via via pip-tools.
- name: Install packages required for running pip requirements checks
  apt:
    name:
      - python3-setuptools
      - virtualenv
    state: present

- name: Create dedicated group for user running pip requirements checks
  group:
    name: "pipreqcheck"
    gid: "{{ pipreqcheck_gid | default(omit) }}"
    state: present

- name: Create user for running pip requirements checks
  user:
    name: "pipreqcheck"
    uid: "{{ pipreqcheck_uid | default(omit) }}"
    group: "pipreqcheck"
    home: "/var/lib/pipreqcheck"
    state: present

- name: Retrieve system Python interpreter version
  command:
    argv:
      - "/usr/bin/python3"
      - "-c"
      - "import sys; print(sys.version.split(' ')[0])"
  changed_when: false
  register: python_interpreter_version

- name: Retrieve virtual environment Python interpreter version (if initialised)
  command:
    argv:
      - "/var/lib/pipreqcheck/virtualenv/bin/python"
      - "-c"
      - "import sys; print(sys.version.split(' ')[0])"
  become: true
  become_user: "pipreqcheck"
  # Virtual environment perhaps does not exist.
  failed_when: false
  changed_when: false
  register: virtualenv_python_version

- name: Retrieve virtual environment prompt
  command:
    argv:
      - "bash"
      - "-c"
      - "source '/var/lib/pipreqcheck/virtualenv/bin/activate'; printenv PS1"
  become: true
  become_user: "pipreqcheck"
  failed_when: false
  changed_when: false
  register: current_virtualenv_prompt

- name: Remove virtual environment in case of mismatches
  file:
    path: "/var/lib/pipreqcheck/virtualenv"
    state: absent
  when: |
    virtualenv_python_version.rc != 0 or
    virtualenv_python_version.stdout.strip() != python_interpreter_version.stdout.strip() or
    current_virtualenv_prompt.stdout != "(pipreqcheck) "

- name: Create directory for Python virtual environment used for installing/running pip-tools
  file:
    path: "{{ item }}"
    state: directory
    owner: pipreqcheck
    group: pipreqcheck
    mode: "0750"
  with_items:
    - "/var/lib/pipreqcheck"
    - "/var/lib/pipreqcheck/virtualenv"

- name: Create Python virtual environment used for installing/running pip-tools
  command: "/usr/bin/virtualenv --python '{{ item.python_path }}' --prompt '{{ item.virtualenv_prompt }}' '{{ item.virtualenv_path }}'"
  args:
    creates: "{{ item.creates }}"
  become: true
  become_user: "pipreqcheck"
  with_items:
    - name: pipreqcheck
      virtualenv_path: "/var/lib/pipreqcheck/virtualenv"
      virtualenv_prompt: "pipreqcheck"
      python_path: "/usr/bin/python3"
      creates: "/var/lib/pipreqcheck/virtualenv/bin/python3"

- name: Create directory for storing pip requirements files
  file:
    path: "{{ item }}"
    state: "directory"
    owner: root
    group: pipreqcheck
    mode: "0750"
  with_items:
    - "/etc/pip_check_requirements_upgrades"

- name: Set-up directory for storing pip requirements file for pip-tools virtual environment itself
  file:
    path: "{{ item }}"
    state: "directory"
    owner: root
    group: pipreqcheck
    mode: "0750"
  with_items:
    - "/etc/pip_check_requirements_upgrades/pipreqcheck"

- name: Deploy .in file for pip requirements in pip-tools virtual environment
  template:
    src: "pipreqcheck_requirements.in.j2"
    dest: "{{ item.path }}"
    owner: root
    group: pipreqcheck
    mode: "0640"
  with_items:
    - path: "/etc/pip_check_requirements_upgrades/pipreqcheck/requirements.in"
      requirements: "{{ pip_check_requirements_in }}"

- name: Deploy requirements file for pipreqcheck virtual environment
  template:
    src: "pipreqcheck_requirements.txt.j2"
    dest: "{{ item.file }}"
    owner: root
    group: pipreqcheck
    mode: "0640"
  with_items:
    - file: "/etc/pip_check_requirements_upgrades/pipreqcheck/requirements.txt"
      requirements: "{{ pip_check_requirements }}"

- name: Install requirements in the pipreqcheck virtual environment
  pip:
    requirements: "{{ item.requirements }}"
    virtualenv: "{{ item.virtualenv }}"
  become: true
  become_user: pipreqcheck
  with_items:
    - virtualenv: "~pipreqcheck/virtualenv"
      requirements: "/etc/pip_check_requirements_upgrades/pipreqcheck/requirements.txt"

- name: Synchronise pip-tools virtual environment via deployed requirements file
  shell: "source ~pipreqcheck/virtualenv/bin/activate && pip-sync /etc/pip_check_requirements_upgrades/pipreqcheck/requirements.txt"
  args:
    executable: /bin/bash
  become: true
  become_user: "pipreqcheck"
  register: pipreqcheck_pip_sync
  changed_when: "pipreqcheck_pip_sync.stdout != 'Everything up-to-date'"

- name: Deploy script for checking available upgrades
  copy:
    src: "pip_check_requirements_upgrades.sh"
    dest: "/usr/local/bin/pip_check_requirements_upgrades.sh"
    owner: root
    group: root
    mode: "0755"

- name: Deploy crontab entry for checking pip requirements
  copy:
    src: "cron_check_pip_requirements"
    dest: "/etc/cron.d/check_pip_requirements"
    owner: root
    group: root
    mode: "0644"

- name: Install NTP packages
  apt:
    name:
      - ntpsec
      - ntpsec-ntpdate
    state: present
  when: ntp_pools | length > 0

- name: Remove NTP packages
  apt:
    name:
      - ntpsec
      - ntpsec-ntpdate
    state: absent
    purge: true
  when: ntp_pools | length == 0

- name: Deploy NTP configuration
  template:
    src: "ntp.conf.j2"
    dest: "/etc/ntpsec/ntp.conf"
    owner: root
    group: root
    mode: "0644"
  when: ntp_pools | length > 0
  notify:
    - Restart NTP server

- name: Explicitly run all handlers
  include_tasks: ../handlers/main.yml
  when: "run_handlers | default(False) | bool()"
  tags:
    - handlers