# Creation of dict on the fly

**URL:** <https://forum.ansible.com/t/creation-of-dict-on-the-fly/17116>\
**Category:** Ansible Project\
**Created:** [December 20, 2014, 5:16am UTC](https://forum.ansible.com/t/creation-of-dict-on-the-fly/17116 "2014-12-20T05:16:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rick\_Kasten](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@Rick\_Kasten](https://forum.ansible.com/u/Rick_Kasten)\
**Post date:** [December 20, 2014, 5:16am UTC](https://forum.ansible.com/t/creation-of-dict-on-the-fly/17116/1 "2014-12-20T05:16:24Z")

</div>

I have to create a haproxy config file with data from an unknown number of backend servers. The number is unknown, because based on the environment I need to run the playbook in, the number of backend servers could be 1 to several. My backend servers in haproxy.cfg need to defined something like this:

backend backend\_nodes  
description Web Servers  
balance roundrobin  
server web01 10.0.2.1:8080 check  
server web02 10.0.2.3:8080 check  
server web03 10.0.2.3:8080 check

So on any given run of the playbook, I need to define the dict ‘backend\_servers’ for X items (to generate the server lines above):

- name: \<server\_name\>  
ip: \<server\_IP\>  
port: \<http\_port\>  
params: 

With that block repeated for every server in groups[‘web\_servers’]. I tried using set\_facts, but that can’t define dict types. I have to do this as a dict, because different inventories will have different numbers of hosts in groups[‘web\_servers’], and I can’t hard-code this into my playbook or template, because the playbook will be used to configure different haproxy roles with different parameters, so this role needs to remain generic.

---

<div class="post-metadata">

**Author:** ![Timothy\_Gerla](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@Timothy\_Gerla](https://forum.ansible.com/u/Timothy_Gerla)\
**Post date:** [December 20, 2014, 5:34am UTC](https://forum.ansible.com/t/creation-of-dict-on-the-fly/17116/2 "2014-12-20T05:34:14Z")

</div>

I wonder if there might be a simpler way to approach this, but you could use a trick I have used a couple of times and use a template to generate a yaml data structure. Something like:

backend\_servers.j2:

---

<div class="post-metadata">

**Author:** ![Rick\_Kasten](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@Rick\_Kasten](https://forum.ansible.com/u/Rick_Kasten)\
**Post date:** [December 20, 2014, 8:29pm UTC](https://forum.ansible.com/t/creation-of-dict-on-the-fly/17116/3 "2014-12-20T20:29:28Z")

</div>

I like your yaml template idea, but I did end up pushing the logic off into my backend.cfg.j2 template:

{% for server in groups[‘web\_servers’] %}  
server {{ server }} {{ hostvars[server].ansible\_fqdn }}:{{ hostvars[server].http\_port }} params  
{% endfor %}

This is based off my current model where role ‘haproxy/common’ handles the global and default settings and role ‘haproxy/web\_servers’ handles backend, frontend and listen settings. This should suffice going forward as long as other uses of haproxy get their own ‘haproxy/other\_backend’ roles.
