diff --git a/README.md b/README.md index a223cec3..ece1d8d6 100644 --- a/README.md +++ b/README.md @@ -379,6 +379,10 @@ Set up (the latest version of) [HAProxy](http://www.haproxy.org/) in Ubuntu syst * `haproxy_backend.{n}.server_template.fqdn`: [required]: A FQDN for all the servers this template initializes * `haproxy_backend.{n}.server_template.port`: [optional]: Port specification * `haproxy_backend.{n}.server_template.{n}.param`: [optional]: A list of parameters for this server template +* `haproxy_backend.{n}.server_dynamic`: [optional]: Dynamic backend server declaration +* `haproxy_backend.{n}.server_dynamic.{n}.group`: [required]: An ansible group containing hosts to be added as backend servers. Uses `inventory_hostname` for name and either `ansible_host` (if defined) or `inventory_hostname` for the listen address of each host. +* `haproxy_backend.{n}.server_dynamic.{n}.listen_port`: [optional]: The port to use with each dynamic backend (translates to `listen :`). +* `haproxy_backend.{n}.server_dynamic.{n}.param`: [optional]: A list of parameters to apply on each backend server. * `haproxy_backend.{n}.retry_on`: [optional, default `[]`]: Specify when to attempt to automatically retry a failed request. Provide a list of keywords or HTTP status codes, each representing a type of failure event on which an attempt to retry the request is desired. For details, see HAProxy documentation. * `haproxy_backend.{n}.retries`: [optional]: Number of retries to perform on a server after a connection failure diff --git a/templates/etc/haproxy/backend.cfg.j2 b/templates/etc/haproxy/backend.cfg.j2 index e0f42324..3f059bbb 100644 --- a/templates/etc/haproxy/backend.cfg.j2 +++ b/templates/etc/haproxy/backend.cfg.j2 @@ -107,6 +107,13 @@ backend {{ backend.name }} {% for server in backend.server | default([]) %} server {{ server.name }} {{ server.listen }}{% for param in server.param | default([]) %} {{ param }}{% endfor %} +{% endfor %} +{% for server_dynamic in backend.server_dynamic | default([]) %} +{% for server_name in groups[server_dynamic.group] %} +{% set server = hostvars[server_name] %} + server {{ server.inventory_hostname }} {% if server.ansible_host is defined %}{{ server.ansible_host }}{% else %}{{ server_name }}{% endif %}{% if server_dynamic.listen_port is defined %}:{{ server_dynamic.listen_port }}{% endif %}{% for param in server_dynamic.param | default([]) %} {{ param }}{% endfor %} + +{% endfor %} {% endfor %} {% if backend.server_template is defined %} server-template {{ backend.server_template.name }} {{ backend.server_template.num}} {{ backend.server_template.fqdn }}{% if backend.server_template.port is defined %}:{{ backend.server_template.port }}{% endif %} {% for param in backend.server_template.param | default([]) %} {{ param }}{% endfor %}