Last active
December 22, 2015 14:58
-
-
Save drags/6488809 to your computer and use it in GitHub Desktop.
Salt Mining and exporting
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 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 |
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
/tmp/collect_file_test: | |
file.managed: | |
- source: salt://haproxy/output.txt | |
- template: jinja |
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
{% 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