Can't understand Ansible 2.0 paths logic

Hi everyone,

Since upgrade Ansible 2.0, I have a lot of problems with path finding in modules like template or file.

Can someone explain me the logic to have this :

  • name: Create temporary folder
    local_action: file path=templates/_tmp state=directory

  • name: Get files to process template
    fetch: src=/var/www/test.php dest=…/templates/_tmp/test.php flat=yes

  • name: Process template
    template: src=…/templates/_tmp/test.php dest=/var/www/test.php

  • name: Remove temporary folder
    local_action: file path=templates/_tmp state=absent

Sometimes we need to specify “…/” to go to project template’s directory, sometimes not. Where is the logic ?

In this example, “templates/” and “…/templates/” are referring to the SAME directory !

With Ansible 1.9, all was working fine with “…/templates/” everywhere !

We have a lot of changes like this to do, that’s annoying.

That’s very strange.

Florent

You are conflating 2 different systems for ‘relative path resolution’:

The first applies to ‘remote actions’ run from ‘cwd’ on the target machine, normally the home dir of the login user. But with local action translates to the directory from which you invoked Ansible. This is what file is using, it is hard to predict this from the play as I show below.

The second applies only to some plugins that use ‘master’ as a source (via action plugins) like template/copy, etc follow a set of paths like this:
( can be files/ vars/ or templates/ depending on plugin)

if inrole:
role_dir//
role_dir/tasks/
role_dir/

play_dir//
play_dir/

In 1.9 this was somewhat broken and it pathed across this tree multiple times and in 1.9.4 it even included ‘cwd’, but the above is the way this was always meant to work as you can execute Ansible from anywhere (i.e /tmp, /etc/, /dev/) and you cannot construct a playbook predictably that way.

So I recommend you rewrite the file and fetch tasks to use role_path or playbook_dir variables to use a predictable path, otherwise if you execute from a different directory you will start having issues again.

Relying on cwd makes these all these commands work differently:
ansible-playbook …/path/to/plays/play.yml

ansible-playbook ./play.yml

ansible-playbook plays/play.yml