ansible-collections / community.grafana

Grafana Collection for Ansible
http://galaxy.ansible.com/community/grafana
GNU General Public License v3.0
129 stars 82 forks source link

AttributeError 'NoneType' object has no attribute get - Module failure #333

Open enidvx opened 10 months ago

enidvx commented 10 months ago

Hello, I am getting this error when trying to execute grafana_user module:

File \"master:/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 304, in <module>\n  File \"master:/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 286, in main\n  File \"master:/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 232, in is_user_update_required\nAttributeError: 'NoneType' object has no attribute 'get'\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

I am using:

community.grafana 1.6.1
grafana version 10.2.1
ansible==8.3.0
ansible-core==2.15.3

I cannot execute this task at all:

  community.grafana.grafana_user:
    url: "https://{{ ansible_fqdn }}"
    url_username: "{{ grafana_admin_user }}"
    url_password: "{{ grafana_admin_password }}"
    login: "{{ member.uid }}"
    email: "{{ member.email }}"
    name: "{{ member.description }}"
    password: "{{ member.password }}"
    state: "present"

Is there anything I can do to solve the issue? Thanks

Nemental commented 10 months ago

Hi @enidvx Can you please provide your members variable (anonymized)? Do you know which user/item of the list is affected? As I understand it, the problem is that the target user doesn't have an email address. But how is that possible? :D

enidvx commented 10 months ago

Hi @Nemental These are some of the vars that are used to loop on users list:

'uid': 'username', 
'description': 'User Name', 
'email': 'user.name@whatever.com', 
'password': 'hashed_passwd'

From all the config all vars seem to be fine because this used to work on another older instance of grafana and community.grafana.

Nemental commented 10 months ago

Hey @enidvx I'm sorry but I can't recreate this... I tried several things, even edited the Grafana database manually but without success.

enidvx commented 10 months ago

