Search code examples
salt-project

Passing variables with include in salt-stack


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

Solution

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