HeavyHorst / remco

remco is a lightweight configuration management tool
https://heavyhorst.github.io/remco/
MIT License
324 stars 36 forks source link

Template fliter and function together usage #93

Open itshikanov opened 1 year ago

itshikanov commented 1 year ago

Hello. I.m try convert my confd config to remco.

try to use filter and function together from here https://github.com/HeavyHorst/remco/tree/26a321844506ca53d2840f22e41ed6ea89043cba/docs/content/template

here the code

{% for member in gets("/members/*") %}
{% set data = member.Value | parseJSON %}
{% set conn_url = data.conn_url | split:"/" | index:"2"  %}
        server {{member.Key | base}} {{ replace (conn_url, "@", "/", -1) | base}}  maxconn 300 check port {{ data.api_url | split:"/" | index:"2" | split:":" | index:"1" }}
{% endfor %}

and here another try

{% for member in gets("/members/*") %}
{% set data = member.Value | parseJSON %}
        server {{member.Key | base}} {{ replace (data.conn_url | split:"/" | index:"2", "@", "/", -1) | base}}  maxconn 300 check port {{ data.api_url | split:"/" | index:"2" | split:":" | index:"1" }}
{% endfor %}

Got the error with replace function

2023-07-11T09:12:55.566+0300 [DEBUG] retrieving keys: backend=zookeeper key_prefix=/patroni/15-main prefix=remco[206] resource=haproxy
2023-07-11T09:12:55.628+0300 [DEBUG] compiling source template: prefix=remco[206] resource=haproxy template=/tmp/haproxy.tmpl
2023-07-11T09:12:55.628+0300 [ERROR] default handler: prefix=remco[206] resource=haproxy error="createStageFileAndSync failed: create stage file failed: template execution failed: [Error (where: execution) in /tmp/haproxy.tmpl | Line 33 Col 41 near 'replace'] Function input argument 0 of 'replace' must be of type string or *pongo2.Value (not *pongo2.Value)."

How can i use function and filter together?

itshikanov commented 1 year ago

I'm trying to use Custom filters JavaScript replace filter. Here is example https://github.com/HeavyHorst/remco/blob/26a321844506ca53d2840f22e41ed6ea89043cba/docs/content/template/template-filters.md

{% for member in gets("/members/*") %}
{% set data = member.Value | parseJSON %}
{{ data.conn_url | split:"/" | index:"2" | replace:"@,/"  }}
{% endfor %}

got error

2023-07-11T15:30:29.414+0300 [INFO]  set backend nodes: backend=zookeeper nodes=["192.168.50.1:2181"] prefix=remco[1642]
2023/07/11 15:30:29 Connected to 192.168.50.1:2181
2023/07/11 15:30:29 Authenticated: id=72058294651455021, timeout=4000
2023/07/11 15:30:29 Re-submitting `0` credentials after reconnect
2023-07-11T15:30:29.420+0300 [ERROR] failed to process: prefix=remco[1642] resource=haproxy error="createStageFileAndSync failed: create stage file failed: template execution failed: [Error (where: javascript-filter:replace) | Line 35 Col 44 near 'replace'] TypeError: Object has no member 'replace' at replace (<eval>:10:23(48))"
2023-07-11T15:30:29.420+0300 [ERROR] not all templates could be rendered, trying again after 22 seconds: prefix=remco[1642] resource=haproxy

That work

{{ data.conn_url | toJSON | replace:"@,/" }}

I think because "data.conn_url | toJSON" become String. But if use another pipe, the type of data is changing. If there was toSRING filter at least, it will work