| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
DOCUMENTATION = ''' |
| 10 |
--- |
| 11 |
module: nios_vlan |
| 12 |
author: "Christoph Spatt (@edeka-spatt)" |
| 13 |
short_description: Configure Infoblox NIOS VLANs |
| 14 |
version_added: "1.8.0" |
| 15 |
description: |
| 16 |
- Adds and/or removes instances of vlan objects from |
| 17 |
Infoblox NIOS servers. This module manages NIOS C(vlan) objects |
| 18 |
using the Infoblox WAPI interface over REST. |
| 19 |
- Updates instances of vlan object from Infoblox NIOS servers. |
| 20 |
requirements: |
| 21 |
- infoblox-client |
| 22 |
extends_documentation_fragment: infoblox.nios_modules.nios |
| 23 |
notes: |
| 24 |
- This module supports C(check_mode). |
| 25 |
options: |
| 26 |
name: |
| 27 |
description: |
| 28 |
- Specifies the vlan name to add or remove from |
| 29 |
the system. |
| 30 |
type: str |
| 31 |
required: true |
| 32 |
id: |
| 33 |
description: |
| 34 |
- Specifies the vlan id to add or remove from |
| 35 |
the system. |
| 36 |
type: int |
| 37 |
required: true |
| 38 |
parent: |
| 39 |
description: |
| 40 |
- Specifies the vlan parent to add or remove from |
| 41 |
the system. Can be either a C(vlanview) or C(vlanrange) |
| 42 |
name. Fetches the required _ref object automatically. |
| 43 |
If not specified defaults to vlan view C(default). |
| 44 |
type: str |
| 45 |
default: default |
| 46 |
comment: |
| 47 |
description: |
| 48 |
- Configures a text string comment to be associated with the instance |
| 49 |
of this object. The provided text string will be configured on the |
| 50 |
object instance. |
| 51 |
type: str |
| 52 |
default: '' |
| 53 |
contact: |
| 54 |
description: |
| 55 |
- Contact information for person/team managing or using VLAN. |
| 56 |
type: str |
| 57 |
default: '' |
| 58 |
department: |
| 59 |
description: |
| 60 |
- Department where VLAN is used. |
| 61 |
type: str |
| 62 |
default: '' |
| 63 |
description: |
| 64 |
description: |
| 65 |
- Description for the VLAN object, may be potentially used for |
| 66 |
longer VLAN names. |
| 67 |
type: str |
| 68 |
default: '' |
| 69 |
reserved: |
| 70 |
description: |
| 71 |
- When set VLAN can only be assigned to IPAM object manually. |
| 72 |
type: bool |
| 73 |
default: False |
| 74 |
extattrs: |
| 75 |
description: |
| 76 |
- Allows for the configuration of Extensible Attributes on the |
| 77 |
instance of the object. This argument accepts a set of key / value |
| 78 |
pairs for configuration. |
| 79 |
type: dict |
| 80 |
state: |
| 81 |
description: |
| 82 |
- Configures the intended state of the instance of the object on |
| 83 |
the NIOS server. When this value is set to C(present), the object |
| 84 |
is configured on the device and when this value is set to C(absent) |
| 85 |
the value is removed (if necessary) from the device. |
| 86 |
type: str |
| 87 |
default: present |
| 88 |
choices: |
| 89 |
- present |
| 90 |
- absent |
| 91 |
''' |
| 92 |
|
| 93 |
EXAMPLES = ''' |
| 94 |
- name: Create a new vlan |
| 95 |
infoblox.nios_modules.nios_vlan: |
| 96 |
name: ansible |
| 97 |
id: 10 |
| 98 |
parent: my_vlanview |
| 99 |
state: present |
| 100 |
provider: |
| 101 |
host: "{{ inventory_hostname_short }}" |
| 102 |
username: admin |
| 103 |
password: admin |
| 104 |
connection: local |
| 105 |
|
| 106 |
- name: Update the comment for a vlan |
| 107 |
infoblox.nios_modules.nios_vlan: |
| 108 |
name: ansible |
| 109 |
id: 10 |
| 110 |
parent: my_vlanview |
| 111 |
comment: this is an example comment |
| 112 |
state: present |
| 113 |
provider: |
| 114 |
host: "{{ inventory_hostname_short }}" |
| 115 |
username: admin |
| 116 |
password: admin |
| 117 |
connection: local |
| 118 |
|
| 119 |
- name: Remove the vlan |
| 120 |
infoblox.nios_modules.nios_vlan: |
| 121 |
name: ansible |
| 122 |
id: 10 |
| 123 |
parent: my_vlanview |
| 124 |
state: absent |
| 125 |
provider: |
| 126 |
host: "{{ inventory_hostname_short }}" |
| 127 |
username: admin |
| 128 |
password: admin |
| 129 |
connection: local |
| 130 |
|
| 131 |
- name: Update an existing vlan |
| 132 |
infoblox.nios_modules.nios_vlan: |
| 133 |
name: {new_name: ansible-new, old_name: ansible} |
| 134 |
id: 10 |
| 135 |
parent: my_vlanview |
| 136 |
state: present |
| 137 |
provider: |
| 138 |
host: "{{ inventory_hostname_short }}" |
| 139 |
username: admin |
| 140 |
password: admin |
| 141 |
connection: local |
| 142 |
|
| 143 |
- name: Create vlan with extensible attributes |
| 144 |
infoblox.nios_modules.nios_vlan: |
| 145 |
name: ansible |
| 146 |
id: 11 |
| 147 |
parent: my_vlanview |
| 148 |
comment: "this is an example comment" |
| 149 |
contact: "[email protected]" |
| 150 |
department: "IT" |
| 151 |
description: "test" |
| 152 |
reserved: True |
| 153 |
extattrs: |
| 154 |
Site: "HQ" |
| 155 |
state: present |
| 156 |
provider: |
| 157 |
host: "{{ inventory_hostname_short }}" |
| 158 |
username: admin |
| 159 |
password: admin |
| 160 |
connection: local |
| 161 |
|
| 162 |
- name: Create a new vlan with next available id |
| 163 |
infoblox.nios_modules.nios_vlan: |
| 164 |
name: ansible-vlan |
| 165 |
id: "{{ |
| 166 |
lookup('infoblox.nios_modules.nios_next_vlan_id', |
| 167 |
parent='my_vlanrange', |
| 168 |
exclude=[1,2], |
| 169 |
provider=nios_provider)[0] |
| 170 |
}}" |
| 171 |
parent: my_vlanrange |
| 172 |
state: present |
| 173 |
provider: |
| 174 |
host: "{{ inventory_hostname_short }}" |
| 175 |
username: admin |
| 176 |
password: admin |
| 177 |
connection: local |
| 178 |
''' |
| 179 |
|
| 180 |
RETURN = ''' # ''' |
| 181 |
|
| 182 |
from ansible.module_utils.basic import AnsibleModule |
| 183 |
from ..module_utils.api import WapiModule |
| 184 |
from ..module_utils.api import NIOS_VLAN |
| 185 |
from ..module_utils.api import normalize_ib_spec |
| 186 |
|
| 187 |
|
| 188 |
def main(): |
| 189 |
''' Main entry point for module execution |
| 190 |
''' |
| 191 |
|
| 192 |
def parent_transform(module): |
| 193 |
parent_ref = str() |
| 194 |
if module.params['parent']: |
| 195 |
parent_obj_vlanview = wapi.get_object('vlanview', {'name': module.params['parent']}) |
| 196 |
parent_obj_vlanrange = wapi.get_object('vlanrange', {'name': module.params['parent']}) |
| 197 |
if parent_obj_vlanrange: |
| 198 |
parent_ref = parent_obj_vlanrange[0]['_ref'] |
| 199 |
elif parent_obj_vlanview: |
| 200 |
parent_ref = parent_obj_vlanview[0]['_ref'] |
| 201 |
else: |
| 202 |
module.fail_json(msg='VLAN View/Range \'%s\' cannot be found.' % module.params['parent']) |
| 203 |
return parent_ref |
| 204 |
|
| 205 |
ib_spec = dict( |
| 206 |
name=dict(required=True, ib_req=True), |
| 207 |
id=dict(type='int', required=True, ib_req=True), |
| 208 |
parent=dict(default='default', transform=parent_transform), |
| 209 |
comment=dict(default=''), |
| 210 |
contact=dict(default=''), |
| 211 |
department=dict(default=''), |
| 212 |
description=dict(default=''), |
| 213 |
reserved=dict(type='bool', default=False), |
| 214 |
extattrs=dict(type='dict'), |
| 215 |
) |
| 216 |
|
| 217 |
argument_spec = dict( |
| 218 |
provider=dict(required=True), |
| 219 |
state=dict(default='present', choices=['present', 'absent']) |
| 220 |
) |
| 221 |
|
| 222 |
argument_spec.update(normalize_ib_spec(ib_spec)) |
| 223 |
argument_spec.update(WapiModule.provider_spec) |
| 224 |
|
| 225 |
module = AnsibleModule(argument_spec=argument_spec, |
| 226 |
supports_check_mode=True) |
| 227 |
|
| 228 |
wapi = WapiModule(module) |
| 229 |
result = wapi.run(NIOS_VLAN, ib_spec) |
| 230 |
|
| 231 |
module.exit_json(**result) |
| 232 |
|
| 233 |
|
| 234 |
if __name__ == '__main__': |
| 235 |
main() |