160

Is there an Ansible variable that has the absolute path to the current playbook that is executing?

Some context: I'm running/creating an Ansible script against localhost to configure a MySQL Docker container and wanting to mount the data volume relative to the Ansible playbook.

For example, let's say I've checkout a repository to ~/branch1/ and then I run ansible-playbook dev.yml I was thinking it should save the volume to ~/branch1/.docker_volume/. If I ran it from ~/branch2 then it should configure the volume to ~/branch2/.docker_volume/.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Josh Unger
  • 6,717
  • 6
  • 33
  • 55

6 Answers6

274

You can use the playbook_dir variable.

See the documentation about magic variables.

Zeitounator
  • 38,476
  • 7
  • 53
  • 66
Seva Poliakov
  • 2,961
  • 1
  • 13
  • 4
  • 2
    I was facing a similar issue and your answer solved it. Thanks. However, I could not find any place where `playbook_dir` variable is documented. Could you point me to the documentation please. OR, could you explain how did you come to know about this variable? – slayedbylucifer Mar 21 '16 at 12:03
  • 3
    I find it in ansible code: https://github.com/ansible/ansible/blob/a183972477de03c8f924525135908d4db258d44f/lib/ansible/vars/hostvars.py#L31 – Seva Poliakov Mar 23 '16 at 20:12
  • 4
    ALSO, there is interesting repo https://github.com/lorin/ansible-quickref with all variables. – Seva Poliakov Mar 24 '17 at 22:31
  • Can you provide an example? – alex Dec 18 '20 at 16:21
  • 4
    @slayedbylucifer https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html it is listed in the magic variables section – rkoller Feb 13 '21 at 18:11
63

There don't seem to be a variable which holds exactly what you want.

However, quoting the docs:

Also available, inventory_dir is the pathname of the directory holding Ansible’s inventory host file, inventory_file is the pathname and the filename pointing to the Ansible’s inventory host file.

playbook_dir contains the playbook base directory.

And finally, role_path will return the current role’s pathname (since 1.8). This will only work inside a role.

Dependent on your setup, those or the $ pwd -based solution might be enough.

Alecto Irene Perez
  • 10,321
  • 23
  • 46
Cray
  • 2,396
  • 19
  • 29
4

There is no build-in variable for this purpose, but you can always find out the playbook's absolute path with "pwd" command, and register its output to a variable.

- name: Find out playbook's path
  shell: pwd
  register: playbook_path_output
- debug: var=playbook_path_output.stdout

Now the path is available in variable playbook_path_output.stdout

iomv
  • 2,409
  • 18
  • 28
hyt
  • 73
  • 3
  • 11
    In this case `pwd` does not return the path of the playbook script. It returns the current directory of the process (normally `ansible-playbook`). The two don't have to be the same and depend on exact way in which ansible was invoked. – Cray Nov 20 '15 at 00:28
  • 7
    @Cray is correct. Irregardless, I just wanted to mention that Ansible already prepares this variable for you: `debug: var=ansible_env.PWD` (this gives me the directory from which I executed `ansible-playbook` on my local machine and the home directory on remote servers) – dutoitns Jun 07 '16 at 08:19
  • 6
    Alternatively the current directory can be found with: `{{ lookup('env','PWD') }}` – isedwards May 12 '17 at 10:25
3

I was using a playbook like this to test my roles locally:

---
- hosts: localhost
  roles:
     - role: .

but this stopped working with Ansible v2.2.

I debugged the aforementioned solution of

---
- hosts: all
  tasks:
    - name: Find out playbooks path
      shell: pwd
      register: playbook_path_output
    - debug: var=playbook_path_output.stdout

and it produced my home directory and not the "current working directory"

I settled with

---
- hosts: all
  roles:
    - role: '{{playbook_dir}}'

per the solution above.

2

Unfortunately there isn't. In fact the absolute path is a bit meaningless (and potentially confusing) in the context of how Ansible runs. In a nutshell, when you invoke a playbook then for each task Ansible physically copies the module associated with the task to a temporary directory on the target machine and then invokes the module with the necessary parameters. So the absolute path on the target machine is just a temporary directory that only contains a few temporary files within it, and it doesn't even include the full playbook. Also, knowing a full path of a file on the Ansible server is pretty much useless on a target machine unless you're replicating your entire Ansible directory tree on the targets.

To see all the variables that are defined by Ansible you can simply run the following command:

$ ansible -m setup hostname

What is the reason you think you need to know the absolute path to the playbook?

Bruce P
  • 19,995
  • 8
  • 63
  • 73
  • Thanks Bruce, I modified my question to elaborate on what I was trying to do. – Josh Unger Jun 16 '15 at 03:43
  • 8
    The absolute local path is not meaningless at all. For example it could be used to run any local commands and/or query other local files or services which could not be included in the ansible config directly. The mere fact that ansible has the `local_action` module means that there are legitimate use-cases for this. – Cray Nov 20 '15 at 00:43
  • your command didn't work for me. This one did: `ansible -m setup -i 127.0.0.1, localhost` – René Pardon Aug 31 '22 at 05:33
0

I use in playbook like this,

---

- hosts: test-server
  remote_user: root
  tasks:
    - name: Test output
      shell: 'pwd'
      register: command_output
      args:
        chdir: /root/new-folder/  # <<<<<<<<<< there
    - debug:
        var: command_output.stdout_lines