Retrieving a task file directory for relative paths

In my playbook, I ran local_action, which executes a script and saves the value for future use:

- local_action: command python release.py register: release_url 

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

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

I have a directory structure as shown below:

  • group_vars /
  • Roles /
    • ghost /
      • Tasks /
        • main.yml
      • Templates /
      • release.py
  • site.yml

However, the role / ghost folder name may change (and hard coding will prevent you from running the playbook from another directory).

How can I get the path to the /main.yml task directories (which calls the local_action command), so I can give a relative path to it?

+6
source share
3 answers

As with 1.8, there is a role_path variable defined that you can use for this purpose.

+5
source

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

 - name: local scrpt local_action: script release.py 

And so you may not need the directory path

This links to this in the layout section of the http://www.ansibleworks.com/docs/playbooks_best_practices.html directory

+2
source

I sent a transfer request before I saw this SO question, but here is its gist.

  • Move the script to the "files" directory in the role
  • Since we cannot know which path the script will be in, copy it to / tmp:

     - name: copy ghost release script to tmp local_action: copy src=release.py dest=/tmp/release.py sudo: no 
  • Then we can run the script

     - name: get ghost release version local_action: command python /tmp/release.py register: ghost_release sudo: no 

Choose sudo: no if the external game cup sets it globally to on . We do not need root for this action, and it can break the incomplete run if it needs to request a password.

There may also be a better way to do step 2 by randomizing the file name in some way, using the fact, and then deleting the file in an additional step after we run the script. This can minimize the risk that there could potentially be a file called /tmp/release.py .

0
source

Source: https://habr.com/ru/post/956014/


All Articles