Using custom module_utils-path for shared code

Heh, that was me. (@felixfontein I still owe you some docs about shared code. I have not forgotten!)

In my case, I wrote a custom test - sch_distinct - and a custom filter - sch_uniqcomb - and I needed to use some code from the test in the filter. I eventually settled on this structure in a “local” (to my project) collection:

collections/ansible_collections/mw/tablinx/plugins
├── filter
│   └── tablinx.py
└── test
    └── tablinx.py

(And, yes, I gave all the files the same name. If we were all named Bob no-one would ever forget a name.)

So the filter uses the following import to pull in the test code’s distinct function:

from ansible_collections.mw.tablinx.plugins.test.tablinx import distinct

The nice extra is that out in user land, it’s all nicely scoped to the mw.tablinx namespace. I’m starting to think about moving the bulk of my project-local bits into this local collection so everything gets scoped.

schedules_all_uc: "{{ schedules_all | mw.tablinx.sch_uniqcomb }}"
whackamole: "{{ foo is mw.tablinx.sch_distinct }}" # The sch_distinct test uses distinct() on the back end.
5 Likes