-
Notifications
You must be signed in to change notification settings - Fork 24k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
command module: environment variables are substituted unconditionally #54162
Comments
Files identified in the description: If these files are inaccurate, please update the |
consider you are already consuming one quote due to calling ansible in a shell |
Shell quotes with ansible command are a red herring (wrapping in double quotes as you wrote actually makes the shell expand ---
- hosts: localhost
tasks:
- name: Should not expand $HOME
command:
/usr/bin/stat $HOME
register: dont_expand_home
failed_when: dont_expand_home.rc == 0 This still fails: https://gist.github.com/rfc1459/fd6b498ad6e4537fb0fd9f51757b6aa2. This is caused by |
Actually, looking at the source of |
Reproduced with Ansible 2.5.5 |
Actually variables are expanded even in other modules where no one expect variables expansion. For example - git:
repo: file:///tmp/$HOME
dest: /tmp/repo
- user:
name: test
comment: $HOME
- unarchive:
src: /tmp/$HOME
dest: /tmp
- acl:
path: $HOME And there is more modules that use |
@fpob all file paths will get expanded, that is expected and done on purpose. In other cases it depends on being backed by an API or by a CLI tool, which is the case for |
@bcoca paths yes, it is expected. Unexpected is env variable expansion. |
Still an issue in 2.9.6. There's no way to pass a literal |
no need to update, while ticket is open the issue is supposed to persist, leaning to this not being a bug, since the substitution is intended in all the cases discussed. I have not really seen a use case for the variables not to be expanded, just demonstrations on when they are. |
Yet the documentation says otherwise:
It all boils down to whether the documentation is correct (then this is definitely a bug) or wrong (then it should be very clear that environment variables will be expanded unconditionally whether you want it or not). By the way, here's the original use case: I need to send a command to a remote host which needs a literal |
@bcoca OK, let's say variable expansion is expected. What about using @rfc1459 If you just want use literal - hosts: localhost
tasks:
- name: Should not expand $HOME
command:
/usr/bin/stat $HOME
register: dont_expand_home
failed_when: dont_expand_home.rc == 0
environment:
HOME: $HOME |
Files identified in the description: If these files are incorrect, please update the |
I just stepped on this mine also ... and wasted at least 1h trying to find the right incantations: without quotes, with quotes, nesting quotes, escaping quotes, using In my case I wanted to run It drove me nuts:
The current behavior is definitely at odds with the documentation. I always expected I see the following possible solutions:
|
I updated this to be a feature request instead of a bug report because we may want to address this by adding a new option to the command module to be backwards compatible. |
While we can't change the behaviour a new option to disable it is being proposed by #80512. |
SUMMARY
Contrary to what is stated in the module documentation, all occurrences of environment variables are unconditionally expanded. The expansion also does not take escape characters into account, so
\$HOME
gets expanded to\actual-home-directory-path
.This happens because
expand_user_and_vars
is not set toFalse
when callingmodule.run_command
and there is no way to control this behavior.ISSUE TYPE
COMPONENT NAME
command module
ANSIBLE VERSION
CONFIGURATION
N/A
OS / ENVIRONMENT
N/A
STEPS TO REPRODUCE
Minimal test case:
EXPECTED RESULTS
Command should fail with result
ls: cannot access '$HOME': No such file or directory
ACTUAL RESULTS
https://gist.github.com/rfc1459/0f80f01d0c93e73f27dfdd64f0069b0c
The text was updated successfully, but these errors were encountered: