Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid repeating myself in Salt states?

We have two different environments, dev and production, managed by a single Salt server. Something like this:

base:
  'dev-*':
    - users-dev
  'prod-*':
    - users-prod

user-dev and users-prod states are pretty much the same, like this:

{% for user, data in pillar['users-dev'].items() %}
{{ user }}-user:
  user.present:
    < ...something... >
{{ user }}_ssh_auth:
  ssh_auth.present:
    < ...something... >
{% endfor %}

We did not want to duplicate the code so our initial idea was to do something like this:

{% users = pillar['users'].items() %}
include:
  - users-common

and then to refer to users in users-common, but this did not work because the proper Jinja syntax was set users = pillar['users'].items() and this was not intended to work across Salt states includes.

So, the question is how to do it properly?

like image 564
Alex Avatar asked Aug 20 '13 22:08

Alex


1 Answers

All jinja is evaluated before any of the states (including the include statements) are evaluated.

However, I would think you would just be able to refer directly to pillar['users'].items() inside of users-common. Is it not allowing you to access pillar from within that state?

like image 56
Colton Myers Avatar answered Oct 30 '22 13:10

Colton Myers