Help using salt mine

498 views
Skip to first unread message

Karl Schafer

unread,
Jan 26, 2017, 1:03:23 PM1/26/17
to Salt-users
Hi there! I'm trying to use salt mine to handle some complex configuration problems.

I'd like to be able to return a list of hostnames that match a given salt grain.

Minion configs have the following appended for mine_functions:

mine_functions:
  grains.items: []

I can't seem to get any output from the command line however. How do I validate that salt-mine is working?

The mine.update command returns "True" for the hosts I have the mine_functions configured on:

salt '*' mine.update

However, none of the command line examples provided in the mine documentation provide any results. The command below returns no results.

salt '*' mine.get '*' network.interfaces

What am I missing? Is there a better document available on how to use salt mine?

Thanks,

Karl

Seth House

unread,
Jan 26, 2017, 1:32:02 PM1/26/17
to salt users list
Try setting the `mine_interval` setting to something very short or
calling `mine.update` or `mine.send` to manually seed the cache.
> --
> You received this message because you are subscribed to the Google Groups
> "Salt-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to salt-users+...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/salt-users/00ecd994-3e61-4db9-ad31-fccd86242dab%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Karl Schafer

unread,
Jan 26, 2017, 2:02:59 PM1/26/17
to Salt-users, se...@eseth.com
Thanks - alt "*' mine.update returns True for the hosts that have mine enabled. Does that mean its working?

The mine.get commands still return nothing.

Karl

Thomas Phipps

unread,
Jan 26, 2017, 2:58:51 PM1/26/17
to Salt-users, se...@eseth.com
looking at what you put in the email. part of the problem could be you don't have the commands you are running in the mine. 

in your first part you only put grains.items. but when you are calling the mine you are searching for network.interfaces. your salt call with that setup should be salt '*' mine.get grains.items

also just in case before your mine.update you should run a saltutil.pillar_refresh. just to make sure the minions have fresh pillar update before hand. 


For more options, visit https://groups.google.com/d/optout.
--
Thomas Phipps
Linux/Openstack/Logstash

Karl Schafer

unread,
Jan 26, 2017, 3:20:10 PM1/26/17
to Salt-users, se...@eseth.com
Thanks, part of the problem I guess is that I'm floundering and getting confused. The documentation on this functionality has been confusing at best.

I can successfully get all the grains using this command:

salt '*' mine.get '*' grains.items

What do I need to do to be able to get the value of a grain from minions that match another grain?

The fqdn of all minions that match have "webserver" for their roles grain.

Thanks,

Karl

Karl Schafer

unread,
Jan 27, 2017, 1:53:24 PM1/27/17
to Salt-users
Well just wanted to update that I was able to figure this out. Here's the commands and syntax I used.

minion config additions - I restarted the minions after setting this. Not sure if thats required.

# salt-mine stuff
mine_functions:
  grains.item:
    - fqdn
    - ipv4

From the master(s), do a mine.update:

salt '*' mine.update)

Command line tests:

(Run this from a minion, this should return the grains set in the minion config for every host matghing the value grain:value parameters; os:Ubuntu for example. Here I want to return the hosts that are tagged with a particular application environment.)

salt-call mine.get 'app_env:someapp-dev-01' grains.item grain

If that works you can use mine.get in a jinja template for some advanced configuration:

{% set my_hosts = [] -%}
{% for host, data in salt['mine.get']('app_env:someapp-dev-01', 'grains.item', expr_form = 'grain').items() -%}
  {% do my_hosts.append(data["fqdn"]) -%}
{% endfor -%}

Another example, this time using the host's own app_env to figure out which its primary app/web hosts are

{% set my_hosts = [] -%}
{% set app_primary = [] -%}
{% set web_primary = [] -%}
{% set app_env = grains['app_env'] -%}

{% for host, data in salt['mine.get']('app_env:%s'|format(app_env), 'grains.item', expr_form = 'grain').items() -%}
  {% do my_hosts.append(data["fqdn"]) -%}
{% endfor -%}
{# assign variables based on hosts in the salt mine data -#}
{% for host in my_hosts -%}
  {% if ( "app-" in host and "-01" in host ) -%}
    {% do app_primary.append(host) -%}
  {% endif -%}
  {% if ( "web-" in host and "-01" in host ) -%}
    {% do web_primary.append(host) -%}
  {% endif -%}
{% endfor -%}

My Hosts: {{ my_hosts }}
Primary Appserver: {{ app_primary[0] }}
Primary Webserver: {{ web_primary[0] }}




Reply all
Reply to author
Forward
0 new messages