15

I want to run an ansible playbook locally (host is localhost, like -i "localhost,") and inside the Playbook obtain the working directory from which the ansible-playbook command was invoked.

However, when I read out the current working directory in a playbook task, I get the directory of the playbook itself.

Is there any variable I can refer to to obtain the working directory on the controlling host (which is localhost) in my case?

I can inject via a variable -e but I would prefer to have a fall-back that if that variable is not set I can obtain it from the ([local]host) environment.

hakre
  • 253
  • 1
  • 3
  • 6

2 Answers2

20

You can use env lookup plugins. Plugins are always evaluated in the context of "parent" ansible process on the control host.

- debug:
    msg: "{{ lookup('env', 'PWD') }}"

More lookup plugins can be found here: http://docs.ansible.com/ansible/latest/user_guide/playbooks_lookups.html

Konstantin Suvorov
  • 1,123
  • 5
  • 8
-1

I'd use the shell module to run a simple pwd then use register to capture that

name: current_working_directory
    shell: pwd
    register: current_working_directory

you can then reuse this anywhere

{{  current_working_directory.stdout }}