Custom module - finding files?

87 views
Skip to first unread message

John McNulty

unread,
May 21, 2015, 10:33:37 AM5/21/15
to ansible...@googlegroups.com
I've written a module that has requires a src and dest file.  I'm puzzled as to how core modules like copy find their source files in the role/<name>/files directory.  I've looked at the sources for copy.py and it just gets the source file from module.params['src'].   Then tests with:    

if not os.path.exists(src):
       
module.fail_json(msg="Source %s failed to transfer" % (src))

If I do the same I find it's looking for the source file in my account home directory, so doesn't find anything:

  # Sanity check the src file exists
 
if not os.path.exists(src):
   
module.fail_json( msg='src file does not exist: %s in %s' % (src, os.getcwd()) )

resulting in:

failed: [localhost] => {"failed": true}
msg: src file does not exist: eu-replay-dev in /home/jjmcnulty

It works fine with absolute path names, just not relative.  What am I missing? 

Thanks. John
    if not os.path.exists(src):

        module.fail_json(msg="Source %s failed to transfer" % (src))
    if not os.path.exists(src):
        module.fail_json(msg="Source %s failed to transfer" % (src))

Brian Coca

unread,
May 21, 2015, 10:39:53 AM5/21/15
to ansible...@googlegroups.com
Those modules are split, the part that runs the 'src' stuff is an
action_plugin of the same name. The code that runs is split across the
action plugin (local) and the actual module (remote).

--
Brian Coca

John McNulty

unread,
May 21, 2015, 10:40:42 AM5/21/15
to ansible...@googlegroups.com
Sorry for the garbage code on the end.  Cut and paste using ctrl-v on my chromebook sometimes does some strange stuff in google groups, and the option to edit ones own posts is not enable in this group (hint :) )

John McNulty

unread,
May 21, 2015, 12:50:02 PM5/21/15
to ansible...@googlegroups.com

Hi Brian,

So really the way forward is for me to split this over two tasks.   One task to copy the file from the role files directory on the Ansible server to a temporary directory on the remote host, and then use that known location in my custom module to find, process and clean the target file.

Cheers,   John

Brian Coca

unread,
May 21, 2015, 12:51:04 PM5/21/15
to ansible...@googlegroups.com
you can create your own action plugin to wrap the copy part.
> --
> 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/f632452d-9d97-453c-be03-97f8d7643ea4%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca

Brian Coca

unread,
May 21, 2015, 12:51:13 PM5/21/15
to ansible...@googlegroups.com
this is how template works
--
Brian Coca
Reply all
Reply to author
Forward
0 new messages