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.

  • The only way I can think about is to pass $PWD with the -e option , your playbook is not supposed to know from where you run the command ( same as a shell script)
    – storm
    Mar 28, 2018 at 14:16
  • currently going with someting similar to -e project_dir="$(pwd -P)" in a shell script, did hope to come away from that.
    – hakre
    Mar 28, 2018 at 14:44
  • I have to ask, but why you need that path into your playbook?
    – storm
    Mar 28, 2018 at 14:47
  • I'd like to use Ansible for some local tasks, perhaps mainly b/c I'm lazy. Situation is I've got a battery of Git repositories all representing a project of some kind/role and I'd like to apply common operations on all of them over time and writing Ansible playbook(s) for such transitions is the idea. So instead of remote hosts I have "local" projects I'd like to manage, but perhaps it's even possible to make Ansible SSH into the Git server and take care on the remote side. But for the moment this is local. If I could obtain the working directory, the playbook could be directly applied.
    – hakre
    Mar 29, 2018 at 6:39

2 Answers 2


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

  • Exactly what I was looking for. This can be combined with a task to provide a current working directory for a file output. dest: "{{ lookup('env', 'PWD') }}/file.txt" Nov 8, 2019 at 20:03
  • 1
    This is also available thought fact gathering, as seen in docs.ansible.com/ansible/latest/user_guide/…. Need to make sure that setup has run.
    – Federico
    Jun 2, 2020 at 4:06

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 }}
  • 1
    As OP said this will return the playbook directory not the directory from which the command is executed.
    – storm
    Mar 28, 2018 at 14:25
  • 1
    Yes, this returns the path of the playbook which is available as variable as well: playbook_dir.
    – hakre
    Mar 28, 2018 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.