Skip to content

Instantly share code, notes, and snippets.

@evrardjp

evrardjp/Issue Secret

Created February 12, 2016 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evrardjp/bff952190e5af6593022 to your computer and use it in GitHub Desktop.
Save evrardjp/bff952190e5af6593022 to your computer and use it in GitHub Desktop.
haproxy implementation - generation of "backend servers" from groups of hosts
This is the result of my template receiving the variable haproxy_services:
backend galera-back
server aio1_galera_container-b5f33b58 {# hostvars[aio1_galera_container-b5f33b58]["ansible_ssh_host"] #}:3306 check port 3306 inter 3306 rise 1 fall 1
Which is problematic, I'd like to have the ip instead of a string "{# hostvars[aio1_galera_container-b5f33b58]["ansible_ssh_host"] #}"
It's because I can't use {{ }} inside a filter. So what should I do?
def haproxy_backend_servers(servers_group, default_port, params=False, connection='ansible_ssh_host'):
"""Return a list of dicts of servers from a group (servers_group), and
formatted for haproxy backend usage. Each item of the list is a server,
with its name, ip, port and parameters.
:param servers_group: Ansible group name
:type servers_group: ``list``
:param default_port: port to service is listening on in the backend network
:type default_port: ``int``
:param params: list of params to pass into haproxy.
:type params: ``list``
:param connection: Network connection name.
:type connection: ``str``
:returns: ``list``
"""
if params is False:
params = [ "check port %s" % ( default_port ) , "inter {{ haproxy_default_interval }}" , "rise %s" % ( len(servers_group) ) , "fall %s" % ( len(servers_group) ) ]
return_list = list()
for server in servers_group:
entry = {
'name': server,
'ip': '{{ hostvars[%s]["%s"] }}:%s' % (
server, connection, default_port
),
"""'ip': "%s:%s" % ( hostvars[server][connection] , default_port )
,"""
'params': params
}
return_list.append(entry)
else:
return return_list
galera_params:
- check port 3306
- rise 1
- fall 1
galera_default_port: 3306
haproxy_services:
galera:
frontends:
- name: galera
binds:
- ip: '*:3306'
balance_type: 'tcp'
options:
- tcplog
timeout_client: 5000s
default_backend: galera-back
backends:
- name: galera-back
balance_type: tcp
balance: leastconn
options:
- "mysql-check user monitoring"
timeout_server: 5000s
servers: "{{ groups['galera_all'][0:1] | haproxy_backend_servers(galera_default_port,galera_params) }}"
backups: "{{ groups['galera_all'][1:] | haproxy_backend_servers(galera_default_port,galera_params) }}"
@evrardjp
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment