Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] lookup plugin for next_available_vlan_id #282

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions plugins/lookup/nios_next_vlan_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Copyright (c) 2018-2019 Red Hat, Inc.
# Copyright (c) 2020 Infoblox, Inc.
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)


from __future__ import (absolute_import, division, print_function)

__metaclass__ = type

DOCUMENTATION = '''
---
name: nios_next_vlan_id
short_description: Return the next available VLAN ID for a VLAN view/range
version_added: "1.8.0"
description:
- Uses the Infoblox WAPI API to return the next available VLAN IDs for a given VLAN view/range
requirements:
- infoblox_client

options:
parent:
description: The VLAN view/range to retrieve the VLAN IDs from.
required: false
default: default
type: str
num:
description: The number of VLAN IDs to return.
required: false
default: 1
type: int
exclude:
description: List of VLAN IDs that need to be excluded from returned VLAN IDs.
required: false
type: list
elements: int
'''

EXAMPLES = """
- name: return the next available VLAN ID from a VLAN view
ansible.builtin.set_fact:
networkaddr: "{{ lookup('infoblox.nios_modules.nios_next_vlan_id', parent='vlanview',
provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"

- name: return the next two available VLAN IDs from a VLAN range
ansible.builtin.set_fact:
networkaddr: "{{ lookup('infoblox.nios_modules.nios_next_vlan_id', parent='vlanrange', num=2,
provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"

- name: return the next available VLAN ID, excluding IDs 1-3
ansible.builtin.set_fact:
networkaddr: "{{ lookup('infoblox.nios_modules.nios_next_vlan_id', parent='vlanrange', exclude=[1,2,3],
provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
"""

RETURN = """
_list:
description:
- The list of next vlan ids available
returned: always
type: list
"""

from ansible.plugins.lookup import LookupBase
from ansible.module_utils._text import to_text
from ansible.errors import AnsibleError
from ..module_utils.api import WapiLookup


class LookupModule(LookupBase):

def run(self, terms, variables=None, **kwargs):
parent_ref = str()
provider = kwargs.pop('provider', {})
wapi = WapiLookup(provider)
num = kwargs.get('num', 1)
exclude_ip = kwargs.get('exclude', [])
parent = kwargs.get('parent', 'default')

try:
parent_obj_vlanview = wapi.get_object('vlanview', {'name': parent})
parent_obj_vlanrange = wapi.get_object('vlanrange', {'name': parent})
if parent_obj_vlanrange:
parent_ref = parent_obj_vlanrange[0]['_ref']
elif parent_obj_vlanview:
parent_ref = parent_obj_vlanview[0]['_ref']
else:
raise AnsibleError(msg='VLAN View/Range \'%s\' cannot be found.' % parent)

avail_ids = wapi.call_func('next_available_vlan_id', parent_ref, {'num': num, 'exclude': exclude_ip})
return [avail_ids['vlan_ids']]

except Exception as exc:
raise AnsibleError(to_text(exc))
17 changes: 17 additions & 0 deletions plugins/modules/nios_vlan.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,23 @@
username: admin
password: admin
connection: local

- name: Create a new vlan with next available id
infoblox.nios_modules.nios_vlan:
name: ansible-vlan
id: "{{
lookup('infoblox.nios_modules.nios_next_vlan_id',
parent='my_vlanrange',
exclude=[1,2],
provider=nios_provider)[0]
}}"
parent: my_vlanrange
state: present
provider:
host: "{{ inventory_hostname_short }}"
username: admin
password: admin
connection: local
'''

RETURN = ''' # '''
Expand Down
Loading