Python boilerplate in modules

Hi all,

being new to Ansible development and currently trying to
understand how some of the core modules work, I was
wondering why modules don't use the usual good practice
Python boilerplate a la

    from ansible.module_utils.basic import *

    # main module code

    if __name__ == '__main__':
        main()

instead of the current

    # main module code
    from ansible.module_utils.basic import *
    main()

The former would allow importing Ansible modules into
other code which might be quite useful in some situations.

Cheers
-Andi

It’s because putting things at the bottom don’t affect line numbers in the tracebacks.

Ansible is designed to not ship around a lot of dependencies, so these particular imports are pre-processed, and while your editor can tab-complete them, they are not standard python imports and get replaced before transfer and execution.

Please also note that while the boilerplate is licensed BSD, all ansible modules in core are GPL - so your application needs to be ok with that.

Enabling them for use as a library isn’t a priority really, but this is why module_utils exists - it enables code sharing between modules.

Michael, thanks for the clarification. I obviously overlooked
the fact that includes of non-core-Python modules need to be
expanded in-situ before transfer to the target machine.
Accordingly, importing one Ansible module into another one does
not make too much sense, I fear …

No worries regarding BSD vs GPL licensing, though. I'm fine with
both of them and my question was more out of technical curiousity
than with a particular application or real world use-case in mind.

Regards
-Andi