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

<pre><code>name: current_working_directory
    shell: pwd
    register: current_working_directory
</code></pre>

<p>you can then reuse this anywhere </p>

<pre><code>{{  current_working_directory.stdout }}
</code></pre>

CC BY-SA 3.0
2018-03-28T14:25:38.013Z
As OP said this will return the playbook directory not the directory from which the command is executed.
storm
CC BY-SA 3.0
2018-03-28T14:40:52.537Z
Yes, this returns the path of the playbook which is available as variable as well: `playbook_dir`.
hakre
CC BY-SA 3.0
2018-03-28T14:22:30.953Z
2018-03-28T14:22:30.953Z
Hamid Elbagir
<p>You can use <code>env</code> lookup plugins. Plugins are always evaluated in the context of "parent" ansible process on the control host.</p>

<pre><code>- debug:
    msg: "{{ lookup('env', 'PWD') }}"
</code></pre>

<p>More lookup plugins can be found here: <a href="http://docs.ansible.com/ansible/latest/user_guide/playbooks_lookups.html" rel="noreferrer">http://docs.ansible.com/ansible/latest/user_guide/playbooks_lookups.html</a></p>

CC BY-SA 4.0
2019-11-08T20:03:54.470Z
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"`
GameSalutes
CC BY-SA 4.0
2020-06-02T04:06:34.270Z
This is also available thought fact gathering, as seen in https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variables-discovered-from-systems-facts. Need to make sure that setup has run.
Federico
CC BY-SA 3.0
2018-03-29T17:28:20.133Z
2018-04-01T10:17:54.027Z
2018-04-01T10:17:54.027Z
Konstantin Suvorov
<p>I want to run an ansible playbook locally (host is localhost, like <code>-i "localhost,"</code>) and inside the Playbook obtain the working directory from which the <code>ansible-playbook</code> command was invoked.</p>

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

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

<p>I can inject via a variable <code>-e</code> 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.</p>

CC BY-SA 3.0
2018-03-28T14:16:13.850Z
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
CC BY-SA 3.0
2018-03-28T14:44:55.297Z
currently going with someting similar to `-e project_dir="$(pwd -P)"` in a shell script, did hope to come away from that.
hakre
CC BY-SA 3.0
2018-03-28T14:47:21.147Z
I have to ask, but why you need that path into your playbook?
storm
CC BY-SA 3.0
2018-03-29T06:39:18.043Z
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
<ansible>
Ansible Playbook Localhost Invocation Working Directory
hakre
# Ansible Playbook Localhost Invocation Working Directory

(Asked by: username_0 on 2018-03-28)

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.

**Tags:** ansible

> **Comments:**
> **username_1**: 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)  
> **username_0**: currently going with someting similar to `-e project_dir="$(pwd -P)"` in a shell script, did hope to come away from that.  
> **username_1**: I have to ask, but why you need that path into your playbook?  
> **username_0**: 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.  

---

## 2 answers

### Answer by username_3 on 2018-03-29. Score: 20 | Accepted answer

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

> **Comments:**
> **username_4**: 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"`  
> **username_5**: This is also available thought fact gathering, as seen in https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variables-discovered-from-systems-facts. Need to make sure that setup has run.  

---

### Answer by username_2 on 2018-03-28. Score: -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 }}

> **Comments:**
> **username_1**: As OP said this will return the playbook directory not the directory from which the command is executed.  
> **username_0**: Yes, this returns the path of the playbook which is available as variable as well: `playbook_dir`.  

---

