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.