9

Is there a way to log a custom debug message in saltstack out of an .sls or a .jinja file? i.e. something like:

{% salt.log_message("Entering...") %}
Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Ya.
  • 1,671
  • 4
  • 27
  • 53
  • 1
    Not sure what exactly you are trying to print. Please give some more context. In simple case - what you can do is smth like: {% set foo="bar" %} debug_print: cmd.run: - name: "echo {{foo}}" This will execute `echo` dumping the content of `foo` . Sorry for the code block, my markdown-foo is too bad :) – alexK Nov 02 '16 at 10:21
  • You can also try [test state](https://docs.saltstack.com/en/latest/ref/states/all/salt.states.test.html#module-salt.states.test) – alexK Nov 02 '16 at 10:54

2 Answers2

12

This was added as a feature in 2017.7.0:

Yes, in Salt, one is able to debug a complex Jinja template using the logs. For example, making the call:

{%- do salt.log.error('testing jinja logging') -%}

Will insert the following message in the minion logs:

2017-02-01 01:24:40,728 [salt.module.logmod][ERROR   ][3779] testing jinja logging
oeuftete
  • 2,628
  • 1
  • 24
  • 33
11

Add a state using test.nop and add things you want to inspect as arguments to it.

The use

salt-call -l debug state.apply yourslsfile test=True

or

salt-call --output=yaml state.show_sls yourslsfile

to check the result.

For example:

debug.sls

test:
  test.nop:
  - user: {{ grains.username }}
  - nested:
      foo: bar

Here is the result of state.show_sls

local:
  test:
    test:
    - user: ian
    - nested:
        foo: bar
    - nop
    - order: 10000
    __sls__: !!python/unicode dotfiles
    __env__: base

It is better to setup a standalong environment to test states, like this

Ian Yang
  • 1,091
  • 8
  • 20