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”?
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.
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)…
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.
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”.
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.
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.
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.