11

How do I make minions use a particular SaltStack environment?

The GitFS backend tutorial states that branches will be mapped to environments using the branch name as identifier. How do I expose these environments to my minions?

As an example, let's say I have a branch named "pippy". I'd like to deploy that branch to particular minions.

It seems like I can set the environment via the minion's configuration file. Are there other ways that can be done from the salt master?

Kyle Kelley
  • 13,804
  • 8
  • 49
  • 78
  • 2
    I found myself here trying to figure out how to use environments to version and promote state configs up through dev/test/prod. I'm not sure this is the use case for this, since I've run in to some issues. I posted more at length about it in the mailing list: https://groups.google.com/forum/#!topic/salt-users/4P9YpaFdRTQ – thaddeusmt Jun 19 '14 at 18:46

1 Answers1

18

The key here is that the top.sls file is cross-environment. Before we jump into that, it's important to note that while most branches will be mapped to environments of the same name, the exception is that the master branch will be mapped to the base environment.

Anyway, on to top.sls. In top.sls you define your environments, which minions are members of that environment, and which statefiles will be run from that environment for a state.highstate.

base:
  '*':
    - basestate
dev:
  'webserver*dev*':
    - webserver
  'db*dev*':
    - db
qa:
  'webserver*qa*':
    - webserver
  'db*qa*':
    - db
pippy:
  'webserver*pippy*':
    - webserver
  'db*pippy*':
    - db

So, all minions will run the basestate.sls file from the base environment. Only the targeted minions will run the states from each of the other environments.

There is much more information in the topfile documentation.

Defining the environment option in the minion config just isolates a minion to a specific environment. It's much more flexible and powerful to define your environments from your topfile.

Colton Myers
  • 1,321
  • 9
  • 12
  • 1
    I see now. I kept skipping the topfile section thinking that my `top.sls` would be in the git repo, and such wasn't the right option for pushing across branches (and changes across top.sls). – Kyle Kelley Oct 11 '13 at 18:31
  • 1
    What happens if the top.sls file differs between branches? – Kyle Kelley Oct 11 '13 at 18:59
  • 1
    There's a note about halfway down in that documentation, but basically the top file from each environment will be combined into a single top data structure. The base environment will win in conflicts. Generally, it's easiest to just keep the top file in the base (master) environment. – Colton Myers Oct 13 '13 at 00:18
  • @Kyle: The base environment's top file is processed first. Any environment which is defined in the base top.sls as well as another environment's top file, will use the instance of the environment configured in base and ignore all other instances. In other words, the base top file is authoritative when defining environments. source: http://docs.saltstack.com/en/latest/ref/states/top.html 22.26.18.3, section 1) – Marek Bettman Nov 12 '14 at 11:36
  • 1
    "Defining the environment option in the minion config..." << Was looking EVERYWHERE for this bit of info. Salt docs thoroughly cover environment setups, but never mentions how you actually PICK one of them. The minion file has an `environment` variable; set that to match the proper environment and you're good to go. I know, I shoulda read the minion commentary closer. ;) – Frank Koehl Jan 14 '15 at 15:38