Python SaltStack:SLS文件中数据的属性(计算值)?
我们在salt管理的仆从上运行了几个Python虚拟环境 系统的名称由以下架构生成:Python SaltStack:SLS文件中数据的属性(计算值)?,python,salt-stack,Python,Salt Stack,我们在salt管理的仆从上运行了几个Python虚拟环境 系统的名称由以下架构生成: project_customer_stage 例如: supercms_favoritcustomer_p 支柱数据: systems: - customer: favoritcustomer project: supercms stage: p - customer: favoritcustomer project: supercms stage: q {% f
project_customer_stage
例如:
supercms_favoritcustomer_p
支柱数据:
systems:
- customer: favoritcustomer
project: supercms
stage: p
- customer: favoritcustomer
project: supercms
stage: q
{% for system in salt['pillar.get']('systems') %}
{{ system.home }}:
file.directory
{% endfor %}
对于每个virtualenv,我们都有一个linux用户。到目前为止,我们计算“home”这样的值:
{% for system in pillar.systems %}
{% set system_name = system.project + '_' + system.customer + '_' + system.stage %}
{% set system_home = '/home/' + system_name %}
...
但这是多余的
我们如何避免复制+粘贴{%set system\u home=…%}
我喜欢面向对象编程的工作方式:
- 您可以为主目录定义一个属性
- 如果在特殊情况下需要不同的主目录,那么可以对基类进行子类化,并覆盖基类的工作方式
在盐中你有YAML和模板。。。都是好东西。但在我的例子中,OOP会很好。您还可以动态生成支柱数据。考虑下面的示例:一个支柱文件:
{% import_yaml "systems.yml" as systems %}
systems:
{% for system in systems %}
{% set name = system['name'] | default(system.project + '_' + system.customer + '_' + system.stage) %}
{% set home = system['home'] | default('/home/' + name) %}
- name: {{ name }}
customer: {{ system['customer'] }}
project: {{ system['project'] }}
stage: {{ system['stage'] }}
home: {{ home }}
{% endfor %}
#!py
import yaml
def run():
systems = []
with open('systems.yml', 'r') as f:
data = yaml.safe_load(f)
for system in data:
if not 'name' in system:
system['name'] = "%s_%s_%s" % (system['project'], system['customer'], system['stage'])
if not 'home' in system:
system['home'] = "/home/%s" % name
systems.append(system)
return {"systems": systems}
此支柱定义从systems.yml
文件加载YAML数据,Salt将在您的支柱根目录中查找该文件。此文件可能如下所示(与您最初的示例非常相似):
<强>另外<强>,如我所认为的,这些JIGA重SLS文件有点难以阅读,您可以考虑切换到您的支柱文件:
{% import_yaml "systems.yml" as systems %}
systems:
{% for system in systems %}
{% set name = system['name'] | default(system.project + '_' + system.customer + '_' + system.stage) %}
{% set home = system['home'] | default('/home/' + name) %}
- name: {{ name }}
customer: {{ system['customer'] }}
project: {{ system['project'] }}
stage: {{ system['stage'] }}
home: {{ home }}
{% endfor %}
#!py
import yaml
def run():
systems = []
with open('systems.yml', 'r') as f:
data = yaml.safe_load(f)
for system in data:
if not 'name' in system:
system['name'] = "%s_%s_%s" % (system['project'], system['customer'], system['stage'])
if not 'home' in system:
system['home'] = "/home/%s" % name
systems.append(system)
return {"systems": systems}
我同意你的看法:“这些Jinja重SLS文件有点难读”。我想我会使用python渲染器。非常感谢。