salt mine and environments

301 views
Skip to first unread message

Ross Black

unread,
Oct 9, 2015, 6:17:28 AM10/9/15
to Salt-users
Hi,

I am using SaltStack with multiple environment definitions, and wanted to know if/how mine functions interact with environments.

I am trying to use a single master that works with multiple environments.  I wanted the machines to be split into different groups based on their defined enviroment (currently just using lists of the machine names).
I have the basic setup working ok.

My setup is:
/etc/salt/master
file_roots:
 
base:
   
- /vagrant/env/base/state
  demo
:
   
- /vagrant/env/demo/state
  prod
:
   
- /vagrant/env/prod/state

pillar_roots
:
 
base:
   
- /vagrant/env/base/pillar
  demo
:
   
- /vagrant/env/demo/pillar
  prod
:
   
- /vagrant/env/prod/pillar


Pillar data defines configuration that is specific to each environment.
/vagrant/env/base/pillar/top.sls
base:
 
'*':
   
- match: glob
   
- config
demo
:
 
'slave1':
   
- match: list
   
- config
prod
:
 
'slave2,slave3':
   
- match: list
   
- config



This seems to be doing what I expect and allows different config to be defined per environment.
Where I have the problem is when a state file uses mine functions to query for machines.  I was hoping that the machines would be divided by environment.
With a state file that queries a mine, the mine returns all machines, not just those matching the environment.

example.sls
{% set p  = salt['pillar.get']('stuff', {}) %}
{%- set value = p.get('value', 'default-value') %}
{%- set force_mine_update = salt['mine.send']('network.get_hostname') %}
{%- set host_dict = salt['mine.get']('*', 'network.get_hostname', 'glob') %}
{%- set host_list = host_dict.values() %}
{%- set hosts = host_list|join(',') %}

/tmp/example-debug.txt:
  file
.managed:
   
- contents: |
        value  
=> {{ value }}
        hosts
=> {{ hosts }}

Running
sudo salt slave1 state.sls example saltenv=demo


gives output in /tmp/example-debug.txt
value => demo-value
hosts
=> slave1,slave3,slave2
so that all machines are listed, not just those from the demo environment.

(
The above is just a simple example to demonstrate the problem.  I am actually using the formula from https://github.com/saltstack-formulas/zookeeper-formula)

Am I doing something wrong, or is this expected behaviour?
Is there a way to ensure that only the machines from the relevant environment are returned from mine.get?


Thanks,
Ross


Colton Myers

unread,
Oct 16, 2015, 4:56:35 PM10/16/15
to salt-...@googlegroups.com
Unfortunately, mine data doesn't divide on environments. You'll have to set up your mine query to target only a certain environment. (If you put the environment name in the minion's grains, that's a fairly straightforward way to do this. You then just target by that grain)

--
Colton Myers
Core Engineer, SaltStack
@basepi on Twitter/Github/IRC
> --
> 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.
> For more options, visit https://groups.google.com/d/optout.

signature.asc

Ross Black

unread,
Oct 19, 2015, 6:51:16 PM10/19/15
to Salt-users
Thanks, good to know that I was not missing something.

Just in case anyone else interested, I ended up not using salt environments at all.
I add an 'environment' grain to each minion, and then querying that + role in the mine function.
Instead of salt environments, I just added node groups based on the 'environment' grain.

pillar/top.sls
base:
 
'*':
   
- base/config
 
'environment:demo':
   
- match: grain
   
- demo/config
 
'environment:pilot':
   
- match: grain
   
- pilot/config

sample.sls
{%- set environment = salt['grains.get']('environment', None) %}
{%- if not environment -%}
 
{%- do salt.test.exception("Machine must specify environment grain") %}
{%- endif -%}

{%- set hosts_target = 'G@roles:sample and G@environment:' + environment %}
{%- set targeting_method = 'compound' %}

{%- set force_mine_update = salt['mine.send']('network.get_hostname') %}
{%- set host_dict = salt['mine.get'](hosts_target, 'network.get_hostname', targeting_method) %}

{%- set host_list = host_dict.values() %}
{%- set hosts = host_list|join(',') %}

Thanks,
Ross

Dani C

unread,
Oct 20, 2015, 5:19:08 AM10/20/15
to Salt-users
Nice one Ross, thanks for sharing!
Reply all
Reply to author
Forward
0 new messages