| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
DOCUMENTATION = ''' |
| 10 |
--- |
| 11 |
module: nios_network |
| 12 |
author: |
| 13 |
- "Peter Sprygada (@privateip)" |
| 14 |
- "Matthew Dennett (@matthewdennett)" |
| 15 |
short_description: Configure Infoblox NIOS network object |
| 16 |
version_added: "1.0.0" |
| 17 |
description: |
| 18 |
- Adds and/or removes instances of network objects from |
| 19 |
Infoblox NIOS servers. This module manages NIOS C(network) objects |
| 20 |
using the Infoblox WAPI interface over REST. |
| 21 |
- Supports both IPV4 and IPV6 internet protocols. |
| 22 |
requirements: |
| 23 |
- infoblox-client |
| 24 |
extends_documentation_fragment: infoblox.nios_modules.nios |
| 25 |
notes: |
| 26 |
- This module supports C(check_mode). |
| 27 |
options: |
| 28 |
network: |
| 29 |
description: |
| 30 |
- Specifies the network to add or remove from the system. The value |
| 31 |
should use CIDR notation. |
| 32 |
type: str |
| 33 |
required: true |
| 34 |
aliases: |
| 35 |
- name |
| 36 |
- cidr |
| 37 |
network_view: |
| 38 |
description: |
| 39 |
- Configures the name of the network view to associate with this |
| 40 |
configured instance. |
| 41 |
type: str |
| 42 |
default: default |
| 43 |
options: |
| 44 |
description: |
| 45 |
- Configures the set of DHCP options to be included as part of |
| 46 |
the configured network instance. This argument accepts a list |
| 47 |
of values (see suboptions). When configuring suboptions at |
| 48 |
least one of C(name) or C(num) must be specified. |
| 49 |
type: list |
| 50 |
default: [] |
| 51 |
elements: dict |
| 52 |
suboptions: |
| 53 |
name: |
| 54 |
description: |
| 55 |
- The name of the DHCP option to configure. The standard options are |
| 56 |
C(router), C(router-templates), C(domain-name-servers), C(domain-name), |
| 57 |
C(broadcast-address), C(broadcast-address-offset), C(dhcp-lease-time), |
| 58 |
and C(dhcp6.name-servers). |
| 59 |
type: str |
| 60 |
num: |
| 61 |
description: |
| 62 |
- The number of the DHCP option to configure |
| 63 |
type: int |
| 64 |
value: |
| 65 |
description: |
| 66 |
- The value of the DHCP option specified by C(name) |
| 67 |
type: str |
| 68 |
required: true |
| 69 |
use_option: |
| 70 |
description: |
| 71 |
- Only applies to a subset of options (see NIOS API documentation) |
| 72 |
type: bool |
| 73 |
default: 'yes' |
| 74 |
vendor_class: |
| 75 |
description: |
| 76 |
- The name of the space this DHCP option is associated to |
| 77 |
type: str |
| 78 |
default: DHCP |
| 79 |
template: |
| 80 |
description: |
| 81 |
- If set on creation, the network is created according to the values |
| 82 |
specified in the selected template. |
| 83 |
type: str |
| 84 |
vlans: |
| 85 |
description: |
| 86 |
- Configures the set of vlans to be included as part of |
| 87 |
the configured network instance. This argument accepts a list |
| 88 |
of values (see suboptions). When configuring suboptions at |
| 89 |
least one of C(name) or C(id) must be specified. |
| 90 |
type: list |
| 91 |
default: [] |
| 92 |
elements: dict |
| 93 |
suboptions: |
| 94 |
name: |
| 95 |
description: |
| 96 |
- The name of the vlan. |
| 97 |
type: str |
| 98 |
id: |
| 99 |
description: |
| 100 |
- The id of the vlan. |
| 101 |
type: int |
| 102 |
parent: |
| 103 |
description: |
| 104 |
- The name of the parent vlanview or vlanrange. |
| 105 |
type: str |
| 106 |
default: default |
| 107 |
extattrs: |
| 108 |
description: |
| 109 |
- Allows for the configuration of Extensible Attributes on the |
| 110 |
instance of the object. This argument accepts a set of key / value |
| 111 |
pairs for configuration. |
| 112 |
type: dict |
| 113 |
comment: |
| 114 |
description: |
| 115 |
- Configures a text string comment to be associated with the instance |
| 116 |
of this object. The provided text string will be configured on the |
| 117 |
object instance. |
| 118 |
type: str |
| 119 |
container: |
| 120 |
description: |
| 121 |
- If set to true it'll create the network container to be added or removed |
| 122 |
from the system. |
| 123 |
type: bool |
| 124 |
members: |
| 125 |
description: |
| 126 |
- Configures the Nios Menber assignment for the configured network instance. |
| 127 |
This argument accepts a list of member names (see suboptions). When omitted |
| 128 |
a default value of an empty list is used. If the field 'container' is set to |
| 129 |
true this field is ignored. |
| 130 |
type: list |
| 131 |
default: [] |
| 132 |
elements: dict |
| 133 |
suboptions: |
| 134 |
name: |
| 135 |
description: |
| 136 |
- The name of the Nios member to be assigned to this network. |
| 137 |
type: str |
| 138 |
use_logic_filter_rules: |
| 139 |
description: |
| 140 |
- If set to true it'll override the logic filter list applied at an upper level. |
| 141 |
type: bool |
| 142 |
default: false |
| 143 |
logic_filter_rules: |
| 144 |
description: |
| 145 |
- Configures the logic filter rules to be applied to the network object. |
| 146 |
This argument accepts a list of logic filter rules (see suboptions). When omitted |
| 147 |
a default value of an empty list is used. |
| 148 |
type: list |
| 149 |
default: [] |
| 150 |
elements: dict |
| 151 |
suboptions: |
| 152 |
filter: |
| 153 |
description: |
| 154 |
- The name of the logic filter to apply to the network object. |
| 155 |
type: str |
| 156 |
type: |
| 157 |
description: |
| 158 |
- The type of the logic filter to apply to the network object. |
| 159 |
type: str |
| 160 |
state: |
| 161 |
description: |
| 162 |
- Configures the intended state of the instance of the object on |
| 163 |
the NIOS server. When this value is set to C(present), the object |
| 164 |
is configured on the device and when this value is set to C(absent) |
| 165 |
the value is removed (if necessary) from the device. |
| 166 |
type: str |
| 167 |
default: present |
| 168 |
choices: |
| 169 |
- present |
| 170 |
- absent |
| 171 |
''' |
| 172 |
|
| 173 |
EXAMPLES = ''' |
| 174 |
- name: Configure a network ipv4 |
| 175 |
infoblox.nios_modules.nios_network: |
| 176 |
network: 192.168.10.0/24 |
| 177 |
comment: this is a test comment |
| 178 |
state: present |
| 179 |
provider: |
| 180 |
host: "{{ inventory_hostname_short }}" |
| 181 |
username: admin |
| 182 |
password: admin |
| 183 |
connection: local |
| 184 |
|
| 185 |
- name: Configure a network ipv6 |
| 186 |
infoblox.nios_modules.nios_network: |
| 187 |
network: fe80::/64 |
| 188 |
comment: this is a test comment |
| 189 |
state: present |
| 190 |
provider: |
| 191 |
host: "{{ inventory_hostname_short }}" |
| 192 |
username: admin |
| 193 |
password: admin |
| 194 |
connection: local |
| 195 |
|
| 196 |
- name: Create network with member assignment for a network ipv4 |
| 197 |
infoblox.nios_modules.nios_network: |
| 198 |
network: 192.168.10.0/24 |
| 199 |
comment: This is a test comment |
| 200 |
members: |
| 201 |
- name: member1.infoblox |
| 202 |
- name: member2.infoblox |
| 203 |
state: present |
| 204 |
provider: |
| 205 |
host: "{{ inventory_hostname_short }}" |
| 206 |
username: admin |
| 207 |
password: admin |
| 208 |
connection: local |
| 209 |
|
| 210 |
- name: Remove member assignment form ipv4 network |
| 211 |
infoblox.nios_modules.nios_network: |
| 212 |
network: 192.168.10.0/24 |
| 213 |
comment: This is a test comment |
| 214 |
state: present |
| 215 |
provider: |
| 216 |
host: "{{ inventory_hostname_short }}" |
| 217 |
username: admin |
| 218 |
password: admin |
| 219 |
connection: local |
| 220 |
|
| 221 |
- name: Set dhcp options for a network ipv4 |
| 222 |
infoblox.nios_modules.nios_network: |
| 223 |
network: 192.168.10.0/24 |
| 224 |
comment: this is a test comment |
| 225 |
options: |
| 226 |
- name: domain-name |
| 227 |
value: ansible.com |
| 228 |
state: present |
| 229 |
provider: |
| 230 |
host: "{{ inventory_hostname_short }}" |
| 231 |
username: admin |
| 232 |
password: admin |
| 233 |
connection: local |
| 234 |
|
| 235 |
- name: Set filters for a network ipv4 |
| 236 |
infoblox.nios_modules.nios_network: |
| 237 |
network: 192.168.10.0/24 |
| 238 |
comment: this is a test comment |
| 239 |
use_logic_filter_rules: true |
| 240 |
logic_filter_rules: |
| 241 |
- filter: PXE-UEFI |
| 242 |
type: Option |
| 243 |
state: present |
| 244 |
provider: |
| 245 |
host: "{{ inventory_hostname_short }}" |
| 246 |
username: admin |
| 247 |
password: admin |
| 248 |
connection: local |
| 249 |
|
| 250 |
- name: Remove a network ipv4 |
| 251 |
infoblox.nios_modules.nios_network: |
| 252 |
network: 192.168.10.0/24 |
| 253 |
state: absent |
| 254 |
provider: |
| 255 |
host: "{{ inventory_hostname_short }}" |
| 256 |
username: admin |
| 257 |
password: admin |
| 258 |
connection: local |
| 259 |
|
| 260 |
- name: Configure an ipv4 network container |
| 261 |
infoblox.nios_modules.nios_network: |
| 262 |
network: 192.168.10.0/24 |
| 263 |
container: true |
| 264 |
comment: test network container |
| 265 |
state: present |
| 266 |
provider: |
| 267 |
host: "{{ inventory_hostname_short }}" |
| 268 |
username: admin |
| 269 |
password: admin |
| 270 |
connection: local |
| 271 |
|
| 272 |
- name: Configure an ipv6 network container |
| 273 |
infoblox.nios_modules.nios_network: |
| 274 |
network: fe80::/64 |
| 275 |
container: true |
| 276 |
comment: test network container |
| 277 |
state: present |
| 278 |
provider: |
| 279 |
host: "{{ inventory_hostname_short }}" |
| 280 |
username: admin |
| 281 |
password: admin |
| 282 |
connection: local |
| 283 |
|
| 284 |
- name: Remove an ipv4 network container |
| 285 |
infoblox.nios_modules.nios_network: |
| 286 |
networkr: 192.168.10.0/24 |
| 287 |
container: true |
| 288 |
comment: test network container |
| 289 |
state: absent |
| 290 |
provider: |
| 291 |
host: "{{ inventory_hostname_short }}" |
| 292 |
username: admin |
| 293 |
password: admin |
| 294 |
connection: local |
| 295 |
|
| 296 |
- name: Configure a network ipv4 and assign vlans |
| 297 |
infoblox.nios_modules.nios_network: |
| 298 |
network: 192.168.10.0/24 |
| 299 |
comment: this is a test comment |
| 300 |
vlans: |
| 301 |
- name: ansiblevlan |
| 302 |
parent: ansiblevlanview |
| 303 |
id: 10 |
| 304 |
state: present |
| 305 |
provider: |
| 306 |
host: "{{ inventory_hostname_short }}" |
| 307 |
username: admin |
| 308 |
password: admin |
| 309 |
connection: local |
| 310 |
''' |
| 311 |
|
| 312 |
RETURN = ''' # ''' |
| 313 |
|
| 314 |
from ansible.module_utils.basic import AnsibleModule |
| 315 |
from ..module_utils.api import WapiModule |
| 316 |
from ..module_utils.api import NIOS_IPV4_NETWORK, NIOS_IPV6_NETWORK |
| 317 |
from ..module_utils.api import NIOS_IPV4_NETWORK_CONTAINER, NIOS_IPV6_NETWORK_CONTAINER |
| 318 |
from ..module_utils.api import normalize_ib_spec |
| 319 |
from ..module_utils.network import validate_ip_address, validate_ip_v6_address |
| 320 |
|
| 321 |
|
| 322 |
def options(module): |
| 323 |
''' Transforms the module argument into a valid WAPI struct |
| 324 |
This function will transform the options argument into a structure that |
| 325 |
is a valid WAPI structure in the format of: |
| 326 |
{ |
| 327 |
name: <value>, |
| 328 |
num: <value>, |
| 329 |
value: <value>, |
| 330 |
use_option: <value>, |
| 331 |
vendor_class: <value> |
| 332 |
} |
| 333 |
It will remove any options that are set to None since WAPI will error on |
| 334 |
that condition. It will also verify that either `name` or `num` is |
| 335 |
set in the structure but does not validate the values are equal. |
| 336 |
The remainder of the value validation is performed by WAPI |
| 337 |
''' |
| 338 |
options = list() |
| 339 |
for item in module.params['options']: |
| 340 |
opt = dict([(k, v) for k, v in item.items() if v is not None]) |
| 341 |
if 'name' not in opt and 'num' not in opt: |
| 342 |
module.fail_json(msg='one of `name` or `num` is required for option value') |
| 343 |
options.append(opt) |
| 344 |
return options |
| 345 |
|
| 346 |
|
| 347 |
def check_ip_addr_type(obj_filter, ib_spec): |
| 348 |
'''This function will check if the argument ip is type v4/v6 and return appropriate infoblox |
| 349 |
network/networkcontainer type |
| 350 |
''' |
| 351 |
|
| 352 |
ip = obj_filter['network'] |
| 353 |
if 'container' in obj_filter and obj_filter['container']: |
| 354 |
check_ip = ip.split('/') |
| 355 |
del ib_spec['container'] |
| 356 |
if validate_ip_address(check_ip[0]): |
| 357 |
return NIOS_IPV4_NETWORK_CONTAINER, ib_spec |
| 358 |
elif validate_ip_v6_address(check_ip[0]): |
| 359 |
return NIOS_IPV6_NETWORK_CONTAINER, ib_spec |
| 360 |
else: |
| 361 |
check_ip = ip.split('/') |
| 362 |
del ib_spec['container'] |
| 363 |
if validate_ip_address(check_ip[0]): |
| 364 |
return NIOS_IPV4_NETWORK, ib_spec |
| 365 |
elif validate_ip_v6_address(check_ip[0]): |
| 366 |
return NIOS_IPV6_NETWORK, ib_spec |
| 367 |
|
| 368 |
|
| 369 |
def check_vendor_specific_dhcp_option(module, ib_spec): |
| 370 |
'''This function will check if the argument dhcp option belongs to vendor-specific and if yes then will remove |
| 371 |
use_options flag which is not supported with vendor-specific dhcp options. |
| 372 |
''' |
| 373 |
for key, value in ib_spec.items(): |
| 374 |
if isinstance(module.params[key], list): |
| 375 |
for temp_dict in module.params[key]: |
| 376 |
if 'num' in temp_dict: |
| 377 |
if temp_dict['num'] in (43, 124, 125, 67, 60): |
| 378 |
del temp_dict['use_option'] |
| 379 |
return ib_spec |
| 380 |
|
| 381 |
|
| 382 |
def main(): |
| 383 |
''' Main entry point for module execution |
| 384 |
''' |
| 385 |
def vlans(module): |
| 386 |
vlans_list = list() |
| 387 |
if module.params['vlans']: |
| 388 |
for vlan in module.params['vlans']: |
| 389 |
|
| 390 |
vlan_filtered = dict((k, v) for k, v in vlan.items() if v is not None) |
| 391 |
if 'name' not in vlan_filtered and 'id' not in vlan_filtered: |
| 392 |
module.fail_json(msg='one of `name` or `id` is required for vlans value') |
| 393 |
|
| 394 |
if 'parent' in vlan_filtered: |
| 395 |
obj_vlanview = wapi.get_object('vlanview', {'name': vlan_filtered['parent']}) |
| 396 |
obj_vlanrange = wapi.get_object('vlanrange', {'name': vlan_filtered['parent']}) |
| 397 |
if obj_vlanrange: |
| 398 |
vlan_filtered['parent'] = obj_vlanrange[0]['_ref'] |
| 399 |
elif obj_vlanview: |
| 400 |
vlan_filtered['parent'] = obj_vlanview[0]['_ref'] |
| 401 |
else: |
| 402 |
module.fail_json(msg='VLAN View/Range \'%s\' cannot be found.' % vlan_filtered['parent']) |
| 403 |
|
| 404 |
obj_vlan = wapi.get_object('vlan', vlan_filtered) |
| 405 |
|
| 406 |
if obj_vlan: |
| 407 |
vlans_list.append({'vlan': obj_vlan[0]['_ref']}) |
| 408 |
else: |
| 409 |
module.fail_json(msg='VLAN `%s` cannot be found.' % vlan) |
| 410 |
|
| 411 |
return vlans_list |
| 412 |
|
| 413 |
option_spec = dict( |
| 414 |
|
| 415 |
name=dict(), |
| 416 |
num=dict(type='int'), |
| 417 |
|
| 418 |
value=dict(required=True), |
| 419 |
|
| 420 |
use_option=dict(type='bool', default=True), |
| 421 |
vendor_class=dict(default='DHCP') |
| 422 |
) |
| 423 |
|
| 424 |
vlans_spec = dict( |
| 425 |
id=dict(type='int'), |
| 426 |
name=dict(), |
| 427 |
parent=dict(default='default') |
| 428 |
) |
| 429 |
|
| 430 |
ib_spec = dict( |
| 431 |
network=dict(required=True, aliases=['name', 'cidr'], ib_req=True), |
| 432 |
network_view=dict(default='default', ib_req=True), |
| 433 |
options=dict(type='list', elements='dict', options=option_spec, transform=options, default=[]), |
| 434 |
vlans=dict(type='list', elements='dict', options=vlans_spec, transform=vlans, default=[]), |
| 435 |
template=dict(type='str'), |
| 436 |
extattrs=dict(type='dict'), |
| 437 |
comment=dict(), |
| 438 |
container=dict(type='bool', ib_req=True), |
| 439 |
members=dict(type='list', elements='dict', default=[]), |
| 440 |
use_logic_filter_rules=dict(type='bool', default=False), |
| 441 |
logic_filter_rules=dict(type='list', elements='dict', default=[]) |
| 442 |
) |
| 443 |
|
| 444 |
argument_spec = dict( |
| 445 |
provider=dict(required=True), |
| 446 |
state=dict(default='present', choices=['present', 'absent']) |
| 447 |
) |
| 448 |
|
| 449 |
argument_spec.update(normalize_ib_spec(ib_spec)) |
| 450 |
argument_spec.update(WapiModule.provider_spec) |
| 451 |
|
| 452 |
module = AnsibleModule(argument_spec=argument_spec, |
| 453 |
supports_check_mode=True) |
| 454 |
|
| 455 |
|
| 456 |
obj_filter = dict([(k, module.params[k]) for k, v in ib_spec.items() if v.get('ib_req')]) |
| 457 |
network_type, ib_spec = check_ip_addr_type(obj_filter, ib_spec) |
| 458 |
|
| 459 |
wapi = WapiModule(module) |
| 460 |
|
| 461 |
ib_spec = check_vendor_specific_dhcp_option(module, ib_spec) |
| 462 |
|
| 463 |
result = wapi.run(network_type, ib_spec) |
| 464 |
|
| 465 |
module.exit_json(**result) |
| 466 |
|
| 467 |
|
| 468 |
if __name__ == '__main__': |
| 469 |
main() |