lucasheld / ansible-uptime-kuma

Ansible collection of modules to configure Uptime Kuma
GNU General Public License v3.0
144 stars 19 forks source link

Module not usable 'NameError: name 'MonitorType' is not defined' #50

Open Hornochs opened 3 months ago

Hornochs commented 3 months ago

Hey there,

I'm not able to use the Modules :( Following is the Error:

Using module file /Users/stephan.schaffner/.ansible/collections/ansible_collections/lucasheld/uptime_kuma/plugins/modules/monitor.py
Pipelining is enabled.
<10.24.1.7> ESTABLISH SSH CONNECTION FOR USER: stephan
<10.24.1.7> SSH: EXEC ssh -o ForwardAgent=yes -o ControlMaster=auto -o ControlPersist=10m -o ControlPath=/tmp/ansible_ssh_%r@%h:%p -o StrictHostKeyChecking=no -o Port=42022 -o 'IdentityFile="äou"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="stephan"' -o ConnectTimeout=5 10.24.1.7 '/bin/sh -c '"'"'sudo -H -S -p "[sudo via ansible, key=q] password:" -u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-q ; /usr/bin/python3'"'"'"'"'"'"'"'"' && sleep 0'"'"''
Escalation succeeded
<10.24.1.7> (1, b'', b'Traceback (most recent call last):\n  File "<stdin>", line 107, in <module>\n  File "<stdin>", line 99, in _ansiballz_main\n  File "<stdin>", line 47, in invoke_module\n  File "<frozen runpy>", line 226, in run_module\n  File "<frozen runpy>", line 98, in _run_module_code\n  File "<frozen runpy>", line 88, in _run_code\n  File "/tmp/ansible_lucasheld.uptime_kuma.monitor_payload_x99wq4wu/ansible_lucasheld.uptime_kuma.monitor_payload.zip/ansible_collections/lucasheld/uptime_kuma/plugins/modules/monitor.py", line 589, in <module>\n  File "/tmp/ansible_lucasheld.uptime_kuma.monitor_payload_x99wq4wu/ansible_lucasheld.uptime_kuma.monitor_payload.zip/ansible_collections/lucasheld/uptime_kuma/plugins/modules/monitor.py", line 449, in main\nNameError: name \'MonitorType\' is not defined\n')
<10.24.1.7> Failed to connect to the host via ssh: Traceback (most recent call last):
  File "<stdin>", line 107, in <module>
  File "<stdin>", line 99, in _ansiballz_main
  File "<stdin>", line 47, in invoke_module
  File "<frozen runpy>", line 226, in run_module
  File "<frozen runpy>", line 98, in _run_module_code
  File "<frozen runpy>", line 88, in _run_code
  File "/tmp/ansible_lucasheld.uptime_kuma.monitor_payload_x99wq4wu/ansible_lucasheld.uptime_kuma.monitor_payload.zip/ansible_collections/lucasheld/uptime_kuma/plugins/modules/monitor.py", line 589, in <module>
  File "/tmp/ansible_lucasheld.uptime_kuma.monitor_payload_x99wq4wu/ansible_lucasheld.uptime_kuma.monitor_payload.zip/ansible_collections/lucasheld/uptime_kuma/plugins/modules/monitor.py", line 449, in main
NameError: name 'MonitorType' is not defined
The full traceback is:
Traceback (most recent call last):
  File "<stdin>", line 107, in <module>
  File "<stdin>", line 99, in _ansiballz_main
  File "<stdin>", line 47, in invoke_module
  File "<frozen runpy>", line 226, in run_module
  File "<frozen runpy>", line 98, in _run_module_code
  File "<frozen runpy>", line 88, in _run_code
  File "/tmp/ansible_lucasheld.uptime_kuma.monitor_payload_x99wq4wu/ansible_lucasheld.uptime_kuma.monitor_payload.zip/ansible_collections/lucasheld/uptime_kuma/plugins/modules/monitor.py", line 589, in <module>
  File "/tmp/ansible_lucasheld.uptime_kuma.monitor_payload_x99wq4wu/ansible_lucasheld.uptime_kuma.monitor_payload.zip/ansible_collections/lucasheld/uptime_kuma/plugins/modules/monitor.py", line 449, in main
NameError: name 'MonitorType' is not defined
fatal: [netbox.vv.de]: FAILED! => changed=false 
  module_stderr: |-
    Traceback (most recent call last):
      File "<stdin>", line 107, in <module>
      File "<stdin>", line 99, in _ansiballz_main
      File "<stdin>", line 47, in invoke_module
      File "<frozen runpy>", line 226, in run_module
      File "<frozen runpy>", line 98, in _run_module_code
      File "<frozen runpy>", line 88, in _run_code
      File "/tmp/ansible_lucasheld.uptime_kuma.monitor_payload_x99wq4wu/ansible_lucasheld.uptime_kuma.monitor_payload.zip/ansible_collections/lucasheld/uptime_kuma/plugins/modules/monitor.py", line 589, in <module>
      File "/tmp/ansible_lucasheld.uptime_kuma.monitor_payload_x99wq4wu/ansible_lucasheld.uptime_kuma.monitor_payload.zip/ansible_collections/lucasheld/uptime_kuma/plugins/modules/monitor.py", line 449, in main
    NameError: name 'MonitorType' is not defined
  module_stdout: ''
  msg: |-
    MODULE FAILURE
    See stdout/stderr for the exact error
  rc: 1
TheAssassin commented 3 months ago

I ran into the same issue, and I don't seem to be able to work around it.

The issue is caused by the fact that the module is needed in the type annotation here. This works fine when the module can be imported, but if not, the global variable that signals the import error to the remaining code is not interpreted within time. I suggest to just have the import fail.

For the record, I'm using ansible with pipx. I injected the dependency library and I can import it in the Python interpreter within the virtualenv created by pipx.

Hornochs commented 3 months ago

Are you able to tell me which module you injected via pipx? Unterwegs gesendet

Am 22.06.2024 um 02:13 schrieb TheAssassin - notifications at github.com @.***>:



I ran into the same issue, and I don't seem to be able to work around it.

The issue is caused by the fact that the module is needed in the type annotation here. This works fine when the module can be imported, but if not, the global variable that signals the import error to the remaining code is not interpreted within time. I suggest to just have the import fail.

For the record, I'm using ansible with pipx. I injected the dependency library and I can import it in the Python interpreter within the virtualenv created by pipx.

— Reply to this email directly, view it on GitHubhttps://github.com/lucasheld/ansible-uptime-kuma/issues/50#issuecomment-2183600284, or unsubscribehttps://github.com/notifications/unsubscribe-auth/APH6BQH5AJHIYX2GOWTWAHTZIS6RVAVCNFSM6AAAAABJIAGNB2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBTGYYDAMRYGQ. You are receiving this because you authored the thread.Message ID: @.***>

TheAssassin commented 3 months ago
pipx inject ansible uptime-kuma-api==1.2.1

It's the equivalent of running pip in there to install the module mentioned in the README. However, it does not solve the problem.

TheAssassin commented 3 months ago

I debugged the problem further. The issue is linked to how ansible runs modules on remote hosts. And yes, now it seems quite a bit more obvious to me than it used to.

Please see https://docs.ansible.com/ansible/latest/dev_guide/developing_program_flow_modules.html#ansiballz-framework. When ansible executes a module from any collection, the module needs to be transferred to the remote host. Including the library in the local interpreter doesn't make any difference. It'd need to be bundled with this collection's code to be available to the target system.

Executing a playbook with connection resp. transport set to local does not make any difference. No matter what you do, ansible will pick some interpreter (likely the newest one that is available, according to my testing) and won't include the library. You can sometimes hack around this by setting PYTHONPATH in the process's environment, but it's not a reliable solution.

At any rate, you likely want to execute these modules locally even when using an inventory anyway, so setting connection: local in your playbook is a good idea. I still need to figure out how to make this collection use the installed Python package.

Edit: the instructions in the README are at least incomplete and definitely misleading.

TheAssassin commented 3 months ago

What seems to work is to use delegate_to: 127.0.0.1 in all respective tasks.

arhue commented 1 month ago

Same issue. delegate_to didn't work for me

arhue commented 1 month ago

After installing uptime-kuma-api as root it did work