# path\_dwim assumes basedir is set

**URL:** <https://forum.ansible.com/t/path-dwim-assumes-basedir-is-set/14287>\
**Category:** Ansible Developer\
**Created:** [January 7, 2014, 10:07am UTC](https://forum.ansible.com/t/path-dwim-assumes-basedir-is-set/14287 "2014-01-07T10:07:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [January 7, 2014, 10:07am UTC](https://forum.ansible.com/t/path-dwim-assumes-basedir-is-set/14287/1 "2014-01-07T10:07:23Z")

</div>

Hi,

I bumped into a small bug. Easily patched, but I’m not 100% sure what I did is the best way here.

Can be reproduced with this basic playbook

> - hosts: localhost  
> gather\_facts: false  
> connection: local
> 
> vars:  
> my: “{{ lookup(‘password’,‘tmppwfile’) }}”  
> tasks:
> 
> - debug: var=my

Which yields this traceback:

> fatal: [localhost] =\> Traceback (most recent call last):  
> File “/home/serge/src/ansible/lib/ansible/runner/ **init**.py”, line 426, in \_executor  
> exec\_rc = self.\_executor\_internal(host, new\_stdin)  
> File “/home/serge/src/ansible/lib/ansible/runner/ **init**.py”, line 517, in \_executor\_internal  
> return self.\_executor\_internal\_inner(host, self.module\_name, self.module\_args, inject, port, complex\_args=complex\_args)  
> File “/home/serge/src/ansible/lib/ansible/runner/ **init**.py”, line 717, in \_executor\_internal\_inner  
> result = handler.run(conn, tmp, module\_name, module\_args, inject, complex\_args)  
> File “/home/serge/src/ansible/lib/ansible/runner/action\_plugins/debug.py”, line 53, in run  
> **results = template.template(None, “{{ %s }}” % args[‘var’], inject)**  
> File “/home/serge/src/ansible/lib/ansible/utils/template.py”, line 319, in template  
> varname = template\_from\_string(basedir, varname, vars, fail\_on\_undefined)  
> File “/home/serge/src/ansible/lib/ansible/utils/template.py”, line 546, in template\_from\_string  
> res = jinja2.utils.concat(rf)  
> File “”, line 6, in root  
> File “/usr/lib/python2.7/dist-packages/jinja2/runtime.py”, line 153, in resolve  
> return self.parent[key]  
> File “/home/serge/src/ansible/lib/ansible/utils/template.py”, line 400, in **getitem**  
> return template(self.basedir, var, self.vars, fail\_on\_undefined=self.fail\_on\_undefined)  
> File “/home/serge/src/ansible/lib/ansible/utils/template.py”, line 319, in template  
> varname = template\_from\_string(basedir, varname, vars, fail\_on\_undefined)  
> File “/home/serge/src/ansible/lib/ansible/utils/template.py”, line 546, in template\_from\_string  
> res = jinja2.utils.concat(rf)  
> File “”, line 8, in root  
> File “/usr/lib/python2.7/dist-packages/jinja2/runtime.py”, line 193, in call  
> return \_\_obj(\*args, \*\*kwargs)  
> File “/home/serge/src/ansible/lib/ansible/utils/template.py”, line 539, in my\_lookup  
> return lookup(\*args, basedir=basedir, \*\*kwargs)  
> File “/home/serge/src/ansible/lib/ansible/utils/template.py”, line 89, in lookup  
> ran = instance.run(\*args, inject=vars, \*\*kwargs)  
> File “/home/serge/src/ansible/lib/ansible/runner/lookup\_plugins/password.py”, line 79, in run  
> **path = utils.path\_dwim(self.basedir, relpath)**  
> File “/home/serge/src/ansible/lib/ansible/utils/ **init**.py”, line 253, in path\_dwim  
> return os.path.abspath(os.path.join(basedir, given))  
> File “/usr/lib/python2.7/posixpath.py”, line 77, in join  
> elif path == ‘’ or path.endswith(‘/’):  
> AttributeError: ‘NoneType’ object has no attribute ‘endswith’

The cause and solution is believe in two places:

- The debug plugin wit a var param calls a template function setting basedir explicit to None

- The path\_dwimd oes a os.path.join(basedir, given) without checking basedir is not None  
I’m not sure why the the debug plugin didn’t set a basedir here, whether that was an explicit choice or not.

Fixing either of those two items solves the bug. I feel though both fixes might both be implemented, to avoid this bug being repeated elsewhere, but not sure.

My patch containing both, is at [https://github.com/sergevanginderachter/ansible/commit/1c43399ba79a5b44b9c2fcd25ce32dc326886da6](https://github.com/sergevanginderachter/ansible/commit/1c43399ba79a5b44b9c2fcd25ce32dc326886da6)  
I’ll update it if necessary, and make a PR if it’s ok.

Posting this here as I’m a bit confused on the best practice here. E.g., this might also be solved, though in a less generic and not optimal fashion, by having the password lookup plugin check the basedir.

I think my question basically comes to "when would basedir not be (need to be) set or be set to None?

Thanks,

Serge

---

<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:** [January 7, 2014, 1:39pm UTC](https://forum.ansible.com/t/path-dwim-assumes-basedir-is-set/14287/2 "2014-01-07T13:39:56Z")

</div>

“whether that was an explicit choice or not.”

It’s not an explicit choice, just something that wasn’t encountered before.

Please send the PR along!

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [January 7, 2014, 1:49pm UTC](https://forum.ansible.com/t/path-dwim-assumes-basedir-is-set/14287/3 "2014-01-07T13:49:34Z")

</div>

[https://github.com/ansible/ansible/pull/5527](https://github.com/ansible/ansible/pull/5527)
