infobloxopen / infoblox-ansible

Ansible modules for interfacing to Infoblox systems
GNU General Public License v3.0
54 stars 60 forks source link

Error "Can not find requested number of networks" using nios_next_network lookup #156

Closed miroslavkohutik closed 1 year ago

miroslavkohutik commented 1 year ago

Infoblox returns an error: Can not find requested number of networks when trying to look up the next available network. My playbook:

- hosts: localhost
  vars:
    nios_provider:
      host: 10.0.0.1
      username: user
      password: password
      wapi_version: 2.11.1
  tasks:
    - name: Create subnet
      infoblox.nios_modules.nios_network:
        provider: "{{ nios_provider }}"
        network: "{{ lookup('nios_next_network', '192.168.0.0/24', cidr=28, num=1, provider=nios_provider ) }}"

The full error message:

infoblox_client.exceptions.InfobloxFuncException: Error occurred during function's 'next_available_network' call: ref networkcontainer/ZG5zLm5ldHdvcmtfY29udGFpbmVyJDE5Mi4xNjguMC4wLzI0LzE:192.168.0.0/24/ipam: b'{ "Error": "AdmConDataError: None (IBDataError: IB.Data:Can not find requested number of networks)", \n  "code": "Client.Ibap.Data", \n  "text": "Can not find requested number of networks"\n}' [code 400]
philipsd6 commented 1 year ago

This is due to this line:

https://github.com/infobloxopen/infoblox-ansible/blob/1a64be31da12be1a300019d13ba38da788da47fc/plugins/lookup/nios_next_network.py#L99

Since there can be multiple network views containing the same networks, we need to ensure that the correct one in the list is picked, not just the first one.

For example in our test Infoblox, when we get the networkcontainer for the 192.168.0.0/24 network, we get the following list:

[{'_ref': 'networkcontainer/ZG5zLm5ldHdb2dj3fuM8C6dPMAVyJDE5Mi4xNjguMC4wLzI0LzE:192.168.0.0/24/ipam',
  'comment': 'Misc module',
  'network': '192.168.0.0/24',
  'network_view': 'ipam'},
 {'_ref': 'networkcontainer/ZG5zLm5ldHdb2dj3fuM8C6dPMAVyJDE5Mi4xNjguMC4wLzI0LzA:192.168.0.0/24/default',
  'comment': 'ipam3',
  'network': '192.168.0.0/24',
  'network_view': 'default'}]

The first one is not the correct one to use. The lookup plugin should provide a network_view parameter that defaults to default but can be overridden to a desired network view name. This should be used to extract the correct _ref.