Variable that has the path to the current ansible-playbook that is executing?

204.7k Views Asked by At

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

6

There are 6 best solutions below

0
On BEST ANSWER

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
3
On

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?

0
On

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
On

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.

0
On

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
3
On

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