Environment matching in saltstack


While I was looking how to match an environment in salt, I could find a way.

{{ env }} is not a grain, so is hard to manipulate, then I came out with this:

For each environment, I define the key pillar_env on it's pillar, then reference it when I want to limit the run to one environment or when want to use a dynamic path to retrieve files.

Examples:

I want to know the version of neutron-common on regionOne environment.

# salt -I 'pillar_env:regionOne' cmd.run 'dpkg -l |grep neutron-common'

I have many /etc/hosts, one for each environment

/etc/hosts:
  file.managed:
    - source: salt://hosts/hosts_{{pillar['pillar_env']}}
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - defaults:
      hostname: {{ salt['grains.get']('host') }}
      fqdn: {{ salt['grains.get']('fqdn') }}

Is not the best thing in the world, but it works :)