question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

custom execution modules not working

See original GitHub issue

Describe the bug Custom execution modules placed in the salt://_modules directory can not be executed.

Steps To Reproduce

  1. create custom module:
root@salt-gbone:/srv/salt# cat _modules/example.py
def first():
    return True
  1. try to run the module:
root@salt-gbone:/srv/salt# salt-sproxy cr-testing* example.first
cr-testing01.lab1:
    The minion function caused an exception: Traceback (most recent call last):
      File "/usr/local/lib/python3.7/dist-packages/salt_sproxy/_runners/proxy.py", line 641, in salt_call
        ret = sa_proxy.functions[salt_function](*args, **kwargs)
      File "/usr/lib/python3/dist-packages/salt/loader.py", line 1278, in __getitem__
        func = super(LazyLoader, self).__getitem__(item)
      File "/usr/lib/python3/dist-packages/salt/utils/lazy.py", line 108, in __getitem__
        raise KeyError(key)
    KeyError: 'example.first'
ERROR: Minions returned with non-zero exit code

Expected behavior Expect the custom module to execute and return “True”

Versions Report

root@salt-gbone:/srv/salt# salt-sproxy -V
Salt Version:
           Salt: 3001.1
    Salt SProxy: 2020.7.0

Dependency Versions:
        Ansible: Not Installed
           cffi: 1.14.2
       dateutil: 2.7.3
      docker-py: Not Installed
          gitdb: 2.0.5
      gitpython: 2.1.11
         Jinja2: 2.10
     junos-eznc: 2.5.3
       jxmlease: Not Installed
        libgit2: 0.27.7
       M2Crypto: Not Installed
           Mako: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 0.5.6
         NAPALM: 3.1.0
       ncclient: 0.6.9
        Netmiko: 3.2.0
       paramiko: 2.7.2
      pycparser: 2.19
       pycrypto: 2.6.1
   pycryptodome: 3.6.1
         pyeapi: 0.8.3
         pygit2: 0.27.4
       PyNetBox: Not Installed
          PyNSO: Not Installed
         Python: 3.7.3 (default, Jul 25 2020, 13:03:44)
   python-gnupg: Not Installed
         PyYAML: 3.13
          PyZMQ: 17.1.2
            scp: 0.13.2
          smmap: 2.0.5
        textfsm: 1.1.0
        timelib: Not Installed
        Tornado: 4.5.3
            ZMQ: 4.3.1

System Versions:
           dist: debian 10 buster
         locale: utf-8
        machine: x86_64
        release: 4.19.0-10-amd64
         system: Linux
        version: Debian GNU/Linux 10 buster

Additional context I followed the documentation for creating custom execution modules here: https://docs.saltstack.com/en/latest/ref/modules/ as well as your blog: https://mirceaulinic.net/2019-04-24-extending-napalm-salt/ https://mirceaulinic.net/2019-06-17-minionless-salt-automation/

This is my file_roots config:

file_roots:
  base:
    - /srv/salt/
    - /srv/salt/ext

Running standard modules on the minion works fine:

root@salt-gbone:/srv/salt# salt-sproxy cr-testing* test.ping
cr-testing01.lab1:
    True

Running saltutil.sync_all, saltutil.sync_modules, sys.reload_modules or saltutil.refresh_modules does not change the outcome. As I understand, running these commands should also not be necessary with salt-sproxy because the modules are loaded on runtime.

The only way I have found to make this work is to place the module either in /usr/lib/python3/dist-packages/salt/modules/ or /usr/local/lib/python3.7/dist-packages/salt_sproxy/_modules/:

root@salt-gbone:/srv/salt# mv _modules/example.py /usr/local/lib/python3.7/dist-packages/salt_sproxy/_modules/
root@salt-gbone:/srv/salt# salt-sproxy cr-testing* example.first
cr-testing01.lab1:
    True

I’m relatively new to salt and salt-sproxy so I can’t rule out that I’m missing something simple here. However I’m unable to figure out what it might be.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
syntax-terr0rcommented, Oct 9, 2020

I can confirm that setting kwargs['sync_modules'] = sync_modules fixed it:

root@salt-gbone:~# salt-sproxy cr-testing* example.first
cr-testing01.mydomain.com:
    True

Thanks for the quick fix!

0reactions
mirceauliniccommented, Oct 9, 2020

@syntax-terr0r could you try #177 to confirm it fixes this? If you’re unable to apply the patch, just setting kwargs['sync_modules'] = sync_modules on line 199 in salt_sproxy/cli.py should normally do it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Classes from custom execution modules are not published
Exporting classes as part of the execution module interface has been unsupported for a while, and it seems this documentation was overlooked.
Read more >
Writing Execution Modules - Salt Project Documentation
Modules Are Easy to Write! Writing Salt execution modules is straightforward. A Salt execution module is a Python or Cython module placed in...
Read more >
SaltStack, unable to run custom state modules - Stack Overflow
Try running the minion in a terminal in the foreground in debug mode: salt-minion -l debug. Then on the master run:
Read more >
Execution Modules - SaltStack
Salt execution modules are the functions called by the salt command. Salt execution modules are different from state modules and cannot be called...
Read more >
importlib — The implementation of import — Python 3.11.1 ...
If you are dynamically importing a module that was created since the interpreter began execution (e.g., created a Python source file), you may...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found