# How to resolve relative paths given as arguments in custom modules?

**URL:** <https://forum.ansible.com/t/how-to-resolve-relative-paths-given-as-arguments-in-custom-modules/18159>\
**Category:** Ansible Developer\
**Created:** [May 5, 2015, 1:50pm UTC](https://forum.ansible.com/t/how-to-resolve-relative-paths-given-as-arguments-in-custom-modules/18159 "2015-05-05T13:50:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eyal\_Roth](https://avatars.discourse-cdn.com/v4/letter/e/5f9b8f/32.png) [@Eyal\_Roth](https://forum.ansible.com/u/Eyal_Roth)\
**Post date:** [May 5, 2015, 1:50pm UTC](https://forum.ansible.com/t/how-to-resolve-relative-paths-given-as-arguments-in-custom-modules/18159/1 "2015-05-05T13:50:40Z")

</div>

I’m referring a question into here after opening an [issue on github](https://github.com/ansible/ansible-modules-core/issues/1245). I’ll copy my question from the issue:

Say I’m writing a new module, and I want to have an argument which points to a file (which I’d like to read or whatever). I want it to be possible to receive an absolute path or a “relative” one (pointing to roles/x/files directory).

How can I receive such a “relative” path and resolve it to the actual file? I couldn’t find a way to do that.

Moreover, I tried to run the “test-module” script on the “copy” core module (as if it was just a custom module), and it wasn’t able to resolve a relative path.  
Meaning, as far as I understand it, there’s nothing special in the “copy” module’s code, but it is being invoked in a different way (and so does all the core modules).

---

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [May 5, 2015, 4:51pm UTC](https://forum.ansible.com/t/how-to-resolve-relative-paths-given-as-arguments-in-custom-modules/18159/2 "2015-05-05T16:51:04Z")

</div>

Ansible modules run on the target server, so they don't have access to the controlling machine's files at all. If you need to access local files you need an action plugin. That's where the actual magic of the copy module happens 🙂

Hagai

PS switched jobs I see?

---

<div class="post-metadata">

**Author:** ![Eyal\_Roth](https://avatars.discourse-cdn.com/v4/letter/e/5f9b8f/32.png) [@Eyal\_Roth](https://forum.ansible.com/u/Eyal_Roth)\
**Post date:** [May 6, 2015, 2:23pm UTC](https://forum.ansible.com/t/how-to-resolve-relative-paths-given-as-arguments-in-custom-modules/18159/3 "2015-05-06T14:23:59Z")

</div>

Well, that’s a little too many undocumented magic :\

According to you suggestion, I tried to write my own custom action plugin; I placed it under playbook-dir/action\_plugins/my\_mod\_name.py.  
Here’s the code:

`  
from ansible.runner.lookup\_plugins.file import LookupModule as FilePlugin  
from ansible import utils

class ActionModule(object):

def **init** (self, runner):  
self.runner = runner

def run(self, conn, tmp\_path, module\_name, module\_args, inject, complex\_args=None, \*\*kwargs):

options = {}  
options.update(utils.parse\_kv(module\_args))

file\_plugin = FilePlugin()  
files\_content = file\_plugin.run(terms=options.get(‘myarg’), inject=inject)  
options[‘myarg’] = files\_content[0]

return self.runner.\_execute\_module(conn=conn, tmp=“/tmp”, module\_name=“mymod”, args=utils.serialize\_args(options), inject=inject)  
`

Then, in the module, the “myarg” argument will have the file content (not the path).

I hope this is the right way to do it.

PS in a way 🙂

---

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [May 6, 2015, 2:28pm UTC](https://forum.ansible.com/t/how-to-resolve-relative-paths-given-as-arguments-in-custom-modules/18159/4 "2015-05-06T14:28:52Z")

</div>

I don’t really remember how it’s done, but action plugins can transfer files to a temp dir on the remote machine, which your module can then use. The copy action plugin does that iirc.
