salt icon indicating copy to clipboard operation
salt copied to clipboard

cmd.run commands with pipe does not work from sls

Open deyanx opened this issue 9 years ago • 4 comments

Description of Issue/Question

This was working properly in state file, few months ago... Not sure in which Salt version...

{% set GID = salt['cmd.run']('getent group xxxxxx | cut -d: -f3') %}

Now this returns following error...

getent: invalid option -- 'd'
Try getent --help' or getent --usage' for more information.

It looks like pipe symbol is removed or probably escaped on execution.

If I exec the same from command line it still works properly...

salt "aaaaaa" cmd.run 'getent group xxxxxx | cut -d: -f3'
aaaaaa:
    1004

Versions Report

MASTER

salt --versions-report Salt Version: Salt: 2017.7.2

Dependency Versions: cffi: Not Installed cherrypy: Not Installed dateutil: Not Installed docker-py: Not Installed gitdb: Not Installed gitpython: Not Installed ioflo: Not Installed Jinja2: 2.8.1 libgit2: Not Installed libnacl: Not Installed M2Crypto: Not Installed Mako: Not Installed msgpack-pure: Not Installed msgpack-python: 0.4.6 mysql-python: Not Installed pycparser: Not Installed pycrypto: 2.6.1 pycryptodome: Not Installed pygit2: Not Installed Python: 2.7.13 (default, Jul 12 2017, 17:32:34) python-gnupg: Not Installed PyYAML: 3.11 PyZMQ: 14.5.0 RAET: Not Installed smmap: Not Installed timelib: Not Installed Tornado: 4.2.1 ZMQ: 4.0.5

System Versions: dist: centos 6.9 Final locale: ANSI_X3.4-1968 machine: x86_64 release: 2.6.32-042stab123.8 system: Linux version: CentOS 6.9 Final

MINION

Salt Version: Salt: 2017.7.2

Dependency Versions: cffi: Not Installed cherrypy: Not Installed dateutil: Not Installed docker-py: Not Installed gitdb: Not Installed gitpython: Not Installed ioflo: Not Installed Jinja2: 2.7.2 libgit2: Not Installed libnacl: Not Installed M2Crypto: 0.21.1 Mako: Not Installed msgpack-pure: Not Installed msgpack-python: 0.4.8 mysql-python: Not Installed pycparser: Not Installed pycrypto: 2.6.1 pycryptodome: Not Installed pygit2: Not Installed Python: 2.7.5 (default, Aug 4 2017, 00:39:18) python-gnupg: Not Installed PyYAML: 3.11 PyZMQ: 15.3.0 RAET: Not Installed smmap: Not Installed timelib: Not Installed Tornado: 4.2.1 ZMQ: 4.1.4

System Versions: dist: redhat 7.4 Georgy Grechko locale: UTF-8 machine: x86_64 release: 3.10.0-714.10.2.lve1.4.65.el7.x86_64 system: Linux version: CloudLinux 7.4 Georgy Grechko

deyanx avatar Oct 21 '17 14:10 deyanx

Pls read https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.cmdmod.html

python_shell (bool) -- If False, let python handle the positional arguments. Set to True to use shell features, such as pipes or redirection.

It's interesting the default for python_shell is None

salt abc  cmd.run python_shell=False 'getent group root | cut -d: -f3'
abc:
    getent: invalid option -- 'd'
    Try `getent --help' or `getent --usage' for more information.
salt abc  cmd.run  'getent group root | cut -d: -f3'
abc:
    0
salt abc  cmd.shell  'getent group root | cut -d: -f3'
abc:
    0

damon-atkins avatar Oct 22 '17 00:10 damon-atkins

Thank you. I now realize that I should actually use cmd.shell for this.

It is strange that execution on command line have different behavior than execution from sls jinja.

It's also strange that this was working properly, I believe on version 2016.11.2, and now suddenly default behavior is changed in 2017.x version, but only for cmd.run execution in sls jinja, but not on cli, so maybe that is still a minor bug with this default behavior that confused me here...

Also documentation states that default python_shell=None, so it's not really clear what should be the default behavior.

deyanx avatar Oct 22 '17 10:10 deyanx

Yes, we have an open issue to document this in the 2017.7 release notes

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

I will try to get to it today.

gtmanfred avatar Oct 23 '17 13:10 gtmanfred

Hi I want to run following through salt state. sudo -u postgres psql postgres \password postgres How can it be done ?

The following is not working : postgres-userupdate : 'sudo -u postgres psql postgres' : cmd.run

Chidamber11 avatar Dec 03 '18 15:12 Chidamber11