lucasheld / ansible-uptime-kuma

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

Maintenance module import error #11

Closed larsl-net closed 1 year ago

larsl-net commented 1 year ago

When trying to use the maintenance module, I get the following error

PLAY [Start maintenance] ************************************************************************************************************************************************************************************************************************************************************

TASK [Resume maintenance] ***********************************************************************************************************************************************************************************************************************************************************
Monday 09 January 2023  20:07:09 +0000 (0:00:00.047)       0:00:00.047 ******** 
Monday 09 January 2023  20:07:09 +0000 (0:00:00.043)       0:00:00.043 ******** 
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (uptime_kuma_api) on 7b746f49e7d9's Python /usr/local/bin/python3.11. Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"}

PLAY RECAP **************************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

After removing the try around the import statement in the module, I have got the following error: ImportError: cannot import name 'MaintenanceType' from 'uptime_kuma_api'

PLAY [Start maintenance] ************************************************************************************************************************************************************************************************************************************************************

TASK [Resume maintenance] ***********************************************************************************************************************************************************************************************************************************************************
Monday 09 January 2023  20:06:30 +0000 (0:00:00.047)       0:00:00.047 ******** 
Monday 09 January 2023  20:06:30 +0000 (0:00:00.044)       0:00:00.044 ******** 
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ImportError: cannot import name 'MaintenanceType' from 'uptime_kuma_api' (/usr/local/lib/python3.11/site-packages/uptime_kuma_api/__init__.py)
fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n  File \"/root/.ansible/tmp/ansible-tmp-1673294790.7070203-123-155382996944439/AnsiballZ_maintenance.py\", line 107, in <module>\n    _ansiballz_main()\n  File \"/root/.ansible/tmp/ansible-tmp-1673294790.7070203-123-155382996944439/AnsiballZ_maintenance.py\", line 99, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/root/.ansible/tmp/ansible-tmp-1673294790.7070203-123-155382996944439/AnsiballZ_maintenance.py\", line 47, in invoke_module\n    runpy.run_module(mod_name='ansible_collections.lucasheld.uptime_kuma.plugins.modules.maintenance', init_globals=dict(_module_fqn='ansible_collections.lucasheld.uptime_kuma.plugins.modules.maintenance', _modlib_path=modlib_path),\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.maintenance_payload_9rmidi5k/ansible_lucasheld.uptime_kuma.maintenance_payload.zip/ansible_collections/lucasheld/uptime_kuma/plugins/modules/maintenance.py\", line 156, in <module>\nImportError: cannot import name 'MaintenanceType' from 'uptime_kuma_api' (/usr/local/lib/python3.11/site-packages/uptime_kuma_api/__init__.py)\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

PLAY RECAP **************************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Version:

/ # ansible --version
ansible [core 2.14.1]
  config file = /data/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.11/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.11.1 (main, Dec  8 2022, 01:03:47) [GCC 12.2.1 20220924] (/usr/local/bin/python3)
  jinja version = 3.1.2
  libyaml = False
lucasheld commented 1 year ago

Thank you for reporting this bug. This is a copy paste error. To solve the problem it should be enough to delete MaintenanceType from the import. I will provide a bugfix tomorrow.

larsl-net commented 1 year ago

Yes, this problem is solved by removing the MaintenanceType from the import.

Another problem is that I have 2fa enabled, which is solved by adding a new parameter api_2fa and updating the login function with this token. plugins/module_utils/common.py

common_module_args = dict(
    api_url=dict(type="str", default="http://127.0.0.1:3001"),
    api_username=dict(type="str"),
    api_password=dict(type="str", no_log=True),
    api_token=dict(type="str", no_log=True),
    api_2fa=dict(type="str", default="", no_log=True)
)

plugins/modules/login.py

def run(api, params, result):
    api_username = params["api_username"]
    api_password = params["api_password"]
    api_2fa = params["api_2fa"]

    r = api.login(api_username, api_password, api_2fa)
    result["token"] = r["token"]

Should I open an PR for that and have you a preference for the parameter name?

lucasheld commented 1 year ago

That would be great, thanks! I totally missed that I haven't implemented login with 2FA yet. The parameter name is fine.