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?

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

| improve this answer | |
  • 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 1:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.