9

What I'd like to do is generate multiple configuration files for each openvpn user. I have the IP address and additional configuration in pillar.

For example:

openvpn:
  - user1:
    ip: 1.2.3.4
    config:
      - line1
      - line2

In the SLS I'd like to do something like:

{% for vpnuser in salt['pillar.get']('openvpn') %}
/etc/openvpn/ccd/{{ vpnuser }}:
  file.managed:
    - template: jinja
    - source: salt://openvpn/ccdtemplate
{% endfor %}

and in the ccdtemplate I'd like to generate the config depending on the user and the config data for this user stored in pillar.

But therefor I would need to know the content of the variable 'vpnuser' from the state.

Is it possible to pass variables from a state to the jinja template?

dawud
  • 14,918
  • 3
  • 41
  • 61
Herrberg
  • 93
  • 1
  • 1
  • 3

1 Answers1

10

You can pass any value you need to the template by using the defaults keyword. In your case, this would be:

{% for vpnuser in salt['pillar.get']('openvpn') %}
/etc/openvpn/ccd/{{ vpnuser }}:
  file.managed:
    - template: jinja
    - source: salt://openvpn/ccdtemplate
    - defaults:
        vpnuser: {{ vpnuser }}
{% endfor %}

This is documented in http://docs.saltstack.com/en/latest/ref/states/all/salt.states.file.html

Christophe Drevet
  • 1,962
  • 2
  • 17
  • 25
  • 2
    You can use the `context` option the same way as `defaults`, which is a little confusing. They try to clarify like so: "The general best practice is to place default values in `defaults`, with conditional overrides going into `context`." – thaddeusmt Dec 23 '15 at 01:08