Passing variables with include in salt-stack

ぐ巨炮叔叔 提交于 2019-12-22 10:22:09

问题


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?

In Ansible it can be done this way:

tasks:
  - include: wordpress.yml
    vars:
        wp_user: timmy
        ssh_keys:
          - keys/one.txt
          - keys/two.txt

回答1:


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>') }}

and:

# jane.sls
{% from 'lib.sls' import create_user with context %}
{{ create_user('john', '<password hash>') }}



回答2:


As I found out there is another way to archive it without messing with templates (more Ansible way). Create an abstract state "python-project". Create concrete roles then and provide different pillars to these roles:

salt/top.sls:

base:
  'roles:python-project-1':
    - match: grain
    - python-project

  'roles:python-project-2':
    - match: grain
    - python-project

pillar/top.sls:

base:
  'roles:python-project-1':
    - match: grain
    - common-pillars
    - pillars-for-the-first

  'roles:python-project-2':
    - match: grain
    - common-pillars
    - pillars-for-the-second

Structure:

pillar/top.sls
pillar/common-pillars/init.sls
pillar/pillars-for-the-first/init.sls
pillar/pillars-for-the-second/init.sls
salt/top.sls
salt/python-project/init.sls


来源:https://stackoverflow.com/questions/38904308/passing-variables-with-include-in-salt-stack

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