saltstack / salt

Software to automate the management and configuration of any infrastructure or application at scale. Get access to the Salt software package repository here:
https://repo.saltproject.io/
Apache License 2.0
14.14k stars 5.47k forks source link

[BUG] run python script occur error message #58707

Open felixglow opened 4 years ago

felixglow commented 4 years ago

Description run python script occur error message

Setup image

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 image

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 ```
tjyang commented 4 years ago

Summary

of minions targeted: 1

of minions returned: 1

of minions that did not return: 0

of minions with errors: 0


[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 "", line 1\n /usr/bin/python\n ^\nSyntaxError: invalid syntax\n' stdout=u''

- 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>
felixglow commented 4 years ago

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.

felixglow commented 4 years ago

My fault, shell must in /etc/shells

tjyang commented 3 years ago

@felixglow , Thanks

Still have one error message left.

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 "", line 1\n /usr/bin/python\n ^\nSyntaxError: invalid syntax\n' stdout=u''

- 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 ~]$

felixglow commented 3 years ago

@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: image

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. image

tjyang commented 3 years ago

@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.)

Ch3LL commented 3 years ago

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?

tjyang commented 3 years ago

I don't have salt01 (centos 7.8) currently. Following is same test on centos 8.2.

salt --versions-report on centos8t01 ``` [me@centos8t01 ~]$ salt --versions-report Salt Version: Salt: 3001.1 Dependency Versions: cffi: 1.11.5 cherrypy: unknown dateutil: 2.6.1 docker-py: Not Installed gitdb: Not Installed gitpython: Not Installed Jinja2: 2.10.1 libgit2: Not Installed M2Crypto: 0.35.2 Mako: Not Installed msgpack-pure: Not Installed msgpack-python: 0.6.2 mysql-python: Not Installed pycparser: 2.14 pycrypto: Not Installed pycryptodome: Not Installed pygit2: Not Installed Python: 3.6.8 (default, Apr 16 2020, 01:36:27) python-gnupg: Not Installed PyYAML: 3.12 PyZMQ: 19.0.0 smmap: Not Installed timelib: Not Installed Tornado: 4.5.3 ZMQ: 4.3.3 System Versions: dist: centos 8 Core locale: UTF-8 machine: x86_64 release: 4.18.0-193.19.1.el8_2.x86_64 system: Linux version: CentOS Linux 8 Core [me@centos8t01 ~]$
[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=''