Custom module - finding files?

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//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))

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).

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 :slight_smile: )

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

you can create your own action plugin to wrap the copy part.

this is how template works