Skip to content

[Bug]: fix for #1427 - netbox_service - More than one result returned for ... #1479

@cernymi

Description

@cernymi

Ansible NetBox Collection version

latest devel branch after: fixes #1426 : broken netbox_service module #1427

Ansible version

ansible [core 2.18.8]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/<user>/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/<user>/.local/lib/python3.13/site-packages/ansible
  ansible collection location = /home/<user>/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.13.9 (main, Oct 14 2025, 00:00:00) [GCC 15.2.1 20250808 (Red Hat 15.2.1-1)] (/usr/bin/python3)
  jinja version = 3.1.6
  libyaml = True

NetBox version

4.3.0 and later

Python version

3.11

Steps to Reproduce

  1. Create whatever VM
  2. Create service for VM
    netbox.netbox.netbox_service:
      netbox_url: "{{ netbox_url }}"
      netbox_token: "{{ netbox_rw_token }}"
      validate_certs: "{{ netbox_validate_certs }}"
      data:
        name: "{{ service_create.name | mandatory }}"
        protocol: "{{ service_create.protocol | lower | mandatory }}"
        description: "{{ service_create.description | default(omit) }}"
        device: "{{ service_create.device | default(omit) }}"
        ipaddresses: "{{ service_create.ipaddresses | default(omit) }}"
        port: "{{ service_create.port | default(omit) }}"
        ports: "{{ service_create.ports | default(omit) }}"
        tags: "{{ service_create.tags | default(omit) }}"
        virtual_machine: "{{ service_create.virtual_machine | default(tenant_vm.name) }}"
    vars:
      service_create:
        name: test_service_https
        protocol: tcp
        virtual_machine: <existing_vm>
        ports: [443]

Expected Behavior

Be able to create/lookup more than one service

Nothing new, just fix for #1427

Observed Behavior

And now we can test netbox_service is failing on:

services = 0 - if there's no service with this name in Netbox - just OK - is created

services = 1 - if there's exactly one service with this name in Netbox - always reassign service to last ```parent_object_id` - KO

--- before
+++ after
@@ -1,2 +1,2 @@
-    parent_object_id: 267
+    parent_object_id: 264

services > 1 - if there are more services with same name already in Netbox - KO

 msg: More than one result returned for ...

This change is maybe missing parent_object_type and parent_object_id in query set?

diff --git a/plugins/module_utils/netbox_utils.py b/plugins/module_utils/netbox_utils.py
index 2d55498..e2dee16 100644
--- a/plugins/module_utils/netbox_utils.py
+++ b/plugins/module_utils/netbox_utils.py
@@ -558,7 +558,7 @@ ALLOWED_QUERY_PARAMS = {
    "rir": set(["slug"]),
    "role": set(["slug"]),
    "route_target": set(["name"]),
-    "services": set(["device", "virtual_machine", "name", "port", "protocol"]),
+    "services": set(["device", "virtual_machine", "name", "port", "protocol", "parent_object_type", "parent_object_id"]),
    "service_template": set(["name"]),
    "site": set(["slug", "name"]),
    "site_group": set(["slug"]),

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions