Browse Source

Merge "Add 'absent' service state"

Zuul 5 months ago
parent
commit
75b2519062
4 changed files with 75 additions and 5 deletions
  1. 2
    0
      defaults/main.yml
  2. 20
    3
      tasks/haproxy_service_config.yml
  3. 8
    0
      tests/test-vars.yml
  4. 45
    2
      tests/test.yml

+ 2
- 0
defaults/main.yml View File

@@ -38,6 +38,8 @@ haproxy_stats_refresh_interval: 60
38 38
 # defined for each service.
39 39
 haproxy_backup_nodes: []
40 40
 
41
+haproxy_service_configs: []
42
+# Example:
41 43
 # haproxy_service_configs:
42 44
 #   - service:
43 45
 #       haproxy_service_name: haproxy_all

+ 20
- 3
tasks/haproxy_service_config.yml View File

@@ -13,21 +13,38 @@
13 13
 # See the License for the specific language governing permissions and
14 14
 # limitations under the License.
15 15
 
16
-- name: "Create haproxy service config files"
16
+- name: Create haproxy service config files
17 17
   template:
18 18
     src: service.j2
19 19
     dest: "/etc/haproxy/conf.d/{{ item.service.haproxy_service_name }}"
20
-  with_items: "{{ haproxy_service_configs | default([]) }}"
20
+  with_items: "{{ haproxy_service_configs }}"
21 21
   when:
22 22
     - (item.service.haproxy_backend_nodes is defined and
23 23
       item.service.haproxy_backend_nodes | length > 0) or
24 24
       (item.service.haproxy_backup_nodes is defined and
25 25
       item.service.haproxy_backup_nodes | length > 0)
26
-    - item.service.haproxy_service_enabled | default('True') | bool
26
+    - (item.service.haproxy_service_enabled | default('True')) | bool
27
+    - (item.service.state is not defined or item.service.state != 'absent')
27 28
   notify: Regenerate haproxy configuration
28 29
   tags:
29 30
     - haproxy-service-config
30 31
 
32
+- name: Remove haproxy service config files for absent services
33
+  file:
34
+    path: "/etc/haproxy/conf.d/{{ item.service.haproxy_service_name }}"
35
+    state: absent
36
+  notify: Regenerate haproxy configuration
37
+  with_items: "{{ haproxy_service_configs }}"
38
+  when:
39
+    - ((item.service.haproxy_backend_nodes is defined and
40
+      item.service.haproxy_backend_nodes | length == 0) and
41
+      (item.service.haproxy_backup_nodes is defined and
42
+      item.service.haproxy_backup_nodes | length == 0)) or
43
+      (not ((item.service.haproxy_service_enabled | default('True')) | bool)) or
44
+      (item.service.state is defined and item.service.state == 'absent')
45
+  tags:
46
+    - haproxy-service-config
47
+
31 48
 - name: Prevent SELinux from preventing haproxy from binding to arbitrary ports
32 49
   seboolean:
33 50
     name: haproxy_connect_any

+ 8
- 0
tests/test-vars.yml View File

@@ -31,3 +31,11 @@ haproxy_service_configs:
31 31
       haproxy_backend_ca: False
32 32
       haproxy_ssl: False
33 33
       haproxy_balance_type: http
34
+  - service:
35
+      haproxy_service_name: test_absent_service
36
+      haproxy_backend_nodes:
37
+        - name: "localhost"
38
+          ip_addr: "127.0.0.1"
39
+      haproxy_port: 65535
40
+      haproxy_balance_type: tcp
41
+      state: "{{ absent_service_state }}"

+ 45
- 2
tests/test.yml View File

@@ -18,7 +18,50 @@
18 18
   connection: local
19 19
   user: root
20 20
   become: true
21
-  roles:
22
-    - role: "haproxy_server"
23 21
   vars_files:
24 22
     - test-vars.yml
23
+  tasks:
24
+    - name: Create marker file for idempotence
25
+      copy:
26
+        content: mark
27
+        dest: /tmp/haproxy_pass1
28
+      register: haproxy_pass1
29
+
30
+    - name: Set fact for idempotence test
31
+      set_fact:
32
+        idempotence_pass_1: "{{ haproxy_pass1 is changed }}"
33
+
34
+    - name: Set fact for absent service state
35
+      set_fact:
36
+        absent_service_state: "{{ (haproxy_pass1 is changed) | ternary('present', 'absent') }}"
37
+
38
+    - name: Run the haproxy_server role
39
+      include_role:
40
+        name: "haproxy_server"
41
+
42
+    - name: Run role again on first pass
43
+      when:
44
+        - "idempotence_pass_1 | bool"
45
+      block:
46
+        - name: Ensure the absent service is present
47
+          stat:
48
+            path: "/etc/haproxy/conf.d/test_absent_service"
49
+          register: absent_services
50
+          failed_when: not absent_services.stat.exists
51
+
52
+        - name: Set fact for absent service state
53
+          set_fact:
54
+            absent_service_state: "absent"
55
+
56
+        - name: Run the haproxy_server role (again)
57
+          include_role:
58
+            name: "haproxy_server"
59
+
60
+    - name: Ensure the absent service is missing
61
+      stat:
62
+        path: "/etc/haproxy/conf.d/test_absent_service"
63
+      register: absent_services
64
+      when:
65
+        - "not (idempotence_pass_1 | bool)"
66
+      failed_when: absent_services.stat.exists
67
+

Loading…
Cancel
Save