Debian Codegraph @

ansible_collections/infoblox/nios_modules/plugins/lookup/nios_next_vlan_id.py

raw symbols SCIP
1 # Copyright (c) 2018-2019 Red Hat, Inc.
2 # Copyright (c) 2020 Infoblox, Inc.
3 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
4
5
6 from __future__ import (absolute_import, division, print_function)
7
8 __metaclass__ = type
9
10 DOCUMENTATION = '''
11 ---
12 name: nios_next_vlan_id
13 short_description: Return the next available VLAN ID for a VLAN view/range
14 version_added: "1.8.0"
15 description:
16 - Uses the Infoblox WAPI API to return the next available VLAN IDs for a given VLAN view/range
17 requirements:
18 - infoblox_client
19
20 options:
21 parent:
22 description: The VLAN view/range to retrieve the VLAN IDs from.
23 required: false
24 default: default
25 type: str
26 num:
27 description: The number of VLAN IDs to return.
28 required: false
29 default: 1
30 type: int
31 exclude:
32 description: List of VLAN IDs that need to be excluded from returned VLAN IDs.
33 required: false
34 type: list
35 elements: int
36 '''
37
38 EXAMPLES = """
39 - name: return the next available VLAN ID from a VLAN view
40 ansible.builtin.set_fact:
41 networkaddr: "{{ lookup('infoblox.nios_modules.nios_next_vlan_id', parent='vlanview',
42 provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
43
44 - name: return the next two available VLAN IDs from a VLAN range
45 ansible.builtin.set_fact:
46 networkaddr: "{{ lookup('infoblox.nios_modules.nios_next_vlan_id', parent='vlanrange', num=2,
47 provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
48
49 - name: return the next available VLAN ID, excluding IDs 1-3
50 ansible.builtin.set_fact:
51 networkaddr: "{{ lookup('infoblox.nios_modules.nios_next_vlan_id', parent='vlanrange', exclude=[1,2,3],
52 provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
53 """
54
55 RETURN = """
56 _list:
57 description:
58 - The list of next vlan ids available
59 returned: always
60 type: list
61 """
62
63 from ansible.plugins.lookup import LookupBase
64 from ansible.module_utils._text import to_text
65 from ansible.errors import AnsibleError
66 from ..module_utils.api import WapiLookup
67
68
69 class LookupModule(LookupBase):
70
71 def run(self, terms, variables=None, **kwargs):
72 parent_ref = str()
73 provider = kwargs.pop('provider', {})
74 wapi = WapiLookup(provider)
75 num = kwargs.get('num', 1)
76 exclude_vlan_id = kwargs.get('exclude', [])
77 parent = kwargs.get('parent', 'default')
78
79 try:
80 parent_obj_vlanview = wapi.get_object('vlanview', {'name': parent})
81 parent_obj_vlanrange = wapi.get_object('vlanrange', {'name': parent})
82 if parent_obj_vlanrange:
83 parent_ref = parent_obj_vlanrange[0]['_ref']
84 elif parent_obj_vlanview:
85 parent_ref = parent_obj_vlanview[0]['_ref']
86 else:
87 raise AnsibleError(message='VLAN View/Range \'%s\' cannot be found.' % parent)
88
89 avail_ids = wapi.call_func('next_available_vlan_id', parent_ref, {'num': num, 'exclude': exclude_vlan_id})
90 return [avail_ids['vlan_ids']]
91
92 except Exception as exc:
93 raise AnsibleError(to_text(exc))