ansible

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


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


Solution

  • 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