diff options
author | Gaëtan Duchaussois <gaetan.duchaussois@fretlink.com> | 2018-05-14 16:22:00 +0200 |
---|---|---|
committer | Gaëtan Duchaussois <gaetan.duchaussois@fretlink.com> | 2018-05-14 16:22:00 +0200 |
commit | fb5b452707a004967d7b27abc5d515a03278d446 (patch) | |
tree | c3e524077759cc2acb6d33bfb320a35c73f17a1f | |
parent | 03d5555b42e25dd6da9a0241e8a5f5937a9bbeb8 (diff) | |
download | ansible-kong-app-fb5b452707a004967d7b27abc5d515a03278d446.tar.gz ansible-kong-app-fb5b452707a004967d7b27abc5d515a03278d446.tar.zst ansible-kong-app-fb5b452707a004967d7b27abc5d515a03278d446.zip |
add support for upstream with multiple targets
-rw-r--r-- | README.md | 5 | ||||
-rw-r--r-- | tasks/kong-app.yml | 4 | ||||
-rw-r--r-- | tasks/upstream.yml | 55 |
3 files changed, 64 insertions, 0 deletions
@@ -16,6 +16,11 @@ Role Variables | |||
16 | * `services` an array of services to setup (default to []) | 16 | * `services` an array of services to setup (default to []) |
17 | * `name` the name of the service to create for this app, mandatory | 17 | * `name` the name of the service to create for this app, mandatory |
18 | * `url` the url of the backend of the app, mandatory | 18 | * `url` the url of the backend of the app, mandatory |
19 | * `upstream` if the url reference an upstream a dict with the configuration | ||
20 | * `conf` the configuration as expected by kong for an upstream creataion | ||
21 | * `targets` an array of dict defining a target for kong | ||
22 | * `target` the host:port to reach the target | ||
23 | * `weight` the weight of the target | ||
19 | * `plugins` An array of plugins to activate with their name and config in a dict | 24 | * `plugins` An array of plugins to activate with their name and config in a dict |
20 | * `name` | 25 | * `name` |
21 | * `config` | 26 | * `config` |
diff --git a/tasks/kong-app.yml b/tasks/kong-app.yml index bff2f46..975e2cd 100644 --- a/tasks/kong-app.yml +++ b/tasks/kong-app.yml | |||
@@ -1,3 +1,7 @@ | |||
1 | - name: Upstream Creation | ||
2 | import_tasks: upstream.yml | ||
3 | when: service.upstream is defined | ||
4 | |||
1 | - name: Check if service exists | 5 | - name: Check if service exists |
2 | uri: | 6 | uri: |
3 | url: "{{ server.kong_app_admin_url }}/services/{{ service.name }}" | 7 | url: "{{ server.kong_app_admin_url }}/services/{{ service.name }}" |
diff --git a/tasks/upstream.yml b/tasks/upstream.yml new file mode 100644 index 0000000..e944c82 --- /dev/null +++ b/tasks/upstream.yml | |||
@@ -0,0 +1,55 @@ | |||
1 | - name: Set upstream variable | ||
2 | set_fact: | ||
3 | upstream: "{{ service.upstream }}" | ||
4 | |||
5 | - name: Check if upstream exists | ||
6 | uri: | ||
7 | url: "{{ server.kong_app_admin_url }}/upstreams/{{ upstream.conf.name }}" | ||
8 | method: GET | ||
9 | headers: | ||
10 | apikey: "{{ server.kong_app_admin_apikey }}" | ||
11 | status_code: 200,404 | ||
12 | register: kong_app_check_upstream | ||
13 | |||
14 | - name: Update or Create upstream | ||
15 | uri: | ||
16 | url: "{{ server.kong_app_admin_url }}/upstreams/{{ exists_upstream | ternary('', upstream.conf.name) }}" | ||
17 | method: "{{ exists_upstream | ternary('POST', 'PATCH') }}" | ||
18 | body: "{{ upstream.conf | to_json }}" | ||
19 | status_code: 200,201 | ||
20 | headers: | ||
21 | apikey: "{{ server.kong_app_admin_apikey }}" | ||
22 | Content-Type: application/json | ||
23 | vars: | ||
24 | exists_upstream: "{{ kong_app_check_upstream.status|default(404) == 404 }}" | ||
25 | |||
26 | - name: Get upstream targets | ||
27 | uri: | ||
28 | url: "{{ server.kong_app_admin_url }}/upstreams/{{ upstream.conf.name }}/targets" | ||
29 | method: GET | ||
30 | headers: | ||
31 | apikey: "{{ server.kong_app_admin_apikey }}" | ||
32 | register: kong_app_service_targets_check | ||
33 | |||
34 | - name: Create targets | ||
35 | uri: | ||
36 | url: "{{ server.kong_app_admin_url }}/upstreams/{{ upstream.conf.name }}/targets" | ||
37 | method: POST | ||
38 | body: "{{ target | to_json }}" | ||
39 | headers: | ||
40 | apikey: "{{ server.kong_app_admin_apikey }}" | ||
41 | Content-Type: application/json | ||
42 | status_code: 201 | ||
43 | with_items: "{{ upstream.targets | default([]) }}" | ||
44 | loop_control: | ||
45 | loop_var: target | ||
46 | |||
47 | - name: Delete old targets | ||
48 | uri: | ||
49 | url: "{{ server.kong_app_admin_url }}/upstreams/{{ upstream.conf.name }}/targets/{{ item.id }}" | ||
50 | method: DELETE | ||
51 | headers: | ||
52 | apikey: "{{ server.kong_app_admin_apikey }}" | ||
53 | status_code: 204 | ||
54 | with_items: "{{ kong_app_service_targets_check.json.data }}" | ||
55 | when: not kong_app_service_targets_check is skipped | ||