# Getting top level directory when using roles

**URL:** <https://forum.ansible.com/t/getting-top-level-directory-when-using-roles/13542>\
**Category:** Ansible Project\
**Created:** [September 24, 2013, 9:40pm UTC](https://forum.ansible.com/t/getting-top-level-directory-when-using-roles/13542 "2013-09-24T21:40:08Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jonathan\_Rowlands](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@Jonathan\_Rowlands](https://forum.ansible.com/u/Jonathan_Rowlands)\
**Post date:** [September 24, 2013, 9:40pm UTC](https://forum.ansible.com/t/getting-top-level-directory-when-using-roles/13542/1 "2013-09-24T21:40:08Z")

</div>

So I have a playbook that installs [gitlab](https://github.com/jgrowl/ansible-playbook-gitlab). The playbook includes a role that optionally installs ssl certificates. The ssl certificates are obviously something I want to be customizable. Is there a better way to get the path than breaking out of the role using …/ ?

[I am doing this:](https://github.com/jgrowl/ansible-playbook-gitlab/blob/5-4-stable/.private.template/vars/private.yml):

> ```auto
> 
> ssl_certs_src_glob: ../../../.private/files/null/ssl/certs/*
> ssl_keys_src_glob: ../../../.private/files/null/ssl/keys/*
> 
> ```

It works, I was just wondering if there was a cleaner way.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 25, 2013, 12:45am UTC](https://forum.ansible.com/t/getting-top-level-directory-when-using-roles/13542/2 "2013-09-25T00:45:11Z")

</div>

Built-in variables like “inventory\_dir” can give you the location of the inventory file.

[http://www.ansibleworks.com/docs/playbooks2.html#id7](http://www.ansibleworks.com/docs/playbooks2.html#id7)

There is no current directory to signify the playbook base directory, however (patches would be welcome!)

For now, I would likely avoid repeating yourself by defining something like:

ssl\_dir: …/…/.private  
ssl\_certs\_src\_glob: “{{ ssl\_dir }}/files/null/ssl/certs/\*”

That way you don’t have to repeat the “…/” everywhere.

---

<div class="post-metadata">

**Author:** ![Jonathan\_Rowlands](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@Jonathan\_Rowlands](https://forum.ansible.com/u/Jonathan_Rowlands)\
**Post date:** [September 25, 2013, 1:13am UTC](https://forum.ansible.com/t/getting-top-level-directory-when-using-roles/13542/3 "2013-09-25T01:13:11Z")

</div>

Cool, thanks for the info Michael. I’ll try to find some time to see about adding a base directory, though can’t promise a pull request. Maybe it is just good enough as is anyway.

Thanks again!

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 25, 2013, 1:27am UTC](https://forum.ansible.com/t/getting-top-level-directory-when-using-roles/13542/4 "2013-09-25T01:27:40Z")

</div>

If you want to file a feature request ticket in github, someone might come along and make it happen too 🙂

---

<div class="post-metadata">

**Author:** ![Jonathan\_Rowlands](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@Jonathan\_Rowlands](https://forum.ansible.com/u/Jonathan_Rowlands)\
**Post date:** [September 25, 2013, 4:10pm UTC](https://forum.ansible.com/t/getting-top-level-directory-when-using-roles/13542/5 "2013-09-25T16:10:46Z")

</div>

[Good idea](https://github.com/ansible/ansible/issues/4246).

---

<div class="post-metadata">

**Author:** ![Jurgen\_Haas2](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@Jurgen\_Haas2](https://forum.ansible.com/u/Jurgen_Haas2)\
**Post date:** [September 26, 2013, 10:43am UTC](https://forum.ansible.com/t/getting-top-level-directory-when-using-roles/13542/6 "2013-09-26T10:43:38Z")

</div>

I’ve tried that variable but it seems to not provide what’s expected - I’m on the latest dev-release of Ansible from github.

When using this in a roles/ROLENAME/defaults/main.yml:

password: “{{ lookup(‘password’, ‘./’ + inventory\_dir + ‘/files/’ + inventory\_hostname + ‘/password length=16’ ) }}”

Then I’m getting a file named roles/ROLENAME/PATH\_TO\_INVENTORY/files/HOSTNAME/password

My structure is:

root

- inventories
- company1
- company2
- company3
- scripts
- script1
- script2
- script3
- playbooks
- pb1.yml
- pb2.yml
- pb3.yml
- roles
- role1
- role2
- role3

I’m calling ansible-playbook from within the directory root/scripts like this:

ansible-playbook -i …/inventories/company1/hosts …/playbooks/pb1

I was expecting the file to be called root/inventories/company1/files/HOSTNAME/password but it isn’t.

Not: in the lookup I tried with and without the leading ‘./’ and both result in the same file name in the end.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 26, 2013, 12:28pm UTC](https://forum.ansible.com/t/getting-top-level-directory-when-using-roles/13542/7 "2013-09-26T12:28:42Z")

</div>

“'ve tried that variable but it seems to not provide what’s expected”

It’s an absolute path, don’t prefix it with a relative one.

---

<div class="post-metadata">

**Author:** ![Jurgen\_Haas2](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@Jurgen\_Haas2](https://forum.ansible.com/u/Jurgen_Haas2)\
**Post date:** [September 26, 2013, 3:34pm UTC](https://forum.ansible.com/t/getting-top-level-directory-when-using-roles/13542/8 "2013-09-26T15:34:03Z")

</div>

What do you mean by this being an absolute path? What I’m getting is “inventories/company1” but no glue where this is in the full directory tree.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 26, 2013, 10:31pm UTC](https://forum.ansible.com/t/getting-top-level-directory-when-using-roles/13542/9 "2013-09-26T22:31:45Z")

</div>

ok so /root is an absolute path

you would never say “.//root” to use that path.

Just like you wouldn’t say ‘./’ + inventory\_dir

Because inventory\_dir is an absolute path.

---

<div class="post-metadata">

**Author:** ![Jurgen\_Haas2](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@Jurgen\_Haas2](https://forum.ansible.com/u/Jurgen_Haas2)\
**Post date:** [September 27, 2013, 6:03am UTC](https://forum.ansible.com/t/getting-top-level-directory-when-using-roles/13542/10 "2013-09-27T06:03:21Z")

</div>

Yes, I agree that /root is an absolute path. But what I get for onventory\_dir is “inventories/customer1” and I would expect to get “/root/inventories/customer1” because then I would exactly know where to go. But with only “inventories/customer1” I don’t.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 27, 2013, 2:31pm UTC](https://forum.ansible.com/t/getting-top-level-directory-when-using-roles/13542/11 "2013-09-27T14:31:47Z")

</div>

If you do not see the inventory directory as an absolute path on 1.3.X please file a bug.

---

<div class="post-metadata">

**Author:** ![Jurgen\_Haas2](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@Jurgen\_Haas2](https://forum.ansible.com/u/Jurgen_Haas2)\
**Post date:** [September 29, 2013, 9:26am UTC](https://forum.ansible.com/t/getting-top-level-directory-when-using-roles/13542/12 "2013-09-29T09:26:09Z")

</div>

Bug report filed under [https://github.com/ansible/ansible/issues/4291](https://github.com/ansible/ansible/issues/4291)

---

<div class="post-metadata">

**Author:** ![Jurgen\_Haas2](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@Jurgen\_Haas2](https://forum.ansible.com/u/Jurgen_Haas2)\
**Post date:** [October 14, 2013, 6:37am UTC](https://forum.ansible.com/t/getting-top-level-directory-when-using-roles/13542/13 "2013-10-14T06:37:08Z")

</div>

@Michael DeHaan, I have just commented on the closed issue 4291 and wonder if you could please have another look there.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [October 14, 2013, 12:30pm UTC](https://forum.ansible.com/t/getting-top-level-directory-when-using-roles/13542/14 "2013-10-14T12:30:47Z")

</div>

I’ve replied, thanks.

---

<div class="post-metadata">

**Author:** ![Jurgen\_Haas2](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@Jurgen\_Haas2](https://forum.ansible.com/u/Jurgen_Haas2)\
**Post date:** [October 15, 2013, 5:57pm UTC](https://forum.ansible.com/t/getting-top-level-directory-when-using-roles/13542/15 "2013-10-15T17:57:11Z")

</div>

Not sure if you’re getting notifications while the issue is already closed, but I have responded there yesterday and provided all the details to reproduce the problem.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [October 15, 2013, 11:33pm UTC](https://forum.ansible.com/t/getting-top-level-directory-when-using-roles/13542/16 "2013-10-15T23:33:03Z")

</div>

Notifications on closed tickets are typically not read or responded to.

If you believe something is still a problem, please open a ticket with a link to a gist or github repo that reproduces the problem.

It is so easy to clobber formatting of content in the github comment box.

---

<div class="post-metadata">

**Author:** ![Jurgen\_Haas2](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@Jurgen\_Haas2](https://forum.ansible.com/u/Jurgen_Haas2)\
**Post date:** [October 16, 2013, 6:50am UTC](https://forum.ansible.com/t/getting-top-level-directory-when-using-roles/13542/17 "2013-10-16T06:50:12Z")

</div>

Understood. All available as [https://github.com/ansible/ansible/issues/4535](https://github.com/ansible/ansible/issues/4535) including the link to the gist and a screenshot.
