There isn't a great answer here.  roles weren't originally designed to work
this way.

You may want to look into collections instead, which have proper python
imports, that aren't effectively "fake".  A collection can include
modules/plugins, as well as roles.  However the location of the module
moves.

This doesn't really solve things for older versions of Ansible however, as
collections are a recent addition.

On Fri, Mar 6, 2020 at 11:01 AM Rich Megginson <rmegg...@redhat.com> wrote:

> When developing a role that has a local module with some code split into
> module_utils, how do you set up your pythonpath or venv so that pylint
> and unit testing resolves the imports correctly?
>
> For example: https://github.com/linux-system-roles/network/
>
> The module code is in
>
> https://github.com/linux-system-roles/network/blob/master/library/network_connections.py
>
> The module_utils code is in
>
> https://github.com/linux-system-roles/network/tree/master/module_utils/network_lsr
>
> The module code imports the module_utils code like this:
>
> # pylint: disable=import-error, no-name-in-module
> from ansible.module_utils.network_lsr import MyError
>
> As you can see, we have to disable pylint checking, because there is no
> ansible.module_utils.network_lsr - there is a module_utils.network_lsr
> however.  We have to do something similar in the unit test code:
>
> https://github.com/linux-system-roles/network/blob/master/tests/unit/test_nm_provider.py
>
> sys.modules["ansible"] = mock.Mock()
> sys.modules["ansible.module_utils.basic"] = mock.Mock()
> sys.modules["ansible.module_utils"] = mock.Mock()
> sys.modules["ansible.module_utils.network_lsr"] = __import__("network_lsr")
>
> This seems pretty hackish, and I'm hoping there is a better or standard
> way to do this.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-devel+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-devel/d5f0c3ad-f7e5-09de-b5b4-3e9c741cd567%40redhat.com
> .
>


-- 
Matt Martz
@sivel
sivel.net

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-devel+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-devel/CAD8N0v_Pn-V51-dC-05VQkL6zoh%3D9kVPZVB%2BpiS7nXY4HjiydA%40mail.gmail.com.

Reply via email to