Search code examples
pythonjinja2salt-project

Jinja variables in map.jinja


I'm trying to set a dynamic variable in map.jinja but it is not setting correctly.

I have a software version I want to be able to set through a grain and pick it up in the map to dynamically select the directories. I'm using the keyword version like below.

map.jinja

{% set version = grains.get('version', '') %}
{% set myapp = salt['grains.filter_by']({
    'RedHat': {
        'sudoers': {
            'name': '/etc/sudoers',
            'text':['appuser ALL=(root) NOPASSWD: /bin/sh /usr/opt/HTTP{{ version }}/bin/apachectl'],
        },
    },
}, merge=salt['pillar.get']('myapp:lookup')) %}

And the result is just the jinja template tag included not rendered to the version number...

salt '10.0.1.15' state.show_sls myapp

/etc/sudoers:
    ----------
    __env__:
        base
    __sls__:
        myapp.accounts
    file:
        |_
            ----------
            text:
                |_
                    ----------
                    appuser ALL=(root) NOPASSWD: /bin/sh /usr/opt/HTTP{{ version }}/bin/apachectl

Notice no change to the line.... its not being rendered

My import line:

{% from "myapp/map.jinja" import myapp %}
/etc/sudoers:
    file.append:
        - text:
            {% for item in myapp.sudoers.text -%}
            - {{ item }}
            {% endfor %}

Solution

  • Turns out I needed to quote {{ item }} in the state file

    {% from "myapp/map.jinja" import myapp %} /etc/sudoers: file.append: - text: {% for item in myapp.sudoers.text -%} - '{{ item }}' {% endfor %}