Example for using of cmd.script in a state file?

4,843 views
Skip to first unread message

Sebastian

unread,
Aug 8, 2012, 8:21:40 AM8/8/12
to salt-...@googlegroups.com
Hi,

I could not find how to really use cmd.script in a state. I followed the docs and are using cmd.run in production with success, but my state file outputs some weird stuff if I run my cmd.script:

minion1:
----------
    State: - cmd
    Name:      upgrade-to-php5.4
    Function:  script
        Result:    True
        Comment:   Command "upgrade-to-php5.4" run
        Changes:   pid: 6927
                   retcode: 0
                   stderr: stdin: is not a tty
                   stdout: 

Here is excerpt from my state file:
...
upgrade-to-php5.4:
  cmd.script:
    - user: root
    - shell: /bin/bash
    - source: salt://language/php54/prepare-for-php54.sh
...

My shell script is writing into to a tmp-file for testing but it is somehow not executed by the state engine. Interestingly, if I run the same stuff via

salt '*' salt://language/php54/prepare-for-php54.sh

then Salt complains that the file is not existing!? But it is existing and the path normally works with other files, too. Does anyone use cmd.script and can maybe give a real-world example? Thank you so much!

Thomas S Hatch

unread,
Aug 8, 2012, 10:11:59 PM8/8/12
to salt-...@googlegroups.com
This sls looks good. I think that the error:
stdin: is not a tty
Is where to start, could there be something wrong with how the script is executed, what is the output from:

salt-call cmd.script salt://language/php54/prepare-for-php54.sh

run on the minion?

Ryan K

unread,
Aug 9, 2012, 7:34:45 AM8/9/12
to salt-...@googlegroups.com
I'm also having some issues with cmd.script.

If I have a simple SLS like:

test:
  cmd:
    - script
    - name: salt://test

And test.sh looks like:

---
#!/usr/bin/env bash

touch /tmp/ran-success
echo "RAN!"
---

The result looks like:

State: - cmd
    Name:      salt://test
    Function:  script
        Result:    True
        Comment:   Command "salt://test" run
        Changes:   pid: 1755
                   retcode: 0
                   stderr: 
                   stdout: 

Notice that there isn't anything in stdout. It didn't run.

If I try to run the file as a different user I get a permissions error. Here is the SLS:

test:
  cmd:
    - script
    - name: salt://test
    - user: john
    - group: john
    - cwd: /home/john

And the result:

State: - cmd
    Name:      salt://test
    Function:  script
        Result:    False
        Comment:   Command "salt://test" run
        Changes:   pid: 1833
                   retcode: 126
                   stderr: -su: /tmp/tmpDHyMHT: Permission denied
                   stdout: 

I'm not sure what's going on.

Ben Hosmer

unread,
Aug 9, 2012, 8:49:18 AM8/9/12
to salt-...@googlegroups.com
This is just a thought, I haven't actually used the cmd.script before, but here:

- name: salt://test

would possibly calling:

- name: salt://test.sh

fix it?
--
Ben Hosmer

Brevard Tech - Brevard County Florida's Tech User Group

Ryan K

unread,
Aug 9, 2012, 9:25:55 AM8/9/12
to salt-...@googlegroups.com
Went ahead and changed it:

runscript:
  cmd:
    - script
    - name: salt://test.sh
    - user: vagrant
    - group: vagrant
    - cwd: /tmp

But no dice.

Maybe it's my environment.

local:
----------
    State: - cmd
    Name:      salt://test.sh
    Function:  script
        Result:    False
        Comment:   Command "salt://test.sh" run
        Changes:   pid: 5261
                   retcode: 126
                   stderr: -su: /tmp/tmpMF16jd: Permission denied
                   stdout:

Running it was root doesn't seem to do anything:

runscript:
  cmd:
    - script
    - name: salt://test.sh

test.sh:

#!/usr/bin/env bash

echo 'RAN!'
touch /tmp/evidence

INFO    ] Executing state cmd.script for salt://test.sh
[INFO    ] Executing command /tmp/tmpS315fm in directory /root
[INFO    ] {'pid': 5295, 'retcode': 0, 'stderr': '', 'stdout': ''}
local:
----------
    State: - cmd
    Name:      salt://test.sh
    Function:  script
        Result:    True
        Comment:   Command "salt://test.sh" run
        Changes:   pid: 5295
                   retcode: 0
                   stderr: 
                   stdout: 

Nothing in stdout and /tmp/evidence doesn't exist.

Thomas S Hatch

unread,
Aug 10, 2012, 1:34:53 AM8/10/12
to salt-...@googlegroups.com
Your sls data all looks good, I have filed a bug and I will try to reproduce the issue and get if fixed for 0.10.3

https://github.com/saltstack/salt/issues/1803

Marat Komarov

unread,
Aug 10, 2012, 7:43:57 AM8/10/12
to salt-...@googlegroups.com
Hi,

I found that retcode is always 0 even if script actually returns non-zero code

state file:

salt://init.sh:
  cmd:
    - script

script:

#!/bin/bash
exit 2


salt version: 0.10.2


Regards,
Marat

Thomas S Hatch

unread,
Aug 10, 2012, 2:18:22 PM8/10/12
to salt-...@googlegroups.com
Thanks, this is good data, I will add it to the issue

On Fri, Aug 10, 2012 at 5:43 AM, Marat Komarov <mac.k...@gmail.com> wrote:
Hi,

I found that retcode is always 0 even if script actually returns non-zero code

state file:

salt://init.sh:
  cmd:
    - script

script:

#!/bin/bash
exit 2


salt version: 0.10.2


Regards,
Marat

On Friday, August 10, 2012 8:34:53 AM UTC+3, Thomas Hatch wrote:
Your sls data all looks good, I have filed a bug and I will try to reproduce the issue and get if fixed for 0.10.3

Reply all
Reply to author
Forward
0 new messages