netbox-community / ansible_modules

NetBox modules for Ansible using Ansible Collections
GNU General Public License v3.0
321 stars 208 forks source link

[Bug]: Cannot add contact_group to contact #1182

Open renekrieg opened 6 months ago

renekrieg commented 6 months ago

Ansible NetBox Collection version

v3.16.0

Ansible version

ansible [core 2.16.3]
  config file = /Users/renekrieg/playground/private/own-projects/homelab-network/configuration/netbox/ansible/ansible.cfg
  configured module search path = ['/Users/renekrieg/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/renekrieg/playground/private/own-projects/homelab-network/configuration/netbox/ansible/.venv/lib/python3.11/site-packages/ansible
  ansible collection location = /Users/renekrieg/.ansible/collections:/usr/share/ansible/collections
  executable location = /Users/renekrieg/playground/private/own-projects/homelab-network/configuration/netbox/ansible/.venv/bin/ansible
  python version = 3.11.7 (main, Dec  4 2023, 18:10:11) [Clang 15.0.0 (clang-1500.1.0.2.5)] (/Users/renekrieg/playground/private/own-projects/homelab-network/configuration/netbox/ansible/.venv/bin/python3)
  jinja version = 3.1.3
  libyaml = True

NetBox version

v3.7.2

Python version

3.11

Steps to Reproduce

The following playbook is expected to create a new contact "test" and assign the contact to the contact_group "Administrator".

---
- name: Create Contacts within NetBox
  netbox.netbox.netbox_contact:
    netbox_url: "{{ lookup('ansible.builtin.env', 'NETBOX_API') }}"
    netbox_token: "{{ lookup('ansible.builtin.env', 'NETBOX_TOKEN') }}"
    data: 
      name: test
      contact_group: Administrator
    state: present

Expected Behavior

Contact "test" should be assigned to Group "Administrator".

Observed Behavior

Netbox API throws this error: image

PLAY [PLAY 2 - Create Contact Groups] ****************************************************************************************************************************************************************************************************************************************************

TASK [create_contact_groups : Create Contact Groups within NetBox] ***********************************************************************************************************************************************************************************************************************
changed: [localhost] => (item=Administrator)
changed: [localhost] => (item=Customer)

PLAY [PLAY 3 - Create Contacts] **********************************************************************************************************************************************************************************************************************************************************

TASK [create_contacts : Create Contacts within NetBox] ***********************************************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "{\"group_id\":[\"“group” is not a valid value.\"]}"}

PLAY RECAP *******************************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
Rovansek commented 5 months ago

I am getting the same when I try to do parent and sub groups.

liquid-metal commented 2 months ago

Ran into the same problem and did some digging.

When the contact module is executed, the constructor of NetboxModule is called (through NetboxTenancyModule) and in turn calls NetboxModule._convert_identical_keys (see plugins/module_utils/netbox_utils.py#L739). That one essentially uses CONVERT_KEYSto map the contact_group to group.

Later on in the module execution, NetboxModule._build_query_params is started, and in plugins/module_utils/netbox_utils.py#L956 this key that was converted to group is found in QUERY_PARAM_IDS, which in turn provokes NetboxModule._get_query_param_id to map the group key in CONVERT_TO_ID to tenant_groups, which is clearly not desired.

Still looking further, but I am a bit uncertain on how to resolve that.