168

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/.

0

7 Answers 7

286

You can use the playbook_dir variable.

See the documentation about special variables.


For example, given the file structure:

.
├── foo
│   └── bar.txt
└── playbook.yml

When running playbook.yml, the task:

- ansible.builtin.debug:
    var: "(playbook_dir ~ '/foo/bar.txt') is file"

Would give:

TASK [ansible.builtin.debug] **************************************
ok: [localhost] => 
  (playbook_dir ~ '/foo/bar.txt') is file: true
0
65
+50

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.

0
5

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.

5

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

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
    Commented Nov 20, 2015 at 0: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
    Commented Jun 7, 2016 at 8:19
  • 9
    Alternatively the current directory can be found with: {{ lookup('env','PWD') }}
    – isedwards
    Commented May 12, 2017 at 10:25
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?

3
  • Thanks Bruce, I modified my question to elaborate on what I was trying to do.
    – Josh Unger
    Commented Jun 16, 2015 at 3:43
  • 9
    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
    Commented Nov 20, 2015 at 0:43
  • your command didn't work for me. This one did: ansible -m setup -i 127.0.0.1, localhost Commented Aug 31, 2022 at 5: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
0

This will get the job done:

- set_fact:
    playbook_name: "{{ (lookup('file', '/proc/self/cmdline')) }}"
  tags: always

- set_fact:
    cmdline_parts: "{{ playbook_name.split('\u0000') }}"
  tags: always

- set_fact:
    extracted_playbook_name: "{{ inventory_dir }}/{{ cmdline_parts[4] | default('No playbook found') }}"
  tags: always

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.