You can use playbook_dir
variable.
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.
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
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With