SaltStack:如何在上下文中重复其他状态? [英] SaltStack: how do I repeat other states with context?

查看:99
本文介绍了SaltStack:如何在上下文中重复其他状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为API服务创建了一个复杂的状态,它涉及git checkouts,python venv,uwsgi,nginx等。它工作正常。

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

现在,我想将其转换为模板,并每分钟执行几次,并使用从支柱提供的变量-例如。

Now I would like to turn it into a template and execute it several times per minion, with variables supplied from pillar - i.e something like.

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

其中apiserver_template将与提供的上下文一起使用,apiserver.config具有每个API的所有配置数据实例。我知道语法是错误的,但希望我能传达这个想法-理想情况下,类似于执行提供局部变量的ruby局部函数。

where apiserver_template will work with context supplied to it, with apiserver.config having all config data for each API instance. I know syntax is wrong but hopefully I am communicating the idea - ideally, something like executing ruby partials with supplying local variables.

在盐田地区如何正确完成?

How is it done properly in saltland?

推荐答案

在我看来,Jinja Macro是您要用于此的东西。您可以在此处找到有关用法的更多信息: https: //docs.saltstack.com/en/2015.8/topics/development/conventions/formulas.html#jinja-macros

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 %}

假设您有一个支柱apiserver,其中每个api服务器都有git_repo,python_venv_path和python_venv_requirements值,则可以使用如下宏:

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 %}

如果需要-您还可以将宏放在单独的状态文件中,然后将marco作为常规盐资源导入。

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

请也不是,我使用了salt.pillar.get('apiservers',[])代替了column.apiservers。这是从支柱获取数据的更安全的方法。如果由于某种原因支柱不可用-后面的代码将导致空dict而不是在第一种情况下失败。

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.

这篇关于SaltStack:如何在上下文中重复其他状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