Skip to content

Instantly share code, notes, and snippets.

@cloudnull
Created October 28, 2015 00:24
Show Gist options
  • Save cloudnull/82227ad73629e6744279 to your computer and use it in GitHub Desktop.
Save cloudnull/82227ad73629e6744279 to your computer and use it in GitHub Desktop.
diff --git a/playbooks/plugins/filters/osa-filters.py b/playbooks/plugins/filters/osa-filters.py
index 61ca112..f18f873 100644
--- a/playbooks/plugins/filters/osa-filters.py
+++ b/playbooks/plugins/filters/osa-filters.py
@@ -187,6 +187,34 @@ def git_link_parse_name(repo):
return git_link_parse(repo)['name']
+def format_haproxy_servers(servers_group, default_port, params,
+ connection='ansible_ssh_host'):
+ """Return a list of dicts from from groups.
+
+ :param servers_group: Ansible group of names of servers.
+ :type servers_group: ``list``
+ :param default_port: port to blance traffic on.
+ :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``
+ """
+ return_list = list()
+ for server in servers_group:
+ entry = {
+ 'name': server,
+ 'ip': '{{ hostvars[%s]["%s"] }}:{{ %s }}' % (
+ server, connection, default_port
+ ),
+ 'params': params
+ }
+ return_list.append(entry)
+ else:
+ return return_list
+
+
class FilterModule(object):
"""Ansible jinja2 filters."""
@@ -202,5 +230,6 @@ class FilterModule(object):
'splitlines': splitlines,
'filtered_list': filtered_list,
'git_link_parse': git_link_parse,
- 'git_link_parse_name': git_link_parse_name
+ 'git_link_parse_name': git_link_parse_name,
+ 'format_haproxy_servers': format_haproxy_servers
}
@cloudnull
Copy link
Author

Usage

default_port: 1234
params: ['params', 'of', 'things']
connection: 'ansible_ssh_host'
servers: '{{ groups['group_of_servers'] | format_haproxy_servers(default_port, params, connection) }}

This will return a list of servers like this

... [{'name': name, 'ip': ip_address, 'params': ['params', 'of', 'things']}]

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