aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGaƫtan <36162164+gaetanfl@users.noreply.github.com>2018-05-17 17:11:14 +0200
committerGitHub <noreply@github.com>2018-05-17 17:11:14 +0200
commit6b4dbb21c7c1e081f8c9d8f84cd9e3153468315a (patch)
treea4e74004829038caa97ff4c7b0c23eaa79d01cb4
parent03d5555b42e25dd6da9a0241e8a5f5937a9bbeb8 (diff)
parente39a878d84f3b3ec5265d332e5e06ee0edd55418 (diff)
downloadansible-kong-app-0.4.tar.gz
ansible-kong-app-0.4.tar.zst
ansible-kong-app-0.4.zip
Merge pull request #5 from gaetanfl/support_backendv0.4
Support ring-balancer
-rw-r--r--README.md9
-rw-r--r--tasks/kong-app.yml4
-rw-r--r--tasks/upstream.yml55
-rw-r--r--tests/test.yml9
4 files changed, 73 insertions, 4 deletions
diff --git a/README.md b/README.md
index bdf665e..8db9391 100644
--- a/README.md
+++ b/README.md
@@ -15,7 +15,14 @@ Role Variables
15 * `kong_app_admin_apikey` the apikey to use kong admin api. Default to "" 15 * `kong_app_admin_apikey` the apikey to use kong admin api. Default to ""
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. May refer to an upstream by its name (https://upstream\_name/path)
19 * `upstream` if the url reference an upstream a dict with the configuration, optional
20 * `conf` the configuration as expected by kong for an upstream creation
21 * `name` mandatory name for the upstream
22 * `healthchecks` optional healthchecks configuration as expected by kong api
23 * `targets` an array of dict defining a target for kong
24 * `target` the host:port to reach the target (mandatory)
25 * `weight` the weight of the target (optional)
19 * `plugins` An array of plugins to activate with their name and config in a dict 26 * `plugins` An array of plugins to activate with their name and config in a dict
20 * `name` 27 * `name`
21 * `config` 28 * `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..43c1baf
--- /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,404
54 with_items: "{{ kong_app_service_targets_check.json.data }}"
55 when: not kong_app_service_targets_check is skipped
diff --git a/tests/test.yml b/tests/test.yml
index fb0d9b8..490c967 100644
--- a/tests/test.yml
+++ b/tests/test.yml
@@ -3,6 +3,9 @@
3 remote_user: root 3 remote_user: root
4 roles: 4 roles:
5 - role: ansible-kong-app 5 - role: ansible-kong-app
6 kong_app_admin_url: http://127.0.0.1:8000 6 kong_servers:
7 kong_app_service_name: toto 7 - kong_app_admin_url: http://127.0.0.1:8000
8 kong_app_service_url: http://example.com 8 kong_app_admin_apikey: ""
9 services:
10 - url: http://example.com
11 name: test