Alex
Alex

Reputation: 916

How to get a list of all salt minions in a template?

Basically I am creating a Salt state describing Munin server configuration and I need to get a list of all minions known to the master, something like this:

{% for host in pillar['munin_clients'] %}
[{{ host.fqdn }}]
    address {{ host.ip }}
    use_node_name yes
{% endfor %}

The only difference is that I don't want to use pillar for that, I need this list to be populated dynamically. ret.get_minions seems to be relevant but I can't make it work for some reason. What are my options?

Upvotes: 16

Views: 13313

Answers (3)

Alex Zel
Alex Zel

Reputation: 688

Why not use minion.list module?

In our pillar environment we have pillar that are private to each minion (/srv/pillar/hosts/[server_A, server_B, server_C...]/some_pillar.sls).

To make things easier (since some people keep forgetting to add new pillars to the top file) our pillar top file looks like this:

{{ saltenv }}:
  {% for minion in salt['minion.list']()['minions'] -%}
  {{ minion + '*' }}:
    - hosts.{{ minion.split('.')[0] }}
  {% endfor -%}

This way I can get a list of all minions known to the master every time the pillar enviorment is updated.

Upvotes: 4

Utah_Dave
Utah_Dave

Reputation: 4581

Alex's answer is great. The Salt Mine will give you the list of minions that's correct as of the last time the Mine was executed.

If you want live up to the second data you can use the peer interface by using the publish module. Publish module docs are here: http://docs.saltstack.com/ref/modules/all/salt.modules.publish.html#module-salt.modules.publish

{% for host in salt['publish.publish']('*', 'network.ip_addrs', 'eth0') %}
[{{ host.fqdn }}]
    address {{ host.ip }}
    use_node_name yes
{% endfor %}

Make sure to set your master config to allow the minions to execute network.ip_addrs.

EDIT:

To answer a question below you must enable the host to query other minions through the peer publish interface. To allow all minions to query the ip addresses of all other minions, add this to your /etc/salt/master:

peer:                                                                          
  .*:
    - network.ip_addrs

Upvotes: 10

Alex
Alex

Reputation: 916

I managed to achieve this using Salt Mine system (thanks to members of Salt-users Google group):

{% for host, hostinfo in salt['mine.get']('*', 'network.interfaces').items() %}
[{{ host }}]
    address {{ hostinfo['eth0']['inet'][0]['address'] if hostinfo['eth0'].has_key('inet') else hostinfo['br0']['inet'][0]['address'] }}
    use_node_name yes
{% endfor %}

I had to add

mine_functions:
  network.interfaces: []

to the end of /etc/salt/minion on every node to enable Salt Mine.

Upvotes: 15

Related Questions