Local path to a file within a role

Apologies for what may be a silly question.

Within a role, I'd like to run a verification command on some files from the .../files/ directory before installing them. I can see how to use a 'local_action' and the command or shell module for this. But I can't see how to give the verification command an appropriate path to the file to process. The best I can come up with is a solution based on playbook_dir:

   {{ playbook_dir }}/roles/foobar-config/files/{{ item }}

But this is brittle, because it includes the role name which might change. Is there a better way to do this?

Jon.

This is what we have right now, there isn’t a role variable that says what’s the path of the current role, because of the way roles are expanded, can have dependencies, and so on.

I don’t suspect your role name will change any, so it shouldn’t be much of a problem - if it does, the validation step would fail, which is a good reason to test playbooks anyway.

See http://docs.ansible.com/test_strategies.html for some notes about stage environments for example

Hi Jon,

There’s actually a way (hack?) to get role directory by using lookup. Define ‘role_dir’ var as shown below and you can use it in your roles’ tasks files to refer to the current role directory:

role_dir: "{{ lookup('pipe','pwd')|dirname }}"

Just note, this works as long as the tasks file (or included fragment) using ‘role_dir’ variable is located in the role’s ‘tasks’ dir. Doesn’t work if used in the fragment included from outside.

Also, role name can be obtained in the same way:

`
role_name: “{{ lookup(‘pipe’,‘pwd’)|dirname|basename }}”

`

Just note that if you have --forks 200 that “pwd” will get executed 200 times at once, which might make your system unhappy.

Belated thanks for that - I'll stick with my current approach, though I'm now wondering if role dependencies, etc, may mean even that won't always work.

Jon.

So this assumes that, when the role_dir variable is expanded, the current directory will be one inside the relevant role directory? That's not what I thought I saw, but perhaps I was wrong.

But if that *is* the case, surely I could use relative paths to construct paths to any other files in the role and bypass the need to run 'pwd'?

I fear I'm missing something...

Jon.

“So this assumes that, when the role_dir variable is expanded, the current directory will be one inside the relevant role directory? That’s not what I thought I saw, but perhaps I was wrong.”

Might be helpful to share what you saw, showing both the actual vs expected values, and playbook snippets.

I’m not sure I understand the pwd issue but yes “/…/” and the like do work in Ansible.