25

I have a templated SLS in Salt I'm trying to build, but it's emitting invalid syntax, which is resulting in errors such as:

my-minion-id:
    - State 'system' in SLS 'network' is not formed as a list

In principle, it should be possible to, somehow examine the output of the Jinja template before it attempts to parse the output as an SLS file. There exists a Python module for the Jinja renderersalt.renderers.jinja, but if I attempt to execute it on the CLI, I get an error:

# salt my-minion-id salt.renderers.jinja.render /srv/salt/network/init.sls
my-minion-id:
    'salt.renderers.jinja.render' is not available.
ERROR: Minions returned with non-zero exit code
# salt my-minion-id renderers.jinja.render /srv/salt/network/init.sls
my-minion-id:
    'renderers.jinja.render' is not available.
ERROR: Minions returned with non-zero exit code

How can I see the output of my template? It seems absurd it should be this difficult to debug.

2 Answers 2

14

Check out the slsutil.renderer module.

This should do what you want

salt my-minion-id slsutil.renderer /srv/salt/network/init.sls 'jinja'

This module just calls the compile_template function directly for you.

Edit: /srv/salt/network/init.sls is the path on the minion, if you are not targeting the master as your minion, you will probably need to do the following.

salt minion-id cp.cache_file salt://network/init.sls
salt minion-id slsutil.renderer /var/cache/salt/minion/files/base/network/init.sls

or point to whatever file that cache_file spits out.

If you are on 2018.3 or newer, you can just specify salt://network/init.sls

5
  • But what path is /srv/salt/network ? Is it the path on the master? The minion?
    – Mrten
    Commented Oct 26, 2018 at 12:55
  • It is a path on the minion. You can do salt minion-id cp.cache_file salt://network/init.sls and then run slsutil.renderer against the file it spits back after being cached on the minion, or starting in 2018.3, you can just specify salt://network/init.sls
    – gtmanfred
    Commented Oct 27, 2018 at 13:04
  • Unless I'm misunderstanding, or something differs in my environment, slsutil.render attempts to compile the template and outputs the results. That lets you see the final resulting state, but it isn't useful for examining the output of jinja that results is an invalid state, which is the question being asked. Commented Apr 7, 2020 at 15:17
  • yes, but you can just print out the jinja object, or template it to yaml and dump it so that you can inspect the data in the jinja object.
    – gtmanfred
    Commented Apr 8, 2020 at 16:15
  • slsutil.render renders then compiles states and there does not appear to be a way to terminate the pipeline prior to compilation. so I don't see how it solves "In principle, it should be possible to, somehow examine the output of the Jinja template before it attempts to parse the output as an SLS file." If you know otherwise then I would be interested to see an example (specifically with Jinja that produces a broken state) because an irksome thing with cp.get_template is that it requires an output FD and so it isn't effective when called by salt from a master as /dev/stdout isn't available. Commented Apr 12, 2020 at 13:56
14

Given how much time I spent weeks ago struggling with a closely-related issue, I wish I'd figured this out sooner.

The solution appears to be to use salt.modules.cp.get_template to have the Salt minion retrieve the file, render it through the templating engine and place it in a readable place:

# salt my-minion-id cp.get_template salt://network/init.sls /root/network.sls template=jinja
my-minion-id:
    /root/network.sls

From there, you connect to the my-minion-id host and examine the file you placed at /root/network.sls.

This makes sense; salt.renderers.jinja is in the salt.renderers namespace, while the modules you have access to from the CLI are in the salt.modules namespace.

It also makes sense from a data visibility standpoint; template rendering happens on the minion, where grains and such are available, and I've yet to see a module that executes minion code return arbitrary output to the master (for view on the CLI, for example); the returned data is invariably well-structured and concise. (There may be such a module, but I don't know what it is. It would be a preferable solution to dropping test files onto a minion.)

edit: @gtmanfred's answer is far better and more direct, and I've accepted that one. I'm leaving this one here for informative purposes. It's not the best solution, but it does still work.

3
  • 2
    This answer seems to be more useful for debugging errors induced by jinja since it provides the text output after jinja processing, before compilation. Calling slsutil.render attempts to compile the template and if the fails (the motivation for the original question) then it not emit output that is useful for determining what kind of mess is being produced by Jinja. Commented Apr 7, 2020 at 15:13
  • If your master is a minion (which seems to be a good idea, since everything in salt, even docs, runs on a minion) you can do salt-call cp.get_template salt://foo.sls /dev/stdout
    – emorris
    Commented Sep 18, 2020 at 10:44
  • This definitely seems the most useful way for seeing what the rendered-to-text output of an SLS file is after Jinja processing. One gotcha is that it always uses pillarenv=base and the normal override mechanism (specifying pillarenv on the command line) does not work as far as I can tell. Workaround is to update the config so pillarenv=base temporarily, which is a pain but does work. Commented Jan 7, 2022 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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