Search code examples
salt-project

Explain using mine.get inside a salt-state


Can someone give me an idiots explanation of how the following works:

{% for server, addrs in salt['mine.get']('roles:web', 'network.ip_addrs', expr_form='grain') | dictsort() %}
server {{ server }} {{ addrs[0] }}:80 check
{% endfor %}

The pillar file looks like:

mine_functions:
  network.ip_addrs: [eth0]

What does salt assign to the first two values, server and addrs? How would you use multiple mine options in the above? i.e. if your pillar file had an extra mine function like so:

mine_functions:
     network.ip_addrs: [eth0]
      role:
        - mine_function: grains.get
        - role

How could you then access the role function in the first code snippet?

Hopefully this makes sense


Solution

  • Mine

    The mine functions, defined in your pillar, tells the minions to push some information about them to the salt master. Then you can access it from any minion, using mine.get. This is documented in this page.

    mine.get

    First, you can get a look at the mine.get result yourself:

    salt 'minion0' mine.get 'roles:web' 'network.ip_addrs' expr_form='grain'
    minion0:
        ----------
        minion1:
            - ip1
            - ip2
        minion2:
            - ip
    

    So you got a dict, with keys being the minions ids, and values lists of network ip addresses. The dictsort() function sorts the keys before being passed to the variables of the loop.

    You end up with server containing the minion id, and addrs a list of its ip addresses.

    The first argument filters which minions data you want to get, with the third one (expr_form) being the filter type of this filter. You can use glob, grain, compound, basically any salt targeting.

    Now, to access the role mined data, you would just replace network.ip_addrs with role.