Loop issue with nested variable and dict

580 views
Skip to first unread message

R. Dikkes

unread,
Aug 5, 2015, 6:55:08 AM8/5/15
to Ansible Project
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

Dick Davies

unread,
Aug 5, 2015, 9:01:44 AM8/5/15
to ansible list
I hit the exact same problem, and someone on IRC pointed me at {% raw
%}....{% endraw %}

http://jinja.pocoo.org/docs/dev/templates/#escaping

R. Dikkes

unread,
Aug 5, 2015, 9:08:10 AM8/5/15
to Ansible Project, di...@hellooperator.net
But that would leave alone my Go template language right? It wouldn't solve the looping over instead of inside the template, am i correct?
Thank you for your answer! :)

R. Dikkes

unread,
Aug 5, 2015, 7:01:34 PM8/5/15
to Ansible Project, di...@hellooperator.net
Issue solved. :)
Reply all
Reply to author
Forward
0 new messages