basename of a list of files returned from 'with_fileglobs'

I need to get the base names of a list of files returned from a call to ‘with_fileglob’. I have written a lookup plugin that calls Python’s os.path.basename function and named it simply ‘basename’. Then in a playbook, I am doing this:

  • name: Test
    debug: msg=“base = [{{lookup(‘basename’, ‘item’)}}]”
    with_fileglob:
  • /path/to/my/files/*

And the basename plugin looks like this:

from ansible import utils, errors
import os;

class LookupModule(object):

def init(self, basedir=None, **kwargs):
self.basedir = basedir

def run(self, terms, inject=None, **kwargs):

terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

for term in terms:

ret = os.path.basename(term)
print "in basename, ret = ", ret

return ret

When I run the playbook, I get results that look like this:

in basename, ret = file1
ok: [server1] => (item=/path/to/my/files/file1) => {
“item”: “/path/to/my/files/file1”,
“msg”: “base = [f,i,l,e,1]
}

msg:
base = [j,n,b,g,d,b]

So, for the most part this is working, however the value being returned is further parsed with commas separating the characters of the name even though the output from the debug print statement in the basename plugin does not show this extra parsing (highlighted above). Can anyone explain why this extra parsing is occurring in Ansible? (version 1.5.3)
If there is an easier method of finding the base name of files already built into Ansible I would appreciate learning about it, but I would still like to know what is causing the above behavior for future reference. I am very new to Python programming and I created the above plugin by copying an existing plugin and modifying it for my purposes, so there is likely stuff in there that is not necessary, or perhaps somthing is missing that should be in there. Any suggestions or advice is gladly appreciated.
Thanks,
-Mark

There is a basename jinja2 filter provided with ansible.

Just use:

“{{ item|basename }}”

That got it! I figured that this shouldn’t be that difficult a thing to accomplish and there must be a simpler way to achieve what I was doing. But I wanted to explore plugins anyway, so I don’t feel my effort was wasted. I would still like to know why the value being returned from my basename plugin is being further parsed by Ansible, so if anyone can help shed some light on that it would be greatly appreciated. Meanwhile, it looks like I should explore what else is available in Jinja too.

Thanks,

-Mark

Filters are that way, I don’t think there needs to be a simpler way as filters work fine for this.