Search code examples
salt-project

SaltStack - using mine in pillar to dynamically build list of host names based on grain match


I'm having trouble using the salt mine in a pillar to dynamically create a list of hosts based on a grain value match. I don't get any error, I get no output for all hosts. Actually, I can't get any output for mine from the pillar at all even when using the example from the salt docs. I know it isn't an issue with my top file, because I can access all of the other pillar values. My test minion's mine.interval is set to 5. I've refreshed pillar data, and ran mine.update.

Here's an example of my pillar:

mine_functions:
  network.ip_addrs: []
  grains.item:
    - host
    - role

My template file that access the mine functions:

#I know this is writing the same list for each match, I'm just doing this for testing and I'll concat the results into a string when I know it works:
    {% for host in salt['mine.get']('roles:web', 'grains.items:host', expr_form='grain') | dictsort() %}
        serverList= {{ host }}
    {% endfor %}

Output from CLI:

salt "server.domain.com" mine.get "*" "*"
server.domain.com:
    ----------

How do I get this to work? I get no errors, no output, it just runs smoothly, but nothing is written in the file and I get nothing from the command line. My goal here is to be able to dynamically build a list of servers that match a specific grain to set a configuration value in a config template. Am I down the wrong path here, is there a better way?


Solution

  • @Utah_Dave, thanks so much for the help both here and in IRC.

    Posting this as an answer so anyone else searching for this gets a good example...

    pillar:

    mine_functions:
      grains.items: []
    

    template file:

      {% set ft_hosts = [] %}
      {% for grain_vals in salt['mine.get']('role:ps:ft:True', 'grains.items', expr_form='grain').items() %}
        {% do ft_hosts.append(grain_vals[1]['host']) %}
      {% endfor %}
    
      ft.ps.server.hosts={{ ft_hosts|join('|') }}