-
-
Notifications
You must be signed in to change notification settings - Fork 343
Description
As discussed in cnfgmngmntcmp.
Currently, if shell is unset (the default), the cmd parameter is interpreted as a list of arguments separated by a non-empty sequence of spaces. (E.g. "ls -l /tmp" becomes ["ls", "-l", "/tmp"])[1].
While useful on the surface, this is an unusual behaviour, that I personally would least expect from this parameter, and most likely will be hard to make useful for complex commands.
Curiously, Ansible's ansible.builtin.command works in a similar way. However the mgmt implementation is not compatible: Ansible uses the Python's shlex module[2], which implements a parody of a working shell parser.
Additionally, if this ever works on Windows, this will become even more weird. Windows uses a single string to pass parameters to the processes, so the POSIX implementation is to concatenate the array of arguments and pass it as one string. So this mgmt resource will split the string, which then will be concatenated in libc, (which then most likely will be split again in the process itself).
My suggestions (in reverse order of preference) are:
- replace Args, Cmd and Shell with the following parameters:
- Command: to specify a command as a list of arguments, where the first argument is a path to the executable
- ShellCommand: to specify a command as a string to be passed to a shell (sh -c "...")
- Shell: to override the shell implementation
Command and (Shell, ShellCommand) would be mutually exclusive. Shell becomes optional, and ShellCommand by itself invokes the default shell.
- similarly to Ansible, add a new resource ("shell") to specifically use shell for running the commands. Change the Cmd parameter of the exec resource to accept a list of arguments
- use a shell-like parser to imitate parsing of the Cmd parameter with more fidelity
- document the current behaviour in detail
I'd be happy to provide a path for whatever suggestion of your choice, as long as you choose correctly.
[1] https://mgmtconfig.com/docs/resources/#exec
[2] https://github.com/python/cpython/blob/3.13/Lib/shlex.py