SaltStack: how can I repeat other states with context?

I created a complex state for the API service, it includes git checkouts, python venv, uwsgi, nginx, etc. etc. It works great.

Now I would like to turn it into a template and execute it several times in a minion, with variables set from a column - in other words.

{% for apiserver in pillar.apiservers %} include apiserver_template.sls, locals: apiserver.config {% endfor %} 

where apiserver_template will work with the context provided by it, with apiserver.config having all the configuration data for each instance of the API. I know that the syntax is incorrect, but I hope I communicate with the idea - ideally, something like executing ruby ​​scores with the delivery of local variables.

How is this done correctly in salt water?

+1
devops configuration-management salt-stack
source share
1 answer

It seems to me that Jinja Macro is what you want to use for this. You can find more usage information here: https://docs.saltstack.com/en/2015.8/topics/development/conventions/formulas.html#jinja-macros

In short, what you have in your case might look like this:

 {% macro api_server(git_repo, python_venv_path, python_venv_requirements) %} {{python_venv_path}}: virtualenv.managed: - system_site_packages: False - requirements: salt://{{python_venv_requirements}} {{git_repo}}: git.latest: - name: {{git_repo}} {% endmacro %} 

Assuming you have column apiservers where each api server has the values ​​git_repo, python_venv_path and python_venv_requirements, you can use a macro like this:

 {% for server in salt.pillar.get('apiservers', []) %} {{ api_server(server['git_repo'], server['python_venv_path'], server['python_venv_requirements']) }} {% endfor %} 

If you want, you can also put the macro in a separate status file, and then import marco as a regular salt resource.

Please also not that I used salt.pillar.get ('apiservers', []) instead of pillar.apiservers. This is a safer way to get data from a column. If for some reason the pillar is unavailable - later code will lead to empty dict instead of failure in the first case.

+2
source share

All Articles