Skip to content

Instantly share code, notes, and snippets.

@drags
Last active December 22, 2015 14:58
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 drags/6488809 to your computer and use it in GitHub Desktop.
Save drags/6488809 to your computer and use it in GitHub Desktop.
Salt Mining and exporting
## This example module allows for generating and collecting haproxy node lines
## (Be sure to sync_modules when installing or changing modules)
##
## To add a server to a pool use haproxy.setup_node:
#$ salt '<minion target>' haproxy.setup_node <pool_name> [port]
## This sets a targeting grain and exports the haproxy line
##
## To collect the host lines on the loadbalancer use haproxy.get_servers
#$ salt '<haproxy host>' haproxy.get_output <pool_name>
## This module is best used when called by a file.managed resource
## managing /etc/haproxy/haproxy.cfg
def setup_node(proxy_pool, port=8080):
'''Set identifying grain and mine.send the HAproxy server line'''
__salt__['grains.setval']('haproxy_%s' % proxy_pool, 'enabled')
__salt__['mine.send']('haproxy.server_line')
line = server_line(port=port)
return line
def server_line(port=8080):
eth0_ip = __salt__['network.ip_addrs']('eth0')[0]
hostname = __salt__['cmd.run']('hostname')
line = 'server %s %s:%d cookie %s check' % (hostname, eth0_ip, port, hostname)
return line
def get_servers(proxy_pool):
'''Get concatenated mine output from minions with matching grains.'''
# Get previously 'mine.send'ed haproxy_config_line from target minions
out = __salt__['mine.get']('haproxy_%s:enabled' % proxy_pool, 'haproxy.server_line', 'grain')
ret = []
for host in out:
ret.append(out[host])
return ret
/tmp/collect_file_test:
file.managed:
- source: salt://haproxy/output.txt
- template: jinja
{% for line in salt['haproxy.get_output']('haproxy_stats:enabled','haproxy.server_line') -%}
{{ line.strip() }}
{% endfor -%}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment