Variable that has the path to the current ansible-playbook that is executing?
AnsibleAnsible Problem Overview
Is there an ansible variable that has the absolute path to the current ansible-playbook that is executing?
some context:
I'm running/creating an ansible script against localhost
to configure a mysql docker and wanting to mount the data volume relative to the ansible script.
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/
.
Ansible Solutions
Solution 1 - Ansible
You can use playbook_dir
variable.
Solution 2 - Ansible
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.
Solution 3 - Ansible
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
Solution 4 - Ansible
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.
Solution 5 - Ansible
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?