1

We are using serverdensity (BETA MODULE - https://docs.saltstack.com/en/latest/ref/states/all/salt.states.serverdensity_device.html) with salt and have hit the (bug?) where if you try a :

salt '*SERVER-01*' --state-output=mixed state.apply serverdensity test=True

Then the serverdensity module always tries to add the server to monitoring whether it's in 'test=True' mode or not.

I was hoping to set a Jinja variable (or use an existing one?) with the current test mode and thus do a "if test then echo 'not adding' else add" to get past this.

However I can't find out how to do this in the docs or google searches. I even looked to see if I could pull in the ENV args on the master to see if it was passed as a command line arg but $@ produces nothing in:

{% set args = salt['environ.get']('@') %}

Any ideas?

Running salt 2018.3.2 (Oxygen) on Centos 7

M Haswell
  • 31
  • 5

2 Answers2

2

In case anyone finds this and wants to know the answer - I checked on the saltstackcommunity slack and some very helpful people responded:

terminalmage [2:55 PM] checks looks like it's just called opts So if you wanted to see if test mode is enabled, one way would be something like:

{% if opts['test'] %}
echo foo:
  cmd.run
{% endif %}

Then if that state is present in the state return, you know it was run in test mode But another way of checking would just be to do a plain old cmd.run state because you'd get a result saying that the command would be run if you ran in test mode

local:
----------
      ID: echo foo
Function: cmd.run
  Result: None
 Comment: Command "echo foo" would have been executed
 Started: 14:58:55.482679
Duration: 0.499 ms
 Changes:

Also want to point out that serverdensity have also picked this up and state that their module is supported so this underlying issue should be solved soon too. Until then the if-then-else of opts['test'] is working for us.

M Haswell
  • 31
  • 5
0

In a state, I think the correct answer is to check __opts__ for tests presence, see https://docs.saltstack.com/en/latest/ref/states/writing.html#test-state

# Return comment of changes if test.
if __opts__['test']:
    ret['result'] = None
    ret['comment'] = 'State Foo will execute with param {0}'.format(bar)
    return ret
daks
  • 673
  • 6
  • 23
  • That's probably true - but only useful for those people who programmed the python module : the company Server Density. I was just using the module in an "off the shelf" capacity and wanted to find out how to work around their bug in Jinja. On a side note I raised this bug with them and they have sent it onto their engineering team so hopefully it should be fixed soon. – M Haswell Jan 18 '19 at 10:45
  • @MHaswell I was completing the previous answer (which is yours) which talks about opts['test'] when it should be __opts__['test'] *when used in formula*. In fact, I didn't see you were talking about a specific formula which you don't want to modify – daks Jan 18 '19 at 11:02