basename of a list of files returned from 'with_fileglobs'

2,647 views
Skip to first unread message

mto...@go2uti.com

unread,
Jun 18, 2014, 7:22:51 PM6/18/14
to ansible...@googlegroups.com

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


Matt Martz

unread,
Jun 18, 2014, 8:16:41 PM6/18/14
to ansible...@googlegroups.com
There is a basename jinja2 filter provided with ansible.

Just use:

"{{ item|basename }}"
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/98ead234-15ba-4c85-906b-6453f389d2ff%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
ma...@sivel.net
http://sivel.net/

Tovey, Mark

unread,
Jun 19, 2014, 12:18:45 AM6/19/14
to ansible...@googlegroups.com

 

    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

 

________________________________________________________________

 

Mark Tovey - UNIX Engineer | Service Strategy & Design

UTi | 400 SW Sixth Ave, Suite 1100 | Portland | Oregon | 97204 | USA

MTo...@go2uti.com | O / C +1 503 953-1389

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/oKrK24bfVPk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.


To post to this group, send email to ansible...@googlegroups.com.

Michael DeHaan

unread,
Jun 19, 2014, 5:19:26 PM6/19/14
to ansible...@googlegroups.com
Filters are that way, I don't think there needs to be a simpler way as filters work fine for this.




Reply all
Reply to author
Forward
0 new messages