Hi,
I am trying to do the following:
Consul-template needs to generate a haproxy config, the consul template i
want to be dynamic filled with Ansible.
The consul template however is a Go template and unfortunately Go uses the
brackets yaml uses. To be able to build a Go template ansible templates i
am putting the Go syntax in a variable.
My whole idea is to place all applications that i want consul to generate
templates for in a variable file, and let these variables be used in a
template i generate to generate the Go template for consul.
I got most of it to work, except for the fact that the variable is looping
over the template instead of within the template. I suspect i need to do it
all different.
The task file main.yml:
---
- include_vars: apps.yml
- name: "Place the Consul Template for haproxy"
template: src=haproxy.ctmpl.yml dest=/etc/haproxy/haproxy-template.ctmpl
owner=consul group=consul mode=0644
with_dict: apps_gen
when: "'load_balancer' in {{ group_names }}"
***
The vars file apps.yml:
---
apps_gen:
app_bridge:
name: bridge-webapp
app_bridge2:
name: bridge-webapp2
****
The defaults file:
---
consul_template_haproxy:
- name: '# Frontend {{ item.value.name }}'
config:
- {{ item.value.name }}_acl
consul_template_backend:
- name: 'backend {{ item.value.name }}'
config:
- mode http{{'{{'}}range service "{{ item.value.name }}"{{'}}'}}
- server {{'{{'}}.Node{{'}}'}}
{{'{{'}}.Address{{'}}'}}:{{'{{'}}.Port{{'}}'}} check{{'{{'}}end{{'}}'}}
***
The template file haproxy.ctmpl.yml:
---
{% for value in apps_gen %}
{% for value in consul_template_frontend %}
{{value.name}}
{% for config in value.config %}
{{config}}
{% endfor %}
{% endfor %}
{% endfor %}
{% for value in apps_gen %}
{% for value in consul_template_backend %}
{{value.name}}
{% for config in value.config %}
{{config}}
{% endfor %}
{% endfor %}
{% endfor %}
****
The result:
# Frontend bridge-webapp
bridge-webapp_acl
# Frontend bridge-webapp
bridge-webapp_acl
backend bridge-webapp
mode http {{range service "bridge-webapp"}}
server {{.Node}} {{.Address}}:{{.Port}} check{{end}}
backend bridge-webapp
mode http {{range service "bridge-webapp"}}
server {{.Node}} {{.Address}}:{{.Port}} check{{end}}
The result i expected:
backend bridge-webapp
mode http {{range service "bridge-webapp"}}
server {{.Node}} {{.Address}}:{{.Port}} check{{end}}
backend bridge-webapp2
mode http {{range service "bridge-webapp2"}}
server {{.Node}} {{.Address}}:{{.Port}} check{{end}}
What i see is that the loop is going over the template when i run the
playbook, while i expected it to be within the template:
changed: [loadbalancer] => (item={'key': 'app_bridge2', 'value': {'name':
'bridge-webapp2'}}) => {"changed": true...
changed: [loadbalancer] => (item={'key': 'app_bridge', 'value': {'name':
'bridge-webapp'}}) => {"changed": true...
Can anyone help me? Im a bit lost, i really appreciate any help! I am
currently thinking about registering the results and use that as a variable
in the template, but it feels like im starting to play the lead role in
Inception. This does not help readability
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/5e692848-eaf0-4493-8010-ec8405055ebb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.