unpredictable 'working directory' on local machine

Hi,

I have found that ansible is unpredictable in terms of determining the current working directory on the LOCAL machine.
Invoking the same shell command as a lookup, or as a command, changes the apparent working directory.

Is this expected behaviour?

e.g. for the playbook given below, and invoked as follows:

~/projectdir $ ansible-playbook -i inventory ~/automation/ansible/playbook.yaml

vars:
inifile: “{{ lookup(‘pipe’,‘locate_config.py --inifile’) }}”

runs from the location of the current playbook
i.e. ~/automation/ansible/playbook.yaml

However,

tasks:

  • name: Get ini properties file
    action: command locate_config.py --inifile
    register: gce_project_ini

runs from the location from which the playbook was invoked
i.e. ~/projectdir

‘locate_config.py --inifile’ is a script that outputs the path to a uniquely-named INI file, specific to the project (but could be a call to pwd)

I want to have the lookup command use the latter ~/projectdir location as its pwd.

Any advice or suggestions gratefully received.

The Playbook is below

  • name: Gather GCE info from localhost
    hosts: localhost
    environment:
    PATH: “{{ lookup(‘env’, ‘PATH’) }}”
    vars:
    inifile: “{{ lookup(‘pipe’,‘locate_config.py --inifile’) }}”

tasks:

  • name: Get ini properties file
    action: command locate_config.py --inifile
    register: gce_project_ini

  • debug: msg=“infile={{ inifile }}”

  • debug: msg=“gce_project_ini={{ gce_project_ini.stdout_lines }}”

Follow us on:

Appsbroker Consulting Limited, Registered office: Appsbroker House, The Square, Swindon,

SN1 3EB, Company Number: 5702796, VAT Number: GB 876 3533 92, Company registered in England and Wales.

just tested this against 2.0:

  • hosts: localhost
    gather_facts: False
    vars:
    third: “{{lookup(‘pipe’, ‘pwd’)}}”
    tasks:

  • shell: pwd
    register: cwd

  • debug: msg={{cwd}}

  • debug: msg={{lookup(‘pipe’, ‘pwd’)}}

  • debug: msg={{third}}

and all 3 give the same result.

Sorry to be pedantic, but did you try e.g.

~/projectdir $ ansible-playbook ~/automation/ansible/playbook.yaml

or just

ansible-playbook playbook.yaml

On two separate installations both ansible 2.0.0, I get the behaviour I have described, when the former is used

That is, pwd reports one location for the lookup calls (2nd & third in your example report location of playbook) and a different location for the shell task (location of ansible invocation)
WHEN the playbook and the invocation are in different locations.

Any chance you could check again?

Cheers

Jonathan

I see what you mean now, but not unpredictable. Lookups are always relative to the role or play.

Local tasks are relative to the cwd from which you execute.

Thanks, that is excellent info.
I have not found this documented anywhere.