Skip to content

Instantly share code, notes, and snippets.

@erchn
Created November 24, 2013 16:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erchn/7629017 to your computer and use it in GitHub Desktop.
Save erchn/7629017 to your computer and use it in GitHub Desktop.
auto-assign states to a minion based on cluster name if they exist on the master. The concept shamelessly copied from another community member.
{# ######################################################################################### #}
{# ######################################################################################### #}
{# this block of code auto applies salt states to a given minion if they exist on the master #}
{% set id = grains['id'] %}
{# web01 host id = 'web' cluster id, allow a custom grain or pillar named 'cluster' to override #}
{% set cluster = salt['config.get']('cluster', id.split('.')[0].rstrip('0123456789')) %}
{# all available states on the master #}
{% set states = salt['cp.list_master']() %}
{% set toApply = [] %}
{# clusters/web/init.sls #}
{% if 'clusters/{0}/init.sls'.format(cluster) in states %}
{% if toApply.append('clusters.{0}'.format(cluster)) %}
{% endif %}
{% endif %}
{# clusters/web/web01.sls #}
{% if 'clusters/{0}/{1}.sls'.format(cluster, id) in states %}
{% if toApply.append('clusters.{0}.{1}'.format(cluster, id)) %}
{% endif %}
{% endif %}
{# if list is not empty #}
{% if toApply %}
'{{ id }}':
{% for a in toApply %}
- {{ a }}
{% endfor %}
{%- endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment