List salt minions in template

Update: I revisited this topic recently and since there's the Salt Mine this can be done in better ways, check for different approaches.

This seems to be a common task with saltstack but I couldn't find a really good answer yet: How does one minion get a targeted list of minions for a template? In my case I wanted to populate the backup server's config file with all minions with the backupclient role.

I came up with the following solution that uses peer communication and a custom module. This is my module:

# states/_modules/

def get_clients():
    Request backuppc.is_client() from all minions
    return __salt__['publish.publish']('*', 'backuppc.is_client')

def is_client():
    Check if this minion has the backupclient role grain
    ret = False
    if 'roles' in __grains__:
        if 'backupclient' in __grains__['roles']:
            ret = True
    return ret

The backup server needs access to the is_client() function to be able to build the hosts config file, so the minions must publish it. The peer configuration in the master config file has to contain something like:

    - backuppc.is_client

Ideally the clients would publish this info only to the master, but this doesn't seem possible with this method.

The configuration template with the hosts list contains a loop like this:

{% for host, enabled in salt['backuppc.get_clients']().iteritems() %}
    {%- if enabled %}
        {{- host }} foo
    {%- endif %}
{% endfor %}


I'm not really happy with this solution. The peer communication seems superfluous, but I couldn't find a better way. The master can easily target minions by grains, but I couldn't find a way to get this info into a minion's template.

Salt 0.15 introduced the mine module which could be used to make this solution more scalable.

Published on June 6, 2013 by Nicolas and tagged SaltStack. You can follow the discussion with the comment feed for this post. Feeling generous? Donate!


Start a new thread

Cancel reply
Markdown. Syntax highlighting with <code lang="php"><?php echo "Hello, world!"; ?></code> etc.