Open felixglow opened 4 years ago
[tjyang@salt01 ~]$ sudo salt -L salt01 cmd.run "print 'hello'" shell="/usr/bin/python" runas=root group=root
salt01:
hello
[tjyang@salt01 ~]$
- But getting same error message in salt01:/var/log/minion
2020-10-11 06:27:00,492 [salt.loaded.int.module.cmdmod:290 ][WARNING ][2933] Attempt to run a shell command with what may be an invalid shell! Check to ensure that the shell </usr/bin/python> is valid for this user.
2020-10-11 06:27:00,522 [salt.loaded.int.module.cmdmod:515 ][ERROR ][2933] Environment could not be retrieved for user 'root': stderr=u' File "
- salt-minion version:
[tjyang@salt01 ~]$ sudo salt-minion --version /usr/lib/python2.7/site-packages/salt/scripts.py:212: DeprecationWarning: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. Salt will drop support for Python 2.7 in the Sodium release or later. salt-minion 3000.3 [tjyang@salt01 ~]$
<details><summary>salt --versions-report</summary>
[tjyang@salt01 ~]$ salt --versions-report Salt Version: Salt: 3000.3
Dependency Versions: cffi: 1.6.0 cherrypy: unknown dateutil: Not Installed docker-py: Not Installed gitdb: Not Installed gitpython: Not Installed Jinja2: 2.7.2 libgit2: 0.26.3 M2Crypto: Not Installed Mako: Not Installed msgpack-pure: Not Installed msgpack-python: 0.6.2 mysql-python: Not Installed pycparser: 2.14 pycrypto: 2.6.1 pycryptodome: Not Installed pygit2: 0.26.3 Python: 2.7.5 (default, Apr 2 2020, 13:16:51) python-gnupg: Not Installed PyYAML: 3.11 PyZMQ: 15.3.0 smmap: Not Installed timelib: Not Installed Tornado: 4.5.3 ZMQ: 4.1.4
System Versions: dist: centos 7.8.2003 Core locale: UTF-8 machine: x86_64 release: 3.10.0-1127.19.1.el7.x86_64 system: Linux version: CentOS Linux 7.8.2003 Core
[tjyang@salt01 ~]$
</details>
the error message occur when use runas or group,and shell != DEFAULT_SHELL please see the code: https://github.com/saltstack/salt/blob/7dc99b3a5ad327f3ae13e0792e6077b7764dddb2/salt/modules/cmdmod.py#L477 I can find the code, but i don't know the reason.
My fault, shell must in /etc/shells
@felixglow , Thanks
Still have one error message left.
2020-10-12 08:23:31,306 [salt.loaded.int.module.cmdmod:290 ][WARNING ][25290] Attempt to run a shell command with what may be an invalid shell! Check to ensure that the shell </usr/bin/python> is valid for this user.
[tjyang@salt01 ~]$ python -c "print \"hello\""
hello
[tjyang@salt01 ~]$
2020-10-12 08:41:31,952 [salt.loaded.int.module.cmdmod:515 ][ERROR ][26491] Environment could not be retrieved for user 'root': stderr=u' File "
- from this command
[tjyang@salt01 ~]$ sudo salt -L salt01 cmd.run "print \"hello\"" shell="/usr/bin/python" runas=root group=root salt01: hello [tjyang@salt01 ~]$
@tjyang yes, if you specify shell="/usr/bin/python", the env_cmd will be "sudo -u root -g root -s -- /usr/bin/python -c /usr/bin/python",when you run the env_cmd, this error message occur
File "<string>", line 1 /usr/bin/python ^ SyntaxError: invalid syntax
see the code:
after read the code, i think we can't specify shell unless it is in the '/etc/shells'.
if you want to run python code, you can use "cmd.exec_code_all", like this:
salt "192.168.23.22" cmd.exec_code_all python "import os; os.system('ls -rlt')" runas=root group=root
the first naming the executable language, aka - python2, python3, ruby, perl, lua, etc. the second string containing the code you wish to execute.
it will create a tmp file, and remove it after exec code.
@felixglow , Thanks for the detail response from source code level. I can now use your "ls -lrt" python oneliner without triggering error in log file. Hopefully re-opening this issue can have salt developer triage the 2nd error message(Environment could not be retrieved ...) is valid or not.)
are you asking why the environment error shows up with this command? salt-call --local cmd.run "print \"hello\"" shell="/usr/bin/python" runas=root group=root
?
Looks like its because like @felixglow pointed out there is a syntax error so there is nothing on stdout for it to parse out and lands on that error. Can you clarify the question?
I don't have salt01 (centos 7.8) currently. Following is same test on centos 8.2.
[me@centos8t01 ~]$ python3 -c "print (\"hello\")"
hello
[me@centos8t01 ~]$
me@centos8t01 ~]$ sudo salt -L centos8t01 cmd.run "print (\"hello\")" shell="/usr/bin/python3" runas=root group=root
centos8t01:
hello
[me@centos8t01 ~]$
[root@centos8t01 salt]# tail -f minion
2020-10-20 16:55:32,414 [salt.utils.parsers:1117][WARNING ][7845] Minion received a SIGTERM. Exiting.
2020-10-20 16:55:58,152 [salt.loaded.int.module.cmdmod:537 ][ERROR ][9350] Environment could not be retrieved for user 'root': stderr=' File "<string>", line 1\n /usr/bin/python3.6\n ^\nSyntaxError: invalid syntax\n' stdout=''
Description run python script occur error message
Setup
salt minion error message
2020-10-10 17:00:19,965 [salt.loaded.int.module.cmdmod:290 ][WARNING ][28010] Attempt to run a shell command with what may be an invalid shell! Check to ensure that the shell </usr/bin/python> is valid for this user. 2020-10-10 17:00:20,012 [salt.loaded.int.module.cmdmod:518 ][ERROR ][28010] Environment could not be retrieved for user 'root': stderr=u' File "<string>", line 1\n /usr/bin/python\n ^\nSyntaxError: invalid syntax\n' stdout=u''
Steps to Reproduce the behavior
salt "192.168.23.22" cmd.run "print 'hello'" shell="/usr/bin/python" runas=root group=root
Expected behavior
Screenshots
Versions Report
salt --versions-report
(Provided by running salt --versions-report. Please also mention any differences in master/minion versions.) ``` Salt Version: Salt: 3000.2 Dependency Versions: cffi: Not Installed cherrypy: Not Installed dateutil: Not Installed docker-py: Not Installed gitdb: Not Installed gitpython: Not Installed Jinja2: 2.10.3 libgit2: Not Installed M2Crypto: 0.21.1 Mako: Not Installed msgpack-pure: Not Installed msgpack-python: 0.6.2 mysql-python: Not Installed pycparser: Not Installed pycrypto: 2.6.1 pycryptodome: Not Installed pygit2: Not Installed Python: 2.7.5 (default, Apr 2 2020, 13:16:51) python-gnupg: Not Installed PyYAML: 3.10 PyZMQ: 14.7.0 smmap: Not Installed timelib: Not Installed Tornado: 4.5.3 ZMQ: 4.1.4 System Versions: dist: centos 7.3.1611 Core locale: UTF-8 machine: x86_64 release: 3.10.0-514.el7.x86_64 system: Linux version: CentOS Linux 7.3.1611 Core ```