# unpredictable 'working directory' on local machine

**URL:** <https://forum.ansible.com/t/unpredictable-working-directory-on-local-machine/19626>\
**Category:** Ansible Project\
**Created:** [October 27, 2015, 11:07am UTC](https://forum.ansible.com/t/unpredictable-working-directory-on-local-machine/19626 "2015-10-27T11:07:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan\_Miller](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@Jonathan\_Miller](https://forum.ansible.com/u/Jonathan_Miller)\
**Post date:** [October 27, 2015, 11:07am UTC](https://forum.ansible.com/t/unpredictable-working-directory-on-local-machine/19626/1 "2015-10-27T11:07:52Z")

</div>

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 }}”

![](https://lh3.googleusercontent.com/-O_BhwkiBPC4/VAnKq6opisI/AAAAAAAAAZ4/Vm7O5YNtMTE/w975-h137-no/signature_final_2015-780.jpg)

Follow us on: [![](https://lh3.googleusercontent.com/-F6SOUlnCxMA/VAh6lUYygmI/AAAAAAAAAX8/RGnnsZ9Os70/s38-no/googleplus.png)](https://plus.google.com/+Appsbroker/)[![](https://lh5.googleusercontent.com/-ENtYeC0inGs/VAh6lsp7fjI/AAAAAAAAAYE/XnOjQ8CkUvA/s38-no/twitter.png)](https://twitter.com/appsbroker)[![](https://lh5.googleusercontent.com/-qntzbd-rp7k/VAh6lrlzY7I/AAAAAAAAAYA/J9kMb1WHg4o/s38-no/linkedin.png)](http://www.linkedin.com/company/appsbroker-consulting-limited/careers?trk=top_nav_careers)

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.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [October 27, 2015, 12:17pm UTC](https://forum.ansible.com/t/unpredictable-working-directory-on-local-machine/19626/2 "2015-10-27T12:17:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jonathan\_Miller](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@Jonathan\_Miller](https://forum.ansible.com/u/Jonathan_Miller)\
**Post date:** [October 27, 2015, 2:43pm UTC](https://forum.ansible.com/t/unpredictable-working-directory-on-local-machine/19626/3 "2015-10-27T14:43:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [October 27, 2015, 4:34pm UTC](https://forum.ansible.com/t/unpredictable-working-directory-on-local-machine/19626/4 "2015-10-27T16:34:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jonathan\_Miller](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@Jonathan\_Miller](https://forum.ansible.com/u/Jonathan_Miller)\
**Post date:** [October 30, 2015, 10:55am UTC](https://forum.ansible.com/t/unpredictable-working-directory-on-local-machine/19626/5 "2015-10-30T10:55:52Z")

</div>

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