Importing raw utils modules

The ANSI module development documentation says:

The key part of the [Ansible module entries] involves always completing the module file:

from ansible.module_utils.basic import *
main()

      

This is contrary to the usual practice of grouping imports at the top of the file. Using it import *

also prevents lint tools (like flake8) from working efficiently and is generally considered bad practice .

Is there any reason for importing this way, or is Ansible just making its own style guide here?

+3


source to share


1 answer


NOTE. The following answer no longer applies to Ansible 2.1+. From comments:

I realize this is an old post, but if anyone is still interested, it's worth noting that this is no longer the case as possible. Taken from here : Before Ansible-2.1.0, importing only what you used from ansible.module_utils.basic didn't work. You needed to use wildcard imports - bouletta

Original Answer


Ansible ( prior to 2.1 ) will refuse to run unless you are doing business import *

. I'm not 100% sure the magic is being done, but I know a few .

Replacer is used to insert code snippets into modules prior to translation. Instead of doing classic python imports, this allows for more efficient transfer in scripts without reloading, without moving additional files over the wire, and also takes care of nesting arguments in passed modules.

This version is done in such a way that local imports can still be used in modular code, so the IDE doesn't have to know what's going on.

Example:

from ansible.module_utils.basic import * 

      

... will insert basic.py into the module from the module_utils / directory in the source tree.

All modules should import at least the basic ones, although there will also be other fragments.

+2


source







All Articles