Answers: <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>
  
Answers: CC BY-SA 3.0
Answers: 2018-03-28T14:25:38.013Z
Answers: As OP said this will return the playbook directory not the directory from which the command is executed.
Answers: storm
Answers: CC BY-SA 3.0
Answers: 2018-03-28T14:40:52.537Z
Answers: Yes, this returns the path of the playbook which is available as variable as well: `playbook_dir`.
Answers: hakre
Answers: CC BY-SA 3.0
Answers: 2018-03-28T14:22:30.953Z
Answers: 2018-03-28T14:22:30.953Z
Answers: Hamid Elbagir
Answers: <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>
  
Answers: CC BY-SA 4.0
Answers: 2019-11-08T20:03:54.470Z
Answers: 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"`
Answers: GameSalutes
Answers: CC BY-SA 4.0
Answers: 2020-06-02T04:06:34.270Z
Answers: 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.
Answers: Federico
Answers: CC BY-SA 3.0
Answers: 2018-03-29T17:28:20.133Z
Answers: 2018-04-01T10:17:54.027Z
Answers: 2018-04-01T10:17:54.027Z
Answers: Konstantin Suvorov
Body: <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>
  
Comments: CC BY-SA 3.0
Comments: 2018-03-28T14:16:13.850Z
Comments: 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)
Comments: storm
Comments: CC BY-SA 3.0
Comments: 2018-03-28T14:44:55.297Z
Comments: currently going with someting similar to `-e project_dir="$(pwd -P)"` in a shell script, did hope to come away from that.
Comments: hakre
Comments: CC BY-SA 3.0
Comments: 2018-03-28T14:47:21.147Z
Comments: I have to ask, but why you need that path into your playbook?
Comments: storm
Comments: CC BY-SA 3.0
Comments: 2018-03-29T06:39:18.043Z
Comments: 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.
Comments: hakre
Tags: <ansible>
Title: Ansible Playbook Localhost Invocation Working Directory
UserDisplayName: hakre
ThreadText: # 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`.  
  
  ---
  
  