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