salt-sproxy icon indicating copy to clipboard operation
salt-sproxy copied to clipboard

Salt 3004 custom modules in state module not access able

Open network-shark opened this issue 2 years ago • 1 comments

Hey @mirceaulinic

Describe the bug I am trying to use a custom module in a state file

Running the execution module via cli does work.

Steps To Reproduce salt-sproxy --sync-all 'xxxx' state.show_sls test_no_custom_mod

{% set custom  = salt['mymodule.my_module_func']() %}

test_output_test:
  test.show_notification:
    - name: Test
    - text: 'Test'
test_output:
  test.show_notification:
    - name: Test
    - text: {{custom}}
│[ERROR   ] Rendering exception occurred
│Traceback (most recent call last):
│  File "/home/myuser/venv/test/lib64/python3.8/site-packages/salt/utils/templates.py", line 502, in render_jinja_tmpl
│    output = template.render(**decoded_context)
│  File "/home/myuser/venv/test/lib64/python3.8/site-packages/jinja2/environment.py", line 1291, in render
│    self.environment.handle_exception()
┤  File "/home/myuser/venv/test/lib64/python3.8/site-packages/jinja2/environment.py", line 925, in handle_exception
│    raise rewrite_traceback_stack(source=source)
│  File "<template>", line 1, in top-level template code
│  File "/home/myuser/venv/test/lib64/python3.8/site-packages/jinja2/sandbox.py", line 391, in call
│    if not __self.is_safe_callable(__obj):
│  File "/home/myuser/venv/test/lib64/python3.8/site-packages/jinja2/sandbox.py", line 275, in is_safe_callable
│    getattr(obj, "unsafe_callable", False) or getattr(obj, "alters_data", False)
│jinja2.exceptions.UndefinedError: 'salt.utils.templates.AliasedLoader object' has no attribute 'mymodule.my_module_func'
│
│During handling of the above exception, another exception occurred:
│
│Traceback (most recent call last):
│  File "/home/myuser/venv/test/lib64/python3.8/site-packages/salt/utils/templates.py", line 261, in render_tmpl
│    output = render_str(tmplstr, context, tmplpath)
│  File "/home/myuser/venv/test/lib64/python3.8/site-packages/salt/utils/templates.py", line 509, in render_jinja_tmpl
│    raise SaltRenderError("Jinja variable {}{}".format(exc, out), buf=tmplstr)
│salt.exceptions.SaltRenderError: Jinja variable 'salt.utils.templates.AliasedLoader object' has no attribute 'mymodule.my_module_func'
│[CRITICAL] Rendering SLS 'base:states.mymodule.test_no_custom_mod' failed: Jinja variable 'salt.utils.templates.AliasedLoader object' has no att
│ribute 'mymodule.my_module_func'

This worked without issues on Salt 3000 . I can't recreate the problem with a regular minion.

╰─ salt-sproxy -V
Salt Version:
           Salt: 3004
    Salt SProxy: 2021.6.1

Dependency Versions:
        Ansible: Not Installed
           cffi: 1.15.0
       dateutil: Not Installed
      docker-py: Not Installed
          gitdb: Not Installed
      gitpython: Not Installed
         Jinja2: 3.0.3
     junos-eznc: 2.6.3
       jxmlease: Not Installed
        libgit2: Not Installed
       M2Crypto: Not Installed
           Mako: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 1.0.3
         NAPALM: 3.4.0
       ncclient: 0.6.9
        Netmiko: 3.4.0
       paramiko: 2.8.1
      pycparser: 2.21
       pycrypto: Not Installed
   pycryptodome: 3.14.1
         pyeapi: 0.8.4
         pygit2: Not Installed
       PyNetBox: Not Installed
          PyNSO: Not Installed
         Python: 3.8.8 (default, Aug 11 2021, 06:52:42)
   python-gnupg: Not Installed
         PyYAML: 6.0
          PyZMQ: 21.0.2
            scp: 0.14.4
          smmap: Not Installed
        textfsm: 1.1.2
        timelib: Not Installed
        Tornado: 4.5.3
            ZMQ: 4.3.3

System Versions:
           dist: rhel 8.5 Ootpa
         locale: utf-8
        machine: x86_64
        release: 4.18.0-348.23.1.el8_5.x86_64
         system: Linux
        version: Red Hat Enterprise Linux 8.5 Ootpa

network-shark avatar May 16 '22 14:05 network-shark

After removing if grains['os'] == 'ios' :it works .

def __virtual__():
    return __virtualname__
#    if __grains__['os'] == 'ios':
#    else:
#        return (False, 'Not a cisco device')

Not sure if this ever worked. It is the first time that I use a custom module within a state

network-shark avatar May 17 '22 08:05 network-shark