# Custom module - finding files?

**URL:** <https://forum.ansible.com/t/custom-module-finding-files/18280>\
**Category:** Ansible Project\
**Created:** [May 21, 2015, 2:33pm UTC](https://forum.ansible.com/t/custom-module-finding-files/18280 "2015-05-21T14:33:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_McNulty](https://avatars.discourse-cdn.com/v4/letter/j/e9a140/32.png) [@John\_McNulty](https://forum.ansible.com/u/John_McNulty)\
**Post date:** [May 21, 2015, 2:33pm UTC](https://forum.ansible.com/t/custom-module-finding-files/18280/1 "2015-05-21T14:33:37Z")

</div>

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

```auto
    if not os.path.exists(src):

```

```auto

```

```auto
        module.fail_json(msg="Source %s failed to transfer" % (src))

```

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

```

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 21, 2015, 2:39pm UTC](https://forum.ansible.com/t/custom-module-finding-files/18280/2 "2015-05-21T14:39:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![John\_McNulty](https://avatars.discourse-cdn.com/v4/letter/j/e9a140/32.png) [@John\_McNulty](https://forum.ansible.com/u/John_McNulty)\
**Post date:** [May 21, 2015, 2:40pm UTC](https://forum.ansible.com/t/custom-module-finding-files/18280/3 "2015-05-21T14:40:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![John\_McNulty](https://avatars.discourse-cdn.com/v4/letter/j/e9a140/32.png) [@John\_McNulty](https://forum.ansible.com/u/John_McNulty)\
**Post date:** [May 21, 2015, 4:50pm UTC](https://forum.ansible.com/t/custom-module-finding-files/18280/4 "2015-05-21T16:50:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 21, 2015, 4:51pm UTC](https://forum.ansible.com/t/custom-module-finding-files/18280/5 "2015-05-21T16:51:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [May 21, 2015, 4:51pm UTC](https://forum.ansible.com/t/custom-module-finding-files/18280/6 "2015-05-21T16:51:10Z")

</div>

this is how template works
