# using dynamic inventory (folder) with vars\_plugins fails

**URL:** <https://forum.ansible.com/t/using-dynamic-inventory-folder-with-vars-plugins-fails/17671>\
**Category:** Ansible Developer\
**Created:** [March 5, 2015, 12:44am UTC](https://forum.ansible.com/t/using-dynamic-inventory-folder-with-vars-plugins-fails/17671 "2015-03-05T00:44:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kesten\_Broughton2](https://avatars.discourse-cdn.com/v4/letter/k/ecc23a/32.png) [@Kesten\_Broughton2](https://forum.ansible.com/u/Kesten_Broughton2)\
**Post date:** [March 5, 2015, 12:44am UTC](https://forum.ansible.com/t/using-dynamic-inventory-folder-with-vars-plugins-fails/17671/1 "2015-03-05T00:44:03Z")

</div>

I use different ansible canonical project directories to separate out devops from platform from deployment variables.  
Ansible affords gluing things together quite nicely using the  
roles\_path:  
in .ansible.cfg

However, I also use a vars\_plugin/deploy\_vars.py to manage my deployment variables in a hierarchical way. My plugin works fine if hosts, site.yml and vars\_plugins are all in the same directory. But it fails if they are separate.

My commandline looks like

[ansible\_deploy\_vars] ansible-playbook -i …/ansible\_platform/inventory …/ansible\_platform/site.yml

and my vars\_plugins is symlinked like so.

[ansible\_deploy\_vars] ls -al …/ansible\_platform  
vars\_plugins → …/…/ansible\_deploy\_vars/vars\_plugins

The problem is here:

```auto
def basedir(self):
    """ if inventory came from a file, what's the directory? """

```

```auto
    if not self.is_file():
        return None
    dname = os.path.dirname(self.host_list.rstrip('/'))
    if dname is None or dname == '' or dname == '.':
        cwd = os.getcwd()
        return os.path.abspath(cwd) 
    return os.path.abspath(dname)

```

```auto

```

```auto
I believe the purpose of basedir is to get the ansible project root dir (typical directory containing group_vars, vars_plugins etc) given a path to an inventory file. But it does not handle dynamic inventory or directories correctly. As is, 

```

```auto
[ansible_deploy_vars] ansible-playbook -i ../ansible_platform/inventory ../ansible_platform/site.yml

```

```auto
vs the following with a trailing '/' follows a different code path which seems wrong.

```

```auto
[ansible_deploy_vars] ansible-playbook -i ../ansible_platform/inventory/ ../ansible_platform/site.yml

```

```auto

```

```auto
Simply adding an rstrip('/') solves all my problems.

```

```auto
Would this be considered for a PR?

```

```auto

```

**diff --git a/lib/ansible/inventory/init.py b/lib/ansible/inventory/init.py**

**index 2048046…41c8f6a 100644**

**— a/lib/ansible/inventory/init.py**

**+++ b/lib/ansible/inventory/init.py**

@@ -562,7 +563,7 @@ class Inventory(object):

“”" if inventory came from a file, what’s the directory? “”"

if not self.is\_file():

return None

- dname = os.path.dirname(self.host\_list)

- dname = os.path.dirname(self.host\_list.rstrip(‘/’))

if dname is None or dname == ‘’ or dname == ‘.’:

cwd = os.getcwd()

return os.path.abspath(cwd)
