There is also this related question "Access predefined ansible modules in custom module" but unfortunately without an helpful answer.
In general, I would like to create a Custom Module which uses some Core Modules.
If I e.g. want to use the Core Module file, I could of course look at the main function and extract the parts needed by importing the functions required.
So to create / remove a file this would require to use something like
from ansible.modules.file import check_owner_exists, check_group_exists, keep_backward_compatibility_on_timestamps, ensure_file_attributes, ensure_absent
Then copy the required code and use it accordingly.
This seems like a huge workaround for something as simple as importing a core module in a custom module.
Are there any better ways to call the file module in a custom module?
As @U880D already pointed out, it is not possible call a module from another module.
The way around this is to use an
actionplugin (https://docs.ansible.com/ansible/latest/dev_guide/developing_plugins.html#developing-actions. The most prominent example is probably the template module:In the hope it simplifies life for others, here is what I ended up using.
So to run the
filemodule inside our own "module" we createlibrary/custom_file.py(the "module" file containing only the documentation) with contentand
action_plugins/custom_file.pywithnow we can use this "module" in our playbook
test.yml