# How to define a base\_dir var so you can use {{ base\_dir }}/playbooks etc?

**URL:** <https://forum.ansible.com/t/how-to-define-a-base-dir-var-so-you-can-use-base-dir-playbooks-etc/22184>\
**Category:** Ansible Project\
**Created:** [August 1, 2016, 4:56pm UTC](https://forum.ansible.com/t/how-to-define-a-base-dir-var-so-you-can-use-base-dir-playbooks-etc/22184 "2016-08-01T16:56:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 1, 2016, 4:56pm UTC](https://forum.ansible.com/t/how-to-define-a-base-dir-var-so-you-can-use-base-dir-playbooks-etc/22184/1 "2016-08-01T16:56:47Z")

</div>

Hi,

Ansible 2.1.1.0.

Situation: Tim and Bob both deploy servers. Tim checks out the central ansible git repo to /home/tim/ansible while Bob checks it out to /home/bob/ansible. No special ansible.cfg is used by either.

Problem: paths differ (/home/tim/ansible vs /home/bob/ansible) so you can not use static paths in the playbooks etc.

Question: how do you solve that? How do you specify something like a base\_dir variable which is read early on so that all static paths can be replaced.

\<example based on Google findings which I could not make work\>

$ grep base\_dir /home/tim/ansible/inventory/group\_vars/all  
base\_dir: "{{ lookup('pipe', 'git rev-parse --show-toplevel') }}"

In Tim's case base\_dir should be "/home/tim/ansible".

$ cat /home/tim/ansible/vars/global\_vars.yml

roles: "{{ base\_dir}}/roles"  
handlersdir: "{{ base\_dir }}/handlers"  
tasksdir: "{{ base\_dir }}/tasks"  
playbooksdir: "{{ base\_dir }}/playbooks"  
includedir: "{{ base\_dir }}/playbooks/include"  
manualdir: "{{ base\_dir }}/playbooks/manual"  
varsdir: "{{ base\_dir }}/vars"

$ cat /home/tim/ansible/playbooks/groups/dns

- name: setup dns servers  
&nbsp;&nbsp;&nbsp;hosts: dns  
&nbsp;&nbsp;&nbsp;user: root  
&nbsp;&nbsp;&nbsp;gather\_facts: True

&nbsp;&nbsp;&nbsp;vars\_files:  
&nbsp;&nbsp;- "{{ base\_dir }}/vars/global\_vars.yml

&nbsp;&nbsp;&nbsp;roles:  
&nbsp;&nbsp;- base  
&nbsp;&nbsp;- dns

TIA,  
Patrick

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 3, 2016, 7:44am UTC](https://forum.ansible.com/t/how-to-define-a-base-dir-var-so-you-can-use-base-dir-playbooks-etc/22184/2 "2016-08-03T07:44:17Z")

</div>

> Hi,
> 
> Ansible 2.1.1.0.
> 
> Situation: Tim and Bob both deploy servers. Tim checks out the central  
> ansible git repo to /home/tim/ansible while Bob checks it out to  
> /home/bob/ansible. No special ansible.cfg is used by either.

This is a very common way to do it.  
You can also put the ansible.cfg in this folder so everybody shares the same ansible.cfg.

> Problem: paths differ (/home/tim/ansible vs /home/bob/ansible) so you  
> can not use static paths in the playbooks etc.

This is not a problem you always use relative paths and not absolute(static) path.

> Question: how do you solve that? How do you specify something like a  
> base\_dir variable which is read early on so that all static paths can  
> be replaced.

Relative path and Ansible has a directory layout that it understand, I recommend reading this page that explain the concept.  
[https://docs.ansible.com/ansible/playbooks\_best\_practices.html](https://docs.ansible.com/ansible/playbooks_best_practices.html)

So when you run ansible or ansible-playbook Tim and Bob will always run them from within ~/ansible

> \<example based on Google findings which I could not make work\>
> 
> $ grep base\_dir /home/tim/ansible/inventory/group\_vars/all  
> base\_dir: "{{ lookup('pipe', 'git rev-parse --show-toplevel') }}"
> 
> In Tim's case base\_dir should be "/home/tim/ansible".
> 
> $ cat /home/tim/ansible/vars/global\_vars.yml
> 
> roles: "{{ base\_dir}}/roles"  
> handlersdir: "{{ base\_dir }}/handlers"  
> tasksdir: "{{ base\_dir }}/tasks"  
> playbooksdir: "{{ base\_dir }}/playbooks"  
> includedir: "{{ base\_dir }}/playbooks/include"  
> manualdir: "{{ base\_dir }}/playbooks/manual"  
> varsdir: "{{ base\_dir }}/vars"
> 
> $ cat /home/tim/ansible/playbooks/groups/dns
> 
> - name: setup dns servers  
> &nbsp;&nbsp;hosts: dns  
> &nbsp;&nbsp;user: root  
> &nbsp;&nbsp;gather\_facts: True
> 
> &nbsp;&nbsp;vars\_files:  
> &nbsp;&nbsp;- "{{ base\_dir }}/vars/global\_vars.yml
> 
> &nbsp;&nbsp;roles:  
> &nbsp;&nbsp;- base  
> &nbsp;&nbsp;- dns

It looks like a very strange concept, I do recommend picking up a Ansible book to get basic knowledge of how Ansible works.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 3, 2016, 12:01pm UTC](https://forum.ansible.com/t/how-to-define-a-base-dir-var-so-you-can-use-base-dir-playbooks-etc/22184/3 "2016-08-03T12:01:20Z")

</div>

Hi Kai,

Thank you for your feedback.

> > Hi,
> > 
> > Ansible 2.1.1.0.
> > 
> > Situation: Tim and Bob both deploy servers. Tim checks out the central  
> > ansible git repo to /home/tim/ansible while Bob checks it out to  
> > /home/bob/ansible. No special ansible.cfg is used by either.
> 
> This is a very common way to do it.  
> You can also put the ansible.cfg in this folder so everybody shares the  
> same ansible.cfg.

Got it.

> > Problem: paths differ (/home/tim/ansible vs /home/bob/ansible) so you  
> > can not use static paths in the playbooks etc.
> 
> This is not a problem you always use relative paths and not  
> absolute(static) path.

Great.

> > Question: how do you solve that? How do you specify something like a  
> > base\_dir variable which is read early on so that all static paths can  
> > be replaced.
> 
> Relative path and Ansible has a directory layout that it understand, I  
> recommend reading this page that explain the concept.  
> [https://docs.ansible.com/ansible/playbooks\_best\_practices.html](https://docs.ansible.com/ansible/playbooks_best_practices.html)

Will read that.

> So when you run ansible or ansible-playbook Tim and Bob will always run  
> them from within ~/ansible

[snip]  
\>

> It looks like a very strange concept, I do recommend picking up a  
> Ansible book to get basic knowledge of how Ansible works.

The concept is sorta used by the Fedora Infrastructure team. Looking at their setup I wondered about defining paths via vars hence my question to the list.

Thanks again for your help!

Best,  
Patrick
