netbox-community / ansible_modules

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

[Bug]: No way to make netbox_prefix with first_available idempotent #1212

Open gc-nathanh opened 4 months ago

gc-nathanh commented 4 months ago

Ansible NetBox Collection version

v3.15.0

Ansible version

ansible [core 2.15.9]
  config file = None
  configured module search path = ['/home/stack/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/stack/tools/lib64/python3.9/site-packages/ansible
  ansible collection location = /home/stack/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/stack/tools/bin/ansible
  python version = 3.9.18 (main, Jan  4 2024, 00:00:00) [GCC 11.4.1 20230605 (Red Hat 11.4.1-2)] (/home/stack/tools/bin/python3)
  jinja version = 3.1.3
  libyaml = True

NetBox version

v3.7.3

Python version

3.9

Steps to Reproduce

Create a new prefix dynamically using

- name: Lets create some prefixes
    netbox.netbox.netbox_prefix:
      netbox_url: "{{ netboxurl }}"
      netbox_token: "{{ netboxtoken }}"
      data:
        parent: "10.100.248.0/22"
        prefix_length: 30
        description: "spine1-leaf1-p2p"
      first_available: true
      state: present

to create a new /30 prefix, then run the playbook a second time.

Expected Behavior

I expected the prefix to be created on the first playbook run, and no new prefixes created on the second run.

Observed Behavior

Running the playbook a second time creates another dynamically assigned prefix with the same description.

I have attempted to use query_params: attempt to ensure uniqueness

- name: Lets create some prefixes
    netbox.netbox.netbox_prefix:
      netbox_url: "{{ netboxurl }}"
      netbox_token: "{{ netboxtoken }}"
      data:
        parent: "10.100.248.0/22"
        prefix_length: 30
        description: "spine1-leaf1-p2p"
      first_available: true
      state: present
      query_params:
      - description

results in a failure to create: fatal: [spine1]: FAILED! => {"changed": false, "msg": "{\"prefix\":[\"Duplicate prefix found in global table: 10.100.248.36/30\"]}"}

aopdal commented 2 months ago

I think the implementation works like it should be. I want to get the first available prefix when I ask for it. If I run the playbook again I do it because I want a new prefix which is the next free now. I use it to get new prefixes when I need a new prefix. You should probably use a variable to create your description so you don't get the same description on all your prefixes.