# Access to files from library in a role

**URL:** <https://forum.ansible.com/t/access-to-files-from-library-in-a-role/18364>\
**Category:** Ansible Project\
**Created:** [June 1, 2015, 7:35am UTC](https://forum.ansible.com/t/access-to-files-from-library-in-a-role/18364 "2015-06-01T07:35:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![zorggy01](https://avatars.discourse-cdn.com/v4/letter/z/76d3ee/32.png) [@zorggy01](https://forum.ansible.com/u/zorggy01)\
**Post date:** [June 1, 2015, 7:35am UTC](https://forum.ansible.com/t/access-to-files-from-library-in-a-role/18364/1 "2015-06-01T07:35:20Z")

</div>

Hello,

I’ve created a role with its main task using my Python module that should access a yaml file located in files dir of the role:

`  
roles  
my\_role  
tasks  
main.yml  
library  
my\_module  
files  
my\_file.yml

`

From my\_module (Python), how can I access my\_file.yml without absolute path? I would like something as “files/my\_file.yml”, does this exists?

---

<div class="post-metadata">

**Author:** ![user64](https://avatars.discourse-cdn.com/v4/letter/u/dec6dc/32.png) [@user64](https://forum.ansible.com/u/user64)\
**Post date:** [June 1, 2015, 10:56am UTC](https://forum.ansible.com/t/access-to-files-from-library-in-a-role/18364/2 "2015-06-01T10:56:39Z")

</div>

Any file in files can used ai is, like  
`

- name: Copy files  
copy: src=‘my\_file.yml’ dest=‘/etc/’ group=‘root’ owner=‘root’ mode=‘0644’

`  
If u catch some vars from thist yml, just place it on my\_role/vars dir.

---

<div class="post-metadata">

**Author:** ![zorggy01](https://avatars.discourse-cdn.com/v4/letter/z/76d3ee/32.png) [@zorggy01](https://forum.ansible.com/u/zorggy01)\
**Post date:** [June 3, 2015, 9:03am UTC](https://forum.ansible.com/t/access-to-files-from-library-in-a-role/18364/3 "2015-06-03T09:03:03Z")

</div>

Thank you for your reply.

As I wrote, I’ve created a Python module. I now access files in “files” directory through absolute path, but I would like to be more flexible, staying in the role directory.

My question is: How do I write, in Python, access to a file in “files"directory”?

---

<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:** [June 3, 2015, 4:53pm UTC](https://forum.ansible.com/t/access-to-files-from-library-in-a-role/18364/4 "2015-06-03T16:53:40Z")

</div>

modules do not access local files on the master (unless running with  
localhost target/local\_action/etc) in the case of copy, it's  
action\_plugin is the one doing this, then copying the file remotely so  
the copy module can use it.

---

<div class="post-metadata">

**Author:** ![zorggy01](https://avatars.discourse-cdn.com/v4/letter/z/76d3ee/32.png) [@zorggy01](https://forum.ansible.com/u/zorggy01)\
**Post date:** [June 4, 2015, 12:16pm UTC](https://forum.ansible.com/t/access-to-files-from-library-in-a-role/18364/5 "2015-06-04T12:16:27Z")

</div>

Hi Brian,

I’ve written a Python module in a role using data that I’m going to change from time to time. That’s why I wanted to separate the Python code from its data, I manually wrote it in a YAML file. I naively thought the “files” directory of the role would be the best place to keep it, but don’t know how to access it from the running Python module.

If it’s not obvious, maybe I should keep data into the Python code of the module even if I don’t feel it very clean (list of 80+ dictionaries)…

---

<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:** [June 4, 2015, 1:16pm UTC](https://forum.ansible.com/t/access-to-files-from-library-in-a-role/18364/6 "2015-06-04T13:16:19Z")

</div>

Hi, it might be easier to use lookup plugins to pass the data to the module, I really would need to know what the modules does and how it works before I can tell you the best approach.

---

<div class="post-metadata">

**Author:** ![zorggy01](https://avatars.discourse-cdn.com/v4/letter/z/76d3ee/32.png) [@zorggy01](https://forum.ansible.com/u/zorggy01)\
**Post date:** [June 5, 2015, 7:28am UTC](https://forum.ansible.com/t/access-to-files-from-library-in-a-role/18364/7 "2015-06-05T07:28:52Z")

</div>

The role is updating /etc/group and /etc/gshadow system files from various sources, such as ldap and a central file which is now in Ansible. That’s why I created a role and put all related data into it: “defaults”, “files”, “library” and “tasks” directories. Data in “defaults” is read by the main.yml task, no problem. But there are various files in “files” directory that should be chosen then read by the module located in “library”.

---

<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:** [June 5, 2015, 5:35pm UTC](https://forum.ansible.com/t/access-to-files-from-library-in-a-role/18364/8 "2015-06-05T17:35:52Z")

</div>

as i mentioned above, modules execute on the 'target' host and cannot  
access data on the 'master' themselves (the exception is when both  
machines are the same, but it has no role/play context at this point).

you can either pass the data by using a lookup module that will read  
the files and make the contents available to the module options ore  
you need an action\_plugin of the same name that can then act on the  
'master'. This is how copy works, the action plugin copies the src  
file to a temporary dir on the target and then runs the copy module  
feeding it the src= as the temporary file it had copied.

---

<div class="post-metadata">

**Author:** ![zorggy01](https://avatars.discourse-cdn.com/v4/letter/z/76d3ee/32.png) [@zorggy01](https://forum.ansible.com/u/zorggy01)\
**Post date:** [June 6, 2015, 5:06pm UTC](https://forum.ansible.com/t/access-to-files-from-library-in-a-role/18364/9 "2015-06-06T17:06:30Z")

</div>

Thank you Brian for your explanation. I’m new to Ansible and don’t understand it perfectly. But it’s better now. I use mainly Ansible in the"pull" way, but it could be possible the other way round.

What I could do is to use the copy module to copy the Ansible role file into a known temp file (/tmp/my\_file.yml) of the target, then my Python module can access it. At the end of the role, the temp file would be removed.

Thank you all for the time spent explaining me how Ansible works! I thought that when a role was executed on a target, the full role directory was copied and then vars, templates and files were simply accessible from tasks and library.

---

<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:** [June 6, 2015, 5:40pm UTC](https://forum.ansible.com/t/access-to-files-from-library-in-a-role/18364/10 "2015-06-06T17:40:19Z")

</div>

no, play/roles/includes get compiled into a task list on the 'master',  
once this is done each task is individually handled on the target  
server, only the task code itself and arguments are copied to the  
machine and then executed.
