Open binocvlar opened 4 years ago
I'd avoid using nested modules and stick with a flat module structure (like with the built-in modules). The Salt Loader has the following constraints (design decisions):
module.function
notation for salt-call
and salt['module.function']
for Jinja)__salt__
, __pillar__
, __grains__
, etc) into functions that are loaded through it (not imported directly), so you have to use the __salt__['module.function']
to make calls across module boundariesSo, to fix the example code you would need:
lumberjack.py
, lumberjack_work.py
, and lumberjack_sleep.py
__salt__
dunder: def is_ok(person):
""" Checks whether a person is really a lumberjack """
return __salt__['lumberjack_sleep.all_night'](person) and __salt__['lumberjack_work.all_day'](person)
I guess the documentation you linked is a bit misleading (the example is clearly unable to handle your use-case).
Thanks for your feedback @max-arnold : FWIW, I've already changed my code to have a flat structure, and am aware (through experimentation/reading the code) that the dunder dictionaries are only injected into the the outermost layer of the module.
The primary issues that I see are as follows:
With respect to point 2, an improved interface for interacting with pillar, grains etc would be a massive improvement. Given that we're in python at this point, it would be nice if we could simply import this data, or make a function call which returns it. This would be way better than dunder dictionaries which are injected at runtime (my IDE rightly hates references to undefined variables, for example), but I'm aware that this particular ship has probably sailed... Point is, I don't actually care that the dunder dictionaries aren't present - what makes life difficult is that I don't think there is a supported alternative method to obtain the data that they contain.
@binocvlar Thanks for the report. And thank you to @max-arnold for the explanation on the current limitation in the Salt loader. A lot of these limitations are fixed with some of the new ideas going into POP. I'm labeling this as a documentation bug as we should be calling out that the various Salt dunder functions are not available in any nested files.
I was about to leave a comment about POP, but Gareth beat me :)
POP/Idem are more Pythonic, so the magic dunders are gone. Instead, each function receives an explicit Hub argument (Explicit is better than implicit
): https://pop-book.readthedocs.io/en/latest/main/hub.html This is much more IDE- and import-friendly.
Also, Salt Magnesium (3002) is going to ship with the experimental Idem support: https://github.com/saltstack/salt/pull/58119
Description
https://docs.saltstack.com/en/latest/ref/modules/index.html seems to be rather misleading:
__salt__
and__pillar__
are not available in execution module functions called from another file in the module. E.g.lumberjack.is_ok()
has access to__salt__
and__pillar__
, butsleep.all_night()
(which is within the modulelumberjack
, and is called bylumberjack.is_ok()
) cannot access__salt__
or__pillar__
When calling module functions with
salt-call
, it seems as though modules cannot be more than one level deep (i.e. you can callfoo.bar()
, but notfoo.bar.baz()
)Setup
Directory structure of the execution module called
lumberjack
:lumberjack/__init__.py
:lumberjack/sleep/__init__.py
:lumberjack/work/__init__.py
:Steps to Reproduce the behavior
lumberjack
directory as described into_modules/
salt-call saltutil.sync_all
on a minionsalt-call lumberjack.is_ok "Michael Palin"
on the same minionNote that adding all functions into the same file seems to fix the first issue (i.e.
__salt__
and__pillar__
missing)Expected behavior I expected
__grains__
et al to be available from other functions within the same module. I also expected to be able to structure my module arbitrarily deep. If there's a technical reason why either of these expectations weren't met, then I'd expect the documentation to make these caveats clear.Screenshots
Versions Report
salt --versions-report
(Provided by running salt --versions-report. Please also mention any differences in master/minion versions.) ``` Salt Version: Salt: 3001.1 Dependency Versions: cffi: Not Installed cherrypy: Not Installed dateutil: 2.7.3 docker-py: Not Installed gitdb: 2.0.6 gitpython: 3.0.7 Jinja2: 2.10.1 libgit2: Not Installed M2Crypto: Not Installed Mako: Not Installed msgpack-pure: Not Installed msgpack-python: 0.6.2 mysql-python: Not Installed pycparser: Not Installed pycrypto: 2.6.1 pycryptodome: 3.6.1 pygit2: Not Installed Python: 3.8.2 (default, Jul 16 2020, 14:00:26) python-gnupg: 0.4.5 PyYAML: 5.3.1 PyZMQ: 18.1.1 smmap: 2.0.5 timelib: Not Installed Tornado: 4.5.3 ZMQ: 4.3.2 System Versions: dist: ubuntu 20.04 focal locale: utf-8 machine: x86_64 release: 5.4.0-45-generic system: Linux version: Ubuntu 20.04 focal ```Additional context Add any other context about the problem here.