Add failure check in cmd.script

62 views
Skip to first unread message

varun bhatnagar

unread,
Jul 18, 2017, 5:25:12 AM7/18/17
to salt-...@googlegroups.com
Hi,

I am having a small testing environment with saltstack - Carbon ( 1 master and 2 windows minions). I have a state file in which I am executing a '.bat' script 

  Execute C:\Users\test\Desktop\my_dir\tmp\extracted\my_service\bin\service_install.bat to start service:
    cmd.script:
      - name: 'C:\Users\test\Desktop\my_dir\tmp\extracted\my_service\bin\service_start.bat'
      - cwd: '{{ loc }}\bin\'
      - args: '--no-pause'

Sometimes the script fails while stopping the service but the result is still green (maybe because the script execution doesn't fail).
I want to add a check here that if in case it fails then the execution should terminate. Can anyone please help me with this?

BR,
Varun

Viet Hung Nguyen

unread,
Jul 18, 2017, 8:22:54 AM7/18/17
to Salt-users
I don't know about Windows, but as you said:
> Sometimes the script fails while stopping the service but the result is still green (maybe because the script execution doesn't fail).

So your script failed or not? (on *NIX, it returns non zero status code, then it failed).

cmd.script normally will fail when the script  returns non-zero status code
, thus, the salt states execution will fail all other states that follow it (terminate).
So I think you should modify your .bat script to handle the check and exit accordingly instead of delegate the task for salt state itself.


--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/CAGxOggFq6aOPmASgVm%2Bm7pr1azJVgy2YciOf6txQt255UtW_FA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.



--
Thanks & Best regards,
Nguyen Viet Hung (Mr)
[a.k.a HVN]

Mobile: 0982090290

varun bhatnagar

unread,
Jul 18, 2017, 8:29:43 AM7/18/17
to Salt-users
I tried modifying the state file as below:

{%- if {{ salt.cmd.run('C:\Users\test\Desktop\my_dir\tmp\extracted\my_service\bin\service_start.bat', kwargs='--no-pause')['stderr'] }} -%}
Print info message:
  module.run:
    - name: test.echo
    - text: 'Service installation failed'
{% endif %}

Now I am getting a new error:

    Data failed to compile:
----------
    Rendering SLS 'base:update_agent.stop_agent' failed: Jinja syntax error: truncated \UXXXXXXXX escape; line 1

---

{%- if {{ salt.cmd.run('C:\Users\test\Desktop\my_dir\tmp\extracted\my_service\bin\service_start.bat', kwargs='--no-pause')['stderr'] }} -%}    <======================
Print info message:
  module.run:
    - name: test.echo
    - text: 'Service installed'
{% endif %}

Any idea how to fix this?

BR,
Varun

varun bhatnagar

unread,
Jul 18, 2017, 8:34:23 AM7/18/17
to Salt-users
Hello HVN,

Thanks for replying.

The .bat script is not written by me so I can not do those changes, that is why I am looking for some alternative until that is fixed.
Now, I am trying to fetch the stderr and then perform the respective operation but I am facing a syntax error there.
Could you please suggest anything to fix that?

BR,
Varun


On Tuesday, 18 July 2017 14:22:54 UTC+2, Viet Hung Nguyen wrote:
I don't know about Windows, but as you said:
> Sometimes the script fails while stopping the service but the result is still green (maybe because the script execution doesn't fail).

So your script failed or not? (on *NIX, it returns non zero status code, then it failed).

cmd.script normally will fail when the script  returns non-zero status code
, thus, the salt states execution will fail all other states that follow it (terminate).
So I think you should modify your .bat script to handle the check and exit accordingly instead of delegate the task for salt state itself.

On Tue, Jul 18, 2017 at 4:25 PM, varun bhatnagar <varun...@gmail.com> wrote:
Hi,

I am having a small testing environment with saltstack - Carbon ( 1 master and 2 windows minions). I have a state file in which I am executing a '.bat' script 

  Execute C:\Users\test\Desktop\my_dir\tmp\extracted\my_service\bin\service_install.bat to start service:
    cmd.script:
      - name: 'C:\Users\test\Desktop\my_dir\tmp\extracted\my_service\bin\service_start.bat'
      - cwd: '{{ loc }}\bin\'
      - args: '--no-pause'

Sometimes the script fails while stopping the service but the result is still green (maybe because the script execution doesn't fail).
I want to add a check here that if in case it fails then the execution should terminate. Can anyone please help me with this?

BR,
Varun

--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.

Kees Bos

unread,
Jul 18, 2017, 8:41:59 AM7/18/17
to salt-...@googlegroups.com
Double the backslashes

-> 'C:\\Users\\t ...
--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.

varun bhatnagar

unread,
Jul 18, 2017, 9:01:58 AM7/18/17
to Salt-users
Thanks Kees, but that didn't help.

    Data failed to compile:
----------
    Rendering SLS 'base:update_agent.stop_agent' failed: Jinja syntax error: expected token ':', got '}'; line 1

---

{%- if {{ salt.cmd.run('C:\\Users\\test\\Desktop\\my_dir\\tmp\\extracted\\my_service\\bin\\service_start.bat', kwargs='--no-pause')['stderr'] }} -%}    <======================
Print info message:
  module.run:
    - name: test.echo
    - text: 'Service installation failed'
{% endif %}

Now it complains about ':'. I tried using double quotes as well but no luck.

BR,
Varun

Kees Bos

unread,
Jul 18, 2017, 9:17:26 AM7/18/17
to salt-...@googlegroups.com

You've got a different error now.

{% ... {{ .. }} ... %} is a mixup of jinja

In this case, remove the '{{' and '}}'
Reply all
Reply to author
Forward
0 new messages