SaltStack: how do I repeat other states with context?

我怕爱的太早我们不能终老 提交于 2019-12-06 05:28:12

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

In short what you will have in your case may look like:

{% 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 a pillar apiservers where each api server has git_repo, python_venv_path and python_venv_requirements values, you can use the 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 a macro in a separate state file and then import a marco as a regular salt resource.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!