Search code examples
jinja2salt-project

Convert a salt module into jinja


I'm having some troubles to get a simple saltstack module running in a jinja template.

# salt-ssh -i 'myhost' lowpkg.info postfix attr=version --> works fine

I thought it could work this way:

{%- set postfixversion = salt['lowpkg.info']('postfix,attr=version') %}
path to version = /folder/{{ postfixversion }}/subfolder`

After some variation ('postfix),(attr=version') or ('postfix)(attr=version') or ... I always get errors:

Comment: Traceback (most recent call last):
    File "/var/tmp/.root_98377f_salt/py2/salt/utils/templates.py", line 167, in render_tmpl
    File "/var/tmp/.root_98377f_salt/py2/salt/utils/templates.py", line 445, in render_jinja_tmpl
    File "/var/tmp/.root_98377f_salt/py2/salt/utils/templates.py", line 244, in _get_jinja_error
    File "/var/tmp/.root_98377f_salt/py2/salt/utils/templates.py", line 227, in _get_jinja_error_line
TypeError: 'NoneType' object has no attribute '__getitem__'

I'm pretty new to saltstack, jinja and python so I ran out of ideas and in the docs I couldn't manage to find an example that fits to this case.


Solution

  • You just have to pass it as two parameters like this:

    {%- set postfixversion = salt['lowpkg.info']('postfix',attr='version') %}
    

    This will return a dictionary:

    {'postfix': {'version': '3.0.5'}}
    

    In order to get the version directly you need to filter the output

    {%- set postfixversion = salt['lowpkg.info']('postfix',attr='version')['postfix']['version'] %}
    

    Then create a new variable that will contains the path as a value:

    {% set path_to_version = '/folder/' + postfixversion + '/subfolder' %}