I even tested it with grafana version 10.2.0 which according to the community.grafana Readme is the last supported version and I do get the exact same error:

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'NoneType' object has no attribute 'get'
fatal: [grafana-1.domain.io]: FAILED! => {"changed": false, "module_stderr": "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_community.grafana.grafana_user_payload_ed5er12q/ansible_community.grafana.grafana_user_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 304, in <module>\n  File \"/tmp/ansible_community.grafana.grafana_user_payload_ed5er12q/ansible_community.grafana.grafana_user_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 286, in main\n  File \"/tmp/ansible_community.grafana.grafana_user_payload_ed5er12q/ansible_community.grafana.grafana_user_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 232, in is_user_update_required\nAttributeError: 'NoneType' object has no attribute 'get'\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

I even tested with and without mitogen enabled (strategy: linear, and strategy: mitogen_linear) with the same error :disappointed:

Demisu commented 7 months ago

Hello,

I have the same issue when creating or updating new users. I have a fresh install of grafana 9.2.20, but I'm using the PMM version and I don't know if it is related.

Ansible 9.3.0 Ansible-core 2.16.4 community.grafana 1.8.0

enidvx commented 5 months ago

I tried again with the latest versions and also a fresh install in hope to have some fix but unfortunately again the same output:

community.grafana 1.8.0 grafana version 10.2.2 ansible==8.6.0 ansible-core==2.15.6

still error is:

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'NoneType' object has no attribute 'get' fatal: [grafana-1.host.domain]: FAILED! => {"changed": false, "module_stderr": "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_community.grafana.grafana_user_payload_rj6zf5tf/ansible_community.grafana.grafana_user_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 323, in <module>\n File \"/tmp/ansible_community.grafana.grafana_user_payload_rj6zf5tf/ansible_community.grafana.grafana_user_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 303, in main\n File \"/tmp/ansible_community.grafana.grafana_user_payload_rj6zf5tf/ansible_community.grafana.grafana_user_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 247, in is_user_update_required\nAttributeError: 'NoneType' object has no attribute 'get'\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

rndmh3ro commented 5 months ago

I can reproduce a similar bug:

  1. run grafana

    > docker run -d -p 3000:3000 grafana/grafana:10.2.2
  2. run the following playbook:

    
    ---
    - hosts: localhost
    gather_facts: no
    vars:
    grafana_admin_user: grafana
    grafana_admin_password: grafana
    members:
      - uid: username
        description: 'User Name'
        email: 'user.name@whatever.com'
        password: 'hashed_passwd'
      - uid: username2
        description: 'User Name1'
        email: 'user.name2@whatever.com'
        password: 'hashed_passwd'
    tasks:
    - name: Create Grafana users
      community.grafana.grafana_user:
        url: "https://127.0.0.1"        # ## notice this is wrong
        url_username: "{{ grafana_admin_user }}"
        url_password: "{{ grafana_admin_password }}"
        login: "{{ member.uid }}"
        email: "{{ member.email }}"
        name: "{{ member.description }}"
        password: "{{ member.password }}"
        state: "present"
      loop: "{{ members }}"
      loop_control:
        loop_var: member

result:

ansible-playbook test2.yml [WARNING]: No inventory was parsed, only implicit localhost is available [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


< PLAY [localhost] >

\ ^^ \ (oo)_____ (__)\ )\/\ ----w

< TASK [Create Grafana users] >

    \   ^__^
     \  (oo)\_______
        (__)\       )\/\
            ||----w |
            ||     ||

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'NoneType' object has no attribute 'read' failed: [localhost] (item={'uid': 'username', 'description': 'User Name', 'email': 'user.name@whatever.com', 'password': 'hashed_passwd'}) => {"ansible_loop_var": "member", "changed": false, "member": {"description": "User Name", "email": "user.name@whatever.com", "password": "hashed_passwd", "uid": "username"}, "module_stderr": "Traceback (most recent call last):\n File \"/home/segu/.ansible/tmp/ansible-tmp-1713969893.8799918-1346142-270361372579429/AnsiballZ_grafana_user.py\", line 107, in \n _ansiballz_main()\n File \"/home/segu/.ansible/tmp/ansible-tmp-1713969893.8799918-1346142-270361372579429/AnsiballZ_grafana_user.py\", line 99, in _ansiballz_main\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n File \"/home/segu/.ansible/tmp/ansible-tmp-1713969893.8799918-1346142-270361372579429/AnsiballZ_grafana_user.py\", line 47, in invoke_module\n runpy.run_module(mod_name='ansible_collections.community.grafana.plugins.modules.grafana_user', init_globals=dict(_module_fqn='ansible_collections.community.grafana.plugins.modules.grafana_user', _modlib_path=modlib_path),\n File \"/usr/lib/python3.10/runpy.py\", line 224, in run_module\n return _run_module_code(code, init_globals, run_name, mod_spec)\n File \"/usr/lib/python3.10/runpy.py\", line 96, in _run_module_code\n _run_code(code, mod_globals, init_globals,\n File \"/usr/lib/python3.10/runpy.py\", line 86, in _run_code\n exec(code, run_globals)\n File \"/tmp/ansible_community.grafana.grafana_user_payload_ctp3n329/ansible_community.grafana.grafana_user_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 323, in \n File \"/tmp/ansible_community.grafana.grafana_user_payload_ctp3n329/ansible_community.grafana.grafana_user_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 292, in main\n File \"/tmp/ansible_community.grafana.grafana_user_payload_ctp3n329/ansible_community.grafana.grafana_user_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 221, in get_user_from_login\n File \"/tmp/ansible_community.grafana.grafana_user_payload_ctp3n329/ansible_community.grafana.grafana_user_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 204, in _send_request\nAttributeError: 'NoneType' object has no attribute 'read'\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1} An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'NoneType' object has no attribute 'read' failed: [localhost] (item={'uid': 'username2', 'description': 'User Name1', 'email': 'user.name2@whatever.com', 'password': 'hashed_passwd'}) => {"ansible_loop_var": "member", "changed": false, "member": {"description": "User Name1", "email": "user.name2@whatever.com", "password": "hashed_passwd", "uid": "username2"}, "module_stderr": "Traceback (most recent call last):\n File \"/home/segu/.ansible/tmp/ansible-tmp-1713969894.262415-1346142-167829676315139/AnsiballZ_grafana_user.py\", line 107, in \n _ansiballz_main()\n File \"/home/segu/.ansible/tmp/ansible-tmp-1713969894.262415-1346142-167829676315139/AnsiballZ_grafana_user.py\", line 99, in _ansiballz_main\n invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n File \"/home/segu/.ansible/tmp/ansible-tmp-1713969894.262415-1346142-167829676315139/AnsiballZ_grafana_user.py\", line 47, in invoke_module\n runpy.run_module(mod_name='ansible_collections.community.grafana.plugins.modules.grafana_user', init_globals=dict(_module_fqn='ansible_collections.community.grafana.plugins.modules.grafana_user', _modlib_path=modlib_path),\n File \"/usr/lib/python3.10/runpy.py\", line 224, in run_module\n return _run_module_code(code, init_globals, run_name, mod_spec)\n File \"/usr/lib/python3.10/runpy.py\", line 96, in _run_module_code\n _run_code(code, mod_globals, init_globals,\n File \"/usr/lib/python3.10/runpy.py\", line 86, in _run_code\n exec(code, run_globals)\n File \"/tmp/ansible_community.grafana.grafana_user_payload_vtib7xty/ansible_community.grafana.grafana_user_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 323, in \n File \"/tmp/ansible_community.grafana.grafana_user_payload_vtib7xty/ansible_community.grafana.grafana_user_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 292, in main\n File \"/tmp/ansible_community.grafana.grafana_user_payload_vtib7xty/ansible_community.grafana.grafana_user_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 221, in get_user_from_login\n File \"/tmp/ansible_community.grafana.grafana_user_payload_vtib7xty/ansible_community.grafana.grafana_user_payload.zip/ansible_collections/community/grafana/plugins/modules/grafana_user.py\", line 204, in _send_request\nAttributeError: 'NoneType' object has no attribute 'read'\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}


< PLAY RECAP >

    \   ^__^
     \  (oo)\_______
        (__)\       )\/\
            ||----w |
            ||     ||

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

fernanfs commented 1 month ago

I've stumbled upon this error as well and could reproduce it. If I have a password that contains an exclamation mark, the invocation fails. To simulate this, I've changed my normal invocation to the following (basically replacing the password with a static one):

- name: Configure Users
  community.grafana.grafana_user:
    url: "https://{{ grafana_fqdn }}"
    url_username: "{{ grafana_admin_user }}"
    url_password: "{{ grafana_admin_password }}"
    name: "{{ item.name }}"
    email: "{{ item.email }}"
    login: "{{ item.login }}"
    password: "te!hst" #"{{ item.password }}"
    is_admin: "{{ item.is_admin }}"
    state: present
  loop: "{{ grafana_users }}"
  loop_control:
    label: "{{ item.login }}"

The configuration above fails. If I'm using a password without an exclamation mark it works well:

- name: Configure Users
  community.grafana.grafana_user:
    url: "https://{{ grafana_fqdn }}"
    url_username: "{{ grafana_admin_user }}"
    url_password: "{{ grafana_admin_password }}"
    name: "{{ item.name }}"
    email: "{{ item.email }}"
    login: "{{ item.login }}"
    password: "test" #"{{ item.password }}"
    is_admin: "{{ item.is_admin }}"
    state: present
  loop: "{{ grafana_users }}"
  loop_control:
    label: "{{ item.login }}"

It should be noted, that invoking it with the password "te!hst" fails (which actually is a typo) and with "te!st" actually succeeds.