Debian Codegraph @

ansible_collections/infoblox/nios_modules/plugins/modules/nios_fixed_address.py

raw symbols SCIP
1 #!/usr/bin/python
2 # Copyright (c) 2018-2019 Red Hat, Inc.
3 # Copyright (c) 2020 Infoblox, Inc.
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import absolute_import, division, print_function
7 __metaclass__ = type
8
9 DOCUMENTATION = '''
10 ---
11 module: nios_fixed_address
12 author: "Sumit Jaiswal (@sjaiswal)"
13 short_description: Configure Infoblox NIOS DHCP Fixed Address
14 version_added: "1.0.0"
15 description:
16 - A fixed address is a specific IP address that a DHCP server
17 always assigns when a lease request comes from a particular
18 MAC address of the client.
19 - A fix address reservation is a specific IP address that a DHCP
20 server reserves and never assigns to a client.
21 - Supports both IPV4 and IPV6 internet protocols.
22 requirements:
23 - infoblox-client
24 extends_documentation_fragment: infoblox.nios_modules.nios
25 notes:
26 - The "mac" field is mandatory for all CRUD operations relating to
27 IPv4 Fixed address.
28 - The "duid" field is mandatory for all CRUD operations relating to
29 IPv6 Fixed address.
30 - This module supports C(check_mode).
31 options:
32 name:
33 description:
34 - Specifies the hostname with which fixed DHCP ip-address is stored
35 for respective mac.
36 type: str
37 required: true
38 ipaddr:
39 description:
40 - IPV4/V6 address of the fixed address.
41 type: str
42 required: true
43 mac:
44 description:
45 - The MAC address of the IPv4 interface. For a fix address reservation
46 specify mac address as 00:00:00:00:00:00
47 type: str
48 duid:
49 description:
50 - The DUID address of the IPv6 interface.
51 type: str
52 network:
53 description:
54 - Specifies the network range in which ipaddr exists.
55 type: str
56 aliases:
57 - network
58 network_view:
59 description:
60 - Configures the name of the network view to associate with this
61 configured instance.
62 type: str
63 default: default
64 options:
65 description:
66 - Configures the set of DHCP options to be included as part of
67 the configured network instance. This argument accepts a list
68 of values (see suboptions). When configuring suboptions at
69 least one of C(name) or C(num) must be specified.
70 type: list
71 elements: dict
72 suboptions:
73 name:
74 description:
75 - The name of the DHCP option to configure
76 type: str
77 num:
78 description:
79 - The number of the DHCP option to configure
80 type: int
81 value:
82 description:
83 - The value of the DHCP option specified by C(name)
84 type: str
85 required: true
86 use_option:
87 description:
88 - Only applies to a subset of options (see NIOS API documentation)
89 type: bool
90 default: 'yes'
91 vendor_class:
92 description:
93 - The name of the space this DHCP option is associated to
94 type: str
95 default: DHCP
96 extattrs:
97 description:
98 - Allows for the configuration of Extensible Attributes on the
99 instance of the object. This argument accepts a set of key / value
100 pairs for configuration.
101 type: dict
102 comment:
103 description:
104 - Configures a text string comment to be associated with the instance
105 of this object. The provided text string will be configured on the
106 object instance.
107 type: str
108 state:
109 description:
110 - Configures the intended state of the instance of the object on
111 the NIOS server. When this value is set to C(present), the object
112 is configured on the device and when this value is set to C(absent)
113 the value is removed (if necessary) from the device.
114 type: str
115 default: present
116 choices:
117 - present
118 - absent
119 '''
120
121 EXAMPLES = '''
122 - name: Configure an ipv4 dhcp fixed address
123 infoblox.nios_modules.nios_fixed_address:
124 name: ipv4_fixed
125 ipaddr: 192.168.10.1
126 mac: 08:6d:41:e8:fd:e8
127 network: 192.168.10.0/24
128 network_view: default
129 comment: this is a test comment
130 state: present
131 provider:
132 host: "{{ inventory_hostname_short }}"
133 username: admin
134 password: admin
135 connection: local
136
137 - name: Configure an ipv4 dhcp fixed address reservation
138 infoblox.nios_modules.nios_fixed_address:
139 name: ipv4_fixed
140 ipaddr: 192.168.10.1
141 mac: 00:00:00:00:00:00
142 network: 192.168.10.0/24
143 network_view: default
144 comment: this is a test comment
145 state: present
146 provider:
147 host: "{{ inventory_hostname_short }}"
148 username: admin
149 password: admin
150 connection: local
151
152 - name: Configure an ipv6 dhcp fixed address
153 infoblox.nios_modules.nios_fixed_address:
154 name: ipv6_fixed
155 ipaddr: fe80::1/10
156 mac: 08:6d:41:e8:fd:e8
157 network: fe80::/64
158 network_view: default
159 comment: this is a test comment
160 state: present
161 provider:
162 host: "{{ inventory_hostname_short }}"
163 username: admin
164 password: admin
165 connection: local
166
167 - name: Set dhcp options for an ipv4 fixed address
168 infoblox.nios_modules.nios_fixed_address:
169 name: ipv4_fixed
170 ipaddr: 192.168.10.1
171 mac: 08:6d:41:e8:fd:e8
172 network: 192.168.10.0/24
173 network_view: default
174 comment: this is a test comment
175 options:
176 - name: domain-name
177 value: ansible.com
178 state: present
179 provider:
180 host: "{{ inventory_hostname_short }}"
181 username: admin
182 password: admin
183 connection: local
184
185 - name: Remove an ipv4 dhcp fixed address
186 infoblox.nios_modules.nios_fixed_address:
187 name: ipv4_fixed
188 ipaddr: 192.168.10.1
189 mac: 08:6d:41:e8:fd:e8
190 network: 192.168.10.0/24
191 network_view: default
192 state: absent
193 provider:
194 host: "{{ inventory_hostname_short }}"
195 username: admin
196 password: admin
197 connection: local
198 '''
199
200 RETURN = ''' # '''
201
202 from ansible.module_utils.basic import AnsibleModule
203 from ..module_utils.api import NIOS_IPV4_FIXED_ADDRESS, NIOS_IPV6_FIXED_ADDRESS
204 from ..module_utils.api import WapiModule
205 from ..module_utils.api import normalize_ib_spec
206 from ..module_utils.network import validate_ip_address, validate_ip_v6_address
207
208
209 def options(module):
210 ''' Transforms the module argument into a valid WAPI struct
211 This function will transform the options argument into a structure that
212 is a valid WAPI structure in the format of:
213 {
214 name: <value>,
215 num: <value>,
216 value: <value>,
217 use_option: <value>,
218 vendor_class: <value>
219 }
220 It will remove any options that are set to None since WAPI will error on
221 that condition. The use_option field only applies
222 to special options that are displayed separately from other options and
223 have a use flag. This function removes the use_option flag from all
224 other options. It will also verify that either `name` or `num` is
225 set in the structure but does not validate the values are equal.
226 The remainder of the value validation is performed by WAPI
227 '''
228 special_options = ['routers', 'router-templates', 'domain-name-servers',
229 'domain-name', 'broadcast-address', 'broadcast-address-offset',
230 'dhcp-lease-time', 'dhcp6.name-servers']
231 # options-router-templates, broadcast-address-offset, dhcp6.name-servers don't have any associated number
232 special_num = [3, 6, 15, 28, 51]
233 options = list()
234 for item in module.params['options']:
235 opt = dict([(k, v) for k, v in item.items() if v is not None])
236 if 'name' not in opt and 'num' not in opt:
237 module.fail_json(msg='one of `name` or `num` is required for option value')
238 if 'name' in opt and opt['name'] not in special_options:
239 del opt['use_option']
240 if 'num' in opt and opt['num'] not in special_num:
241 del opt['use_option']
242 options.append(opt)
243 return options
244
245
246 def validate_ip_addr_type(ip, arg_spec, module):
247 '''This function will check if the argument ip is type v4/v6 and return appropriate infoblox network type
248 '''
249 check_ip = ip.split('/')
250
251 if validate_ip_address(check_ip[0]) and 'ipaddr' in arg_spec:
252 arg_spec['ipv4addr'] = arg_spec.pop('ipaddr')
253 module.params['ipv4addr'] = module.params.pop('ipaddr')
254 del arg_spec['duid']
255 del module.params['duid']
256 if module.params["mac"] is None:
257 raise ValueError("the 'mac' address of the object must be specified")
258 module.params['mac'] = module.params['mac'].lower()
259 return NIOS_IPV4_FIXED_ADDRESS, arg_spec, module
260 elif validate_ip_v6_address(check_ip[0]) and 'ipaddr' in arg_spec:
261 arg_spec['ipv6addr'] = arg_spec.pop('ipaddr')
262 module.params['ipv6addr'] = module.params.pop('ipaddr')
263 del arg_spec['mac']
264 del module.params['mac']
265 if module.params["duid"] is None:
266 raise ValueError("the 'duid' of the object must be specified")
267 module.params['duid'] = module.params['duid'].lower()
268 return NIOS_IPV6_FIXED_ADDRESS, arg_spec, module
269
270
271 def main():
272 ''' Main entry point for module execution
273 '''
274 option_spec = dict(
275 # one of name or num is required; enforced by the function options()
276 name=dict(),
277 num=dict(type='int'),
278
279 value=dict(required=True),
280
281 use_option=dict(type='bool', default=True),
282 vendor_class=dict(default='DHCP')
283 )
284
285 ib_spec = dict(
286 name=dict(required=True),
287 ipaddr=dict(required=True, ib_req=True, type='str'),
288 mac=dict(ib_req=True, type='str'),
289 duid=dict(ib_req=True, type='str'),
290 network=dict(),
291 network_view=dict(default='default'),
292
293 options=dict(type='list', elements='dict', options=option_spec, transform=options),
294
295 extattrs=dict(type='dict'),
296 comment=dict(type='str')
297 )
298
299 argument_spec = dict(
300 provider=dict(required=True),
301 state=dict(default='present', choices=['present', 'absent'])
302 )
303
304 argument_spec.update(normalize_ib_spec(ib_spec))
305 argument_spec.update(WapiModule.provider_spec)
306
307 module = AnsibleModule(argument_spec=argument_spec,
308 supports_check_mode=True)
309
310 # to get the argument ipaddr
311 obj_filter = dict([(k, module.params[k]) for k, v in ib_spec.items() if v.get('ib_req')])
312 # to modify argument based on ipaddr type i.e. IPV4/IPV6
313 fixed_address_ip_type, ib_spec, module = validate_ip_addr_type(obj_filter['ipaddr'], ib_spec, module)
314
315 wapi = WapiModule(module)
316
317 result = wapi.run(fixed_address_ip_type, ib_spec)
318
319 module.exit_json(**result)
320
321
322 if __name__ == '__main__':
323 main()