Skip to content
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

File: module fails when path contains $HOME (or other env variables) in the name #25005

Closed
steve-gregory opened this issue May 24, 2017 · 0 comments · Fixed by #27137
Closed
Assignees
Labels
affects_2.3 This issue/PR affects Ansible v2.3 bug This issue/PR relates to a bug. module This issue/PR relates to a module. P2 Priority 2 - Issue Blocks Release support:core This issue/PR relates to code supported by the Ansible Engineering Team.

Comments

@steve-gregory
Copy link

ISSUE TYPE
  • Bug Report
COMPONENT NAME

file module

ANSIBLE VERSION
ansible 2.3.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4]
OS / ENVIRONMENT

Ubuntu 14.04

SUMMARY

IF a path includes a "known envrionment variable" (For example, $HOME) as a file/directory (i.e. /tmp2/$HOME/my_file.txt), then ansible will evaluate it (rather than treating it as a literal), resulting in /tmp2//root/my_file.txt which produces an OSError - No such file/directory.

STEPS TO REPRODUCE

1. Create a directory and some files:

mkdir /tmp2
mkdir /tmp2/\$HOME
touch /tmp2/\$HOME/myfile.txt

ll /tmp2/
# drwxrwxr-x  3 www-data www-data 4096 May 24 13:47 ./
# drwxr-xr-x 26 root     root     4096 May 24 13:46 ../
# drwxr-xr-x  2 root     root     4096 May 24 13:47 $HOME/
ll /tmp2/\$HOME/
# drwxr-xr-x 2 root     root     4096 May 24 13:47 ./
# drwxrwxr-x 3 root  root 4096 May 24 13:47 ../
# -rw-r--r-- 1 root     root        0 May 24 13:47 myfile.txt

#2. Create a new role with one task:
(Assumes a user:group named www-data exists)

 - name: Test ansible issue with path
+  file: path="/tmp2" owner=www-data group=www-data mode="g+rw" recurse=yes
EXPECTED RESULTS
# chown -R www-data:www-data /tmp2
ll /tmp2/\$HOME/
# drwxr-xr-x 2 www-data www-data 4096 May 24 13:47 ./
# drwxrwxr-x 3 www-data www-data 4096 May 24 13:47 ../
# -rw-r--r-- 1 www-data www-data    0 May 24 13:47 myfile.txt
ACTUAL RESULTS
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: root
<localhost> EXEC /bin/sh -c '/usr/bin/python && sleep 0'
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_rLV5HB/ansible_module_file.py", line 480, in <module>
    main()
  File "/tmp/ansible_rLV5HB/ansible_module_file.py", line 362, in main
    changed |= recursive_set_attributes(module, to_bytes(file_args['path'], errors='surrogate_or_strict'), follow, file_args)
  File "/tmp/ansible_rLV5HB/ansible_module_file.py", line 171, in recursive_set_attributes
    changed |= module.set_fs_attributes_if_different(tmp_file_args, changed)
  File "/tmp/ansible_rLV5HB/ansible_modlib.zip/ansible/module_utils/basic.py", line 1292, in set_fs_attributes_if_different
  File "/tmp/ansible_rLV5HB/ansible_modlib.zip/ansible/module_utils/basic.py", line 998, in set_owner_if_different
  File "/tmp/ansible_rLV5HB/ansible_modlib.zip/ansible/module_utils/basic.py", line 909, in user_and_group
OSError: [Errno 2] No such file or directory: '/tmp2//root'

fatal: [localhost]: FAILED! => {
    "changed": false,
    "failed": true,
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_rLV5HB/ansible_module_file.py\", line 480, in <module>\n    main()\n  File \"/tmp/ansible_rLV5HB/ansible_module_file.py\", $
ine 362, in main\n    changed |= recursive_set_attributes(module, to_bytes(file_args['path'], errors='surrogate_or_strict'), follow, file_args)\n  File \"/tmp/ansible_rLV5HB/ansible_module_file.py\", lin$
 171, in recursive_set_attributes\n    changed |= module.set_fs_attributes_if_different(tmp_file_args, changed)\n  File \"/tmp/ansible_rLV5HB/ansible_modlib.zip/ansible/module_utils/basic.py\", line 1292$
 in set_fs_attributes_if_different\n  File \"/tmp/ansible_rLV5HB/ansible_modlib.zip/ansible/module_utils/basic.py\", line 998, in set_owner_if_different\n  File \"/tmp/ansible_rLV5HB/ansible_modlib.zip/an
sible/module_utils/basic.py\", line 909, in user_and_group\nOSError: [Errno 2] No such file or directory: '/tmp2//root'\n",
    "module_stdout": "",
    "msg": "MODULE FAILURE",
    "rc": 1
}
@ansibot ansibot added affects_2.3 This issue/PR affects Ansible v2.3 bug_report module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. labels May 24, 2017
@jctanner jctanner added P2 Priority 2 - Issue Blocks Release and removed needs_triage Needs a first human triage before being processed. labels May 26, 2017
@ansibot ansibot added the support:core This issue/PR relates to code supported by the Ansible Engineering Team. label Jun 29, 2017
@mckerrj mckerrj removed the P2 Priority 2 - Issue Blocks Release label Aug 1, 2017
@mckerrj mckerrj added the P2 Priority 2 - Issue Blocks Release label Aug 1, 2017
andreaso added a commit to andreaso/ansible that referenced this issue Aug 8, 2017
Since the module's path parameter is of the AnsibleModule type path
it's already being expanded. Hence no need to have the
set_fs_attributes_if_different method do its own expand.

This additional expand is an actual problem when the file module runs
recursively, as real existing file names can be mistakenly expanded to
something completely wrong and non-existing.

Fixes ansible#25005
Fixes ansible#25639
abadger pushed a commit that referenced this issue Aug 8, 2017
Since the module's path parameter is of the AnsibleModule type path
it's already being expanded. Hence no need to have the
set_fs_attributes_if_different method do its own expand.

This additional expand is an actual problem when the file module runs
recursively, as real existing file names can be mistakenly expanded to
something completely wrong and non-existing.

Fixes #25005
Fixes #25639
abadger pushed a commit that referenced this issue Aug 8, 2017
Since the module's path parameter is of the AnsibleModule type path
it's already being expanded. Hence no need to have the
set_fs_attributes_if_different method do its own expand.

This additional expand is an actual problem when the file module runs
recursively, as real existing file names can be mistakenly expanded to
something completely wrong and non-existing.

Fixes #25005
Fixes #25639

(cherry picked from commit ccc68f7)
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 7, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.3 This issue/PR affects Ansible v2.3 bug This issue/PR relates to a bug. module This issue/PR relates to a module. P2 Priority 2 - Issue Blocks Release support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants