使用包含在盐堆栈中的传递变量 [英] Passing variables with include in salt-stack
问题描述
我有几个几乎相同的状态.他们都部署项目,创建 virtualenv 并配置主管.区别仅在于 repo、项目名称和一些附加操作.
I have several states that are almost the same. All of them deploy project, create virtualenv and configure supervisor. Difference is only in repo, project name and some additional actions.
很多代码是重复的.是否可以将相同的部分放入文件并包含其他变量?
A lot of code is duplicated. Is it possible to put the same parts into file and include it with additional variables?
在 Ansible 中,可以这样做:
In Ansible it can be done this way:
tasks:
- include: wordpress.yml
vars:
wp_user: timmy
ssh_keys:
- keys/one.txt
- keys/two.txt
推荐答案
这个问题看起来类似于 这个
This question looks similar to this one
如果我正确理解了您的问题 - 我相信实现您想要的最佳方法是使用 盐宏.
If I understood your question correctly - I believe the best way to achieve what you want is to use Salt Macros.
有了这个,你的大部分状态将转到宏,占位符是参数,例如:
With this most of your state will go to macros with placeholders being parameters like:
# lib.sls
{% macro create_user(user, password) %}
{{user}}:
user.present:
- home: /home/{{user}}
- password: {{password}}
{% endmacro %}
然后您的状态将如下所示:
Then your state will look like:
# john.sls
{% from 'lib.sls' import create_user with context %}
{{ create_user('john', '<password hash>') }}
和:
# jane.sls
{% from 'lib.sls' import create_user with context %}
{{ create_user('john', '<password hash>') }}
这篇关于使用包含在盐堆栈中的传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!