-
-
Save evrardjp/bff952190e5af6593022 to your computer and use it in GitHub Desktop.
haproxy implementation - generation of "backend servers" from groups of hosts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) }}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I didn't paste the template, but it's basically there: https://github.com/evrardjp/ansible-haproxy/blob/master/templates/service.j2
and the task running the template is here: https://github.com/evrardjp/ansible-haproxy/blob/master/tasks/haproxy_post_install.yml#L47-L51