Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variables with include in salt-stack

Tags:

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
like image 636
Raz Avatar asked Aug 11 '16 19:08

Raz


1 Answers

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>') }}
like image 160
alexK Avatar answered Sep 23 '22 04:09

alexK