# Local path to a file within a role

**URL:** <https://forum.ansible.com/t/local-path-to-a-file-within-a-role/15585>\
**Category:** Ansible Project\
**Created:** [July 1, 2014, 3:45pm UTC](https://forum.ansible.com/t/local-path-to-a-file-within-a-role/15585 "2014-07-01T15:45:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jon\_Warbrick](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@Jon\_Warbrick](https://forum.ansible.com/u/Jon_Warbrick)\
**Post date:** [July 1, 2014, 3:45pm UTC](https://forum.ansible.com/t/local-path-to-a-file-within-a-role/15585/1 "2014-07-01T15:45:41Z")

</div>

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:

&nbsp;&nbsp;&nbsp;{{ 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.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 1, 2014, 6:51pm UTC](https://forum.ansible.com/t/local-path-to-a-file-within-a-role/15585/2 "2014-07-01T18:51:09Z")

</div>

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](http://docs.ansible.com/test_strategies.html) for some notes about stage environments for example

---

<div class="post-metadata">

**Author:** ![mhael](https://avatars.discourse-cdn.com/v4/letter/m/919ad9/32.png) [@mhael](https://forum.ansible.com/u/mhael)\
**Post date:** [July 1, 2014, 8:56pm UTC](https://forum.ansible.com/t/local-path-to-a-file-within-a-role/15585/3 "2014-07-01T20:56:17Z")

</div>

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 }}”

`

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 1, 2014, 10:12pm UTC](https://forum.ansible.com/t/local-path-to-a-file-within-a-role/15585/4 "2014-07-01T22:12:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jon\_Warbrick](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@Jon\_Warbrick](https://forum.ansible.com/u/Jon_Warbrick)\
**Post date:** [July 12, 2014, 12:38pm UTC](https://forum.ansible.com/t/local-path-to-a-file-within-a-role/15585/5 "2014-07-12T12:38:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jon\_Warbrick](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@Jon\_Warbrick](https://forum.ansible.com/u/Jon_Warbrick)\
**Post date:** [July 12, 2014, 12:49pm UTC](https://forum.ansible.com/t/local-path-to-a-file-within-a-role/15585/6 "2014-07-12T12:49:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 12, 2014, 12:54pm UTC](https://forum.ansible.com/t/local-path-to-a-file-within-a-role/15585/7 "2014-07-12T12:54:44Z")

</div>

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