-
-
Notifications
You must be signed in to change notification settings - Fork 357
/
services.yml
101 lines (90 loc) · 3.32 KB
/
services.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
---
## File: services.yml - services configuration
- name: Configure consul services
ansible.builtin.template:
dest: "{{ consul_configd_path }}/service_{{ service_item.id }}.json"
src: service.json.j2
owner: "{{ consul_user }}"
group: "{{ consul_group }}"
mode: "0644"
with_items: "{{ consul_services }}"
loop_control:
loop_var: service_item
notify:
- restart consul
- name: Get the list of service config files
ansible.builtin.find:
paths: "{{ consul_configd_path }}"
file_type: file
register: services_enabled_unix
when: ansible_os_family != 'Windows'
- name: Get the list of service config files [Windows]
ansible.windows.win_find:
paths: "{{ consul_configd_path }}"
file_type: file
register: services_enabled_windows
when: ansible_os_family == 'Windows'
- name: Set var for enabled services
ansible.builtin.set_fact:
services_enabled_files: "{{ services_enabled_unix['files'] }}"
when: ansible_os_family != 'Windows'
- name: Set var for enabled services [Windows]
ansible.builtin.set_fact:
services_enabled_files: "{{ services_enabled_windows['files'] }}"
when: ansible_os_family == 'Windows'
- name: Set fact with list of existing configuration files
ansible.builtin.set_fact:
list_current_service_config: "{{ list_current_service_config | default([]) + [config_file_item.path] }}"
with_items: "{{ services_enabled_files }}"
loop_control:
loop_var: config_file_item
- name: Set fact with list of services we manage
ansible.builtin.set_fact:
managed_files: "{{ managed_files | default([]) }} + [ '{{ consul_configd_path }}/service_{{ service_item.id }}.json' ]"
with_items: "{{ consul_services }}"
loop_control:
loop_var: service_item
when: ansible_os_family != 'Windows'
- name: Find all service config files that we manage [Windows]
ansible.windows.win_stat:
path: "{{ consul_configd_path }}/service_{{ service_config_item.id }}.json"
with_items: "{{ consul_services }}"
loop_control:
loop_var: service_config_item
register: managed_files_win_paths
when: ansible_os_family == 'Windows'
- name: Set fact with list of services we manage [Windows]
ansible.builtin.set_fact:
managed_files: "{{ managed_files | default([]) }} + [ '{{ service_item.stat.path }}' ]"
with_items: "{{ managed_files_win_paths.results }}"
loop_control:
loop_var: service_item
when: ansible_os_family == 'Windows'
- name: Delete non declared services
ansible.builtin.file:
path: "{{ non_declared_service_item }}"
state: absent
when:
- ansible_os_family != 'Windows'
- non_declared_service_item not in managed_files
- non_declared_service_item not in consul_cleanup_ignore_files
with_items: "{{ list_current_service_config }}"
loop_control:
loop_var: non_declared_service_item
ignore_errors: "{{ ansible_check_mode }}"
notify:
- restart consul
- name: Delete non declared services [Windows]
ansible.windows.win_file:
path: "{{ non_declared_service_item }}"
state: absent
when:
- ansible_os_family == 'Windows'
- non_declared_service_item not in managed_files
- non_declared_service_item not in consul_cleanup_ignore_files
with_items: "{{ list_current_service_config }}"
loop_control:
loop_var: non_declared_service_item
ignore_errors: "{{ ansible_check_mode }}"
notify:
- restart consul