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

The tilde expansion doesn't work with templates #8949

Closed
romainrichard opened this issue Sep 9, 2014 · 3 comments
Closed

The tilde expansion doesn't work with templates #8949

romainrichard opened this issue Sep 9, 2014 · 3 comments
Labels
bug This issue/PR relates to a bug.

Comments

@romainrichard
Copy link
Contributor

Issue Type:

Bug Report

Ansible Version:

1.6.3 and 1.7.1

Environment:

Ubuntu 14.04

Summary:

When inside a template, the tilde expansion doesn't seem to work correctly.

Steps To Reproduce:

Here's the contents of the playbook:

romain:~/workspace/it_ansible_check_issue $ tree
.
├── copy.yml
└── roles
    └── copy
        ├── tasks
        │   └── main.yml
        └── templates
            └── template.j2

4 directories, 3 files
romain:~/workspace/it_ansible_check_issue $ cat copy.yml 

---
- name: copy template to servers
  hosts: all
  roles:
    - copy
romain:~/workspace/it_ansible_check_issue $ cat roles/copy/tasks/main.yml 
# copy/tasks/main.yml

---
- name: copy template to host (tilde)
  template: >
    src=template.j2
    dest=~romain/template_tilde

- name: copy template to host (slash)
  template: >
    src=template.j2
    dest=/home/romain/template_slash
romain:~/workspace/it_ansible_check_issue $ cat roles/copy/templates/template.j2 
test

Here's what happens when running the playbook without --check:

romain:~/workspace/it_ansible_check_issue $ ansible-playbook copy.yml -i localhost,

PLAY [copy template to servers] *********************************************** 

GATHERING FACTS *************************************************************** 
ok: [localhost]

TASK: [copy | copy template to host (tilde)] ********************************** 
ok: [localhost]

TASK: [copy | copy template to host (slash)] ********************************** 
ok: [localhost]

PLAY RECAP ******************************************************************** 
localhost                  : ok=3    changed=0    unreachable=0    failed=0

Here's what happens when running the playbook with --check:

romain:~/workspace/it_ansible_check_issue $ ansible-playbook copy.yml -i localhost, --check

PLAY [copy template to servers] *********************************************** 

GATHERING FACTS *************************************************************** 
ok: [localhost]

TASK: [copy | copy template to host (tilde)] ********************************** 
changed: [localhost]

TASK: [copy | copy template to host (slash)] ********************************** 
ok: [localhost]

PLAY RECAP ******************************************************************** 
localhost                  : ok=3    changed=1    unreachable=0    failed=0
Expected Results:

Running the playbook with or without the --check option shows the same number of changes.

Actual Results:

Running the playbook with --check option shows changes that don't happen when the playbook is run without it.

@jimi-c jimi-c added P3 labels Sep 11, 2014
@jimi-c jimi-c changed the title --check shows changes that won't actually happen --check shows changes that won't actually happen with authorized_key Sep 11, 2014
@romainrichard romainrichard changed the title --check shows changes that won't actually happen with authorized_key The tilde expansion doesn't work correctly when used with --check Sep 16, 2014
@skmoen
Copy link

skmoen commented Sep 16, 2014

Ok, so what is happening is the template module/plugin is failing to expand the tilde when it connects to the remote to md5 the file, and therefore (in both the normal and --check case) it is assuming the file does not exist on the remote and is acting accordingly. In the normal case it is irrelevant because the copy module appears to still be doing the right thing (eg, it tries to do the file copy, and returns the correct changed based on whether the file was updated). In the case of --check it automatically assumes it is changed (since it thinks there is no file on the remote) and returns changed=True.

playbook:

- name: copy authorized key template to host
  template: >
    src=authorized_keys.j2
    dest=~{{key_user}}/.ssh/authorized_keys

action_plugins/template.py:

    local_md5 = utils.md5s(resultant)
    remote_md5 = self.runner._remote_md5(conn, tmp, dest)  # returns '1', file not found
    if local_md5 != remote_md5:
        ....
        if self.runner.noop_on_check(inject):
            return ReturnData(conn=conn, comm_ok=True, result=dict(changed=True), diff=dict(before_header=dest, after_header=source, before=dest_contents, after=resultant))
        else:
            res = self.runner._execute_module(conn, tmp, 'copy', module_args_tmp, inject=inject, complex_args=complex_args)

As best I can tell, the path isn't being expanded because the path string is being escaped prior to forming the command to md5 the file here:

shell_plugins/sh.py:

    def md5(self, path):
        path = pipes.quote(path)  # this quotes the path string, preventing the tilde from being expanded

If I comment out the line that quotes the path, then everything works fine. Ideally we'd use something like os.path.expanduser to interpret the tilde, but this code is running on the deploy box, not the remote, so that won't work.

Is it safe to just not quote the path? I'm sure it's there for security reasons. Do we need to implement something more fancy to either determine the expanded path ahead of time or approach the md5 process differently altogether?

@skmoen
Copy link

skmoen commented Sep 17, 2014

FYI we are currently working around this by using the home field from the user module.

- user: user={{key_user}}
  register: user_info

- template: >
    src=authorized_keys.j2
    dest={{user_info.home}}/.ssh/authorized_keys

@romainrichard romainrichard changed the title The tilde expansion doesn't work correctly when used with --check The tilde expansion doesn't work templates Sep 17, 2014
@romainrichard romainrichard changed the title The tilde expansion doesn't work templates The tilde expansion doesn't work with templates Sep 17, 2014
@mpdehaan
Copy link
Contributor

Hi!

Thanks very much for your interest in Ansible. It sincerely means a lot to us.

On September 26, 2014, due to enormous levels of contribution to the project Ansible decided to reorganize module repos, making it easier
for developers to work on the project and for us to more easily manage new contributions and tickets.

We split modules from the main project off into two repos, http://github.com/ansible/ansible-modules-core and http://github.com/ansible/ansible-modules-extras

If you would still like this ticket attended to, we will need your help in having it reopened in one of the two new repos, and instructions are provided below.

We apologize that we are not able to make this transition happen seamlessly, though this is a one-time change and your help is greatly appreciated --
this will greatly improve velocity going forward.

Both sets of modules will ship with Ansible, though they'll receive slightly different ticket handling.

To locate where a module lives between 'core' and 'extras'

Additionally, should you need more help with this, you can ask questions on:

Thank you very much!

@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 6, 2018
@ansible ansible locked and limited conversation to collaborators Apr 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue/PR relates to a bug.
Projects
None yet
Development

No branches or pull requests

5 participants