Using Ansible, Split and Concatenate Kong Config File"
Scroll DownBelow is an example configuration file for Kong, including some basic configurations for services:
kong_yml:
_format_version: "1.1"
services:
- name: example-service
url: http://example.com/api
routes:
- name: example-route
paths:
- /example
methods:
- GET
we extended this configuration by adding more services, routes, plugins, and other settings as needed. Managing a large Kong configuration file became challenging, so using ansible we split it into multiple files and then concatenating them together making it easier to read, edit, and track changes, improving overall simplicity.
Step 1: Create a directory structure for Ansible role. Navigate to the desired location and create a directory structure like this:
Step 2: In your playbook.yml
, include the "kong" role:
- hosts: localhost
gather_facts: no
vars:
myinput: "vars/kong/includes.yaml"
myoutput: "vars/kong/vars.yml"
roles:
- kong
Step 3: In the roles/kong/tasks/main.yml
file, define the tasks for your Kong role.
- name: include file list
include_vars:
file: "{{ myinput }}"
name: files
- name: concatenating the kong config files
template:
src: concatenate.j2
dest: "{{ myoutput }}"
Step 4: Create a Jinja2 template in templates/concatenate.j2
. This template will be used to concatenate the individual configuration files
{% for i in files["input-files"] %}
{{ lookup('file', i) }}
{% endfor %}
Step 4: I maintain my config files under directory templates/concatenate/config_files
. Whenever I create a new config file I added the file path in /vars/kong/includes.yaml
.
input-files:
- concatenate/config_files/base.yml
- concatenate/config_files/base1.yml
- concatenate/config_files/base2.yml
Now, when you run ansible-playbook kong.yml
, Ansible will execute the tasks which will concatenate the Kong configuration files in the input path and create a single configuration file in the output path /vars/kong/vars.yml
View Comments