Relative paths behaviour changes 1.9 => 2.0 => 2.1.2

172 views
Skip to first unread message

Steven Ringo

unread,
Nov 14, 2016, 7:37:26 PM11/14/16
to Ansible Project

Hi,


It seems like Ansible 2.1.2 and newer has changed path handling.


My observations are:

  • In 1.9.x it bases relative paths on the path of the playbook itself.
  • In 2.0.0 to 2.1.1 it bases relative paths on the path of where ansible-playbook is being executed from.
  • In 2.1.2 it reverts back to 1.9.x behaviour.

This affects, among others, the template module when using relative paths, as well as pwd from a shell command.


This seems like a pretty big change.


Am I doing something wrong? Which is correct?


To reproduce the observations:


Assuming the following directory tree, and a test.yml playbook:


.

├── ansible

│   ├── group_vars

│   ├── inventory

│   └── test.yml

└── other


test.yml:


- name: test

  hosts: localhost

  gather_facts: no

  tasks:

    - command: >

        pwd

      register: result

    - debug: var=result.stdout


Executing:


cd /projects/acme-corp/my-project/

ansible-playbook -i local, ./ansible/test.yml


In Ansible 2.1.1, I get:


TASK [debug] *******************************************************************

ok: [localhost] => {

    "result.stdout": "/projects/acme-corp/my-project"

}


In Ansible 2.1.2 and 1.9.0, I get:


TASK [debug] *******************************************************************

ok: [localhost] => {

    "result.stdout": "/projects/acme-corp/my-project/ansible"

}


Thanks,


Steve

Brian Coca

unread,
Nov 14, 2016, 8:03:51 PM11/14/16
to ansible...@googlegroups.com
First, not all paths are equal nor work the same way. The execution path, which is what you are getting when running `pwd`, depends on the connection plugin, it normally is the 'login directory' which for ssh is usually the remote $HOME. For most plugins this has not changed across versions.

The 'local' connection plugin (default for localhost/local_action) had a bug introduced in 2.0 in which the path changed from 'playbook directory' to invocation path, 2.1.2 fixed that bug. 

This is what you are seeing in your tests, which do not really apply to the template plugin.


The lookup and action plugins (like template and copy) have their own logic for finding paths (which has also had a few bugs). The way it is supposed to work is (given relative paths) to search based on role, include or playbook directory (more specific wins with fallback to parent). Also pre-pending 'files', 'vars' or 'templates' if needed and depending on the exact plugin.​ These are different than the 'execution path' detailed above.


----------
Brian Coca

Steven Ringo

unread,
Nov 14, 2016, 8:16:48 PM11/14/16
to Ansible Project
Hi Brian,

Thanks for the prompt reply.

For the template module, in 2.0.0-2.1.1, this worked:

- template:
    src:  templates/template-file.txt.j2
    dest: ./subdirectory/result.txt

For 2.1.2 and later I have had to change this to:

- template:
    src:  templates/template-file.txt.j2
    dest: ../subdirectory/result.txt

(Note the `..` as opposed to the `.` in the dest parameter)

Whether the cause is the same or different to `command: pwd`, the result seems to be the same.

Regardless, I now know that the 2.0 to 2.1.1 behaviour is buggy, and I should change playbooks to use the "fixed" behaviour.

Thanks so much for the help.

Steve

Brian Coca

unread,
Nov 14, 2016, 8:31:26 PM11/14/16
to ansible...@googlegroups.com
Ah, sorry, I seem to have misinterpreted the problem. The template patching i was referring to was for src=, for dest= you rely on 'execution pathing', which thankfully I also answered in the previous email.

src gets handled mainly by the action plugin, dest gets handled by the module of the same name, which does run relative to execution path.

----------
Brian Coca

Steven Ringo

unread,
Nov 14, 2016, 9:00:43 PM11/14/16
to Ansible Project
My bad. I should have been clearer.

Thanks for the support!
Reply all
Reply to author
Forward
0 new messages