Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a directory of a tasks file for relative paths

Tags:

ansible

In my playbook I run a local_action that executes a script and stores the value for future use:

- local_action: command python release.py
  register: release_url

This worked great, until I moved my playbook into a subdirectory. It turns out that the command runs from your current working directory (that you run ansible from), so the path needs to look like this:

- local_action: command python roles/ghost/release.py

I have a directory structure as below:

  • group_vars/
  • roles/
    • ghost/
      • tasks/
        • main.yml
      • templates/
      • release.py
  • site.yml

However the roles/ghost folder name can change (and hard-coding this won't let you run the playbook from another directory).

How can I get the directory path of tasks/main.yml (which issues the local_action command), so I can give a relative path to it?

like image 774
Ross Avatar asked Oct 15 '13 19:10

Ross


2 Answers

As of ansible 1.8, there's a role_path variable defined that you can use for this purpose.

like image 176
bgw Avatar answered Oct 03 '22 10:10

bgw


Can you put release.py script in roles/ghost/files/ and then use it with local_action

- name: local scrpt 
  local_action: script release.py

And that way you may not need to get the directory path

This makes reference to doing so in the Directory Layout part http://www.ansibleworks.com/docs/playbooks_best_practices.html

like image 40
Leonardo Ruiz Avatar answered Oct 03 '22 12:10

Leonardo Ruiz