Access to files from library in a role

149 views
Skip to first unread message

zorg...@gmail.com

unread,
Jun 1, 2015, 3:35:20 AM6/1/15
to ansible...@googlegroups.com
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?

Шалыгин Константин

unread,
Jun 1, 2015, 8:44:21 AM6/1/15
to ansible...@googlegroups.com
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.

zorg...@gmail.com

unread,
Jun 3, 2015, 5:03:04 AM6/3/15
to ansible...@googlegroups.com
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"?

Brian Coca

unread,
Jun 3, 2015, 12:53:44 PM6/3/15
to ansible...@googlegroups.com
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.
> --
> 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/b8dc1366-0c5c-4c8a-a0de-8b23b6ec392b%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca

zorg...@gmail.com

unread,
Jun 4, 2015, 8:16:27 AM6/4/15
to ansible...@googlegroups.com
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)...

Brian Coca

unread,
Jun 4, 2015, 9:16:23 AM6/4/15
to ansible...@googlegroups.com
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.


On Thursday, June 4, 2015, <zorg...@gmail.com> wrote:
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)...




--
Brian Coca

zorg...@gmail.com

unread,
Jun 5, 2015, 3:28:53 AM6/5/15
to ansible...@googlegroups.com
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".

Brian Coca

unread,
Jun 5, 2015, 1:35:55 PM6/5/15
to ansible...@googlegroups.com
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.
> --
> 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/3d00e8fb-acce-49a4-85f9-abaaccc49963%40googlegroups.com.

zorg...@gmail.com

unread,
Jun 6, 2015, 1:06:31 PM6/6/15
to ansible...@googlegroups.com
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.

Brian Coca

unread,
Jun 6, 2015, 1:40:23 PM6/6/15
to ansible...@googlegroups.com
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.
> https://groups.google.com/d/msgid/ansible-project/9415924c-55ae-4194-864c-5d4ee163ac6b%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages