| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
DOCUMENTATION = ''' |
| 10 |
--- |
| 11 |
module: nios_dtc_lbdn |
| 12 |
author: "Mauricio Teixeira (@badnetmask)" |
| 13 |
version_added: "1.1.0" |
| 14 |
short_description: Configure Infoblox NIOS DTC LBDN |
| 15 |
description: |
| 16 |
- Adds and/or removes instances of DTC Load Balanced Domain Name (LBDN) |
| 17 |
objects from Infoblox NIOS servers. This module manages NIOS |
| 18 |
C(dtc:lbdn) objects using the Infoblox WAPI interface over REST. |
| 19 |
requirements: |
| 20 |
- infoblox-client |
| 21 |
extends_documentation_fragment: infoblox.nios_modules.nios |
| 22 |
notes: |
| 23 |
- This module supports C(check_mode). |
| 24 |
options: |
| 25 |
name: |
| 26 |
description: |
| 27 |
- Specifies the display name of the DTC LBDN, not DNS related. |
| 28 |
required: true |
| 29 |
type: str |
| 30 |
lb_method: |
| 31 |
description: |
| 32 |
- Configures the load balancing method. Used to select pool. |
| 33 |
required: true |
| 34 |
type: str |
| 35 |
choices: |
| 36 |
- GLOBAL_AVAILABILITY |
| 37 |
- RATIO |
| 38 |
- ROUND_ROBIN |
| 39 |
- TOPOLOGY |
| 40 |
topology: |
| 41 |
description: |
| 42 |
- Configures the topology rules for the C(TOPOLOGY) load balancing method. |
| 43 |
- Required only when I(lb_method) is set to C(TOPOLOGY). |
| 44 |
required: false |
| 45 |
type: str |
| 46 |
auth_zones: |
| 47 |
description: |
| 48 |
- List of linked authoritative zones. |
| 49 |
- When using I(auth_zones), you must specify at least one I(patterns). |
| 50 |
- Each zone can be specified as a string (FQDN) or as a dict with 'fqdn' and optional 'view' keys. |
| 51 |
- When provided as a string, the first matching zone in any view will be used. |
| 52 |
- When zones with the same name exist in different views, use the dict format to specify which view to use. |
| 53 |
- "Example ['example.com'] or [{'fqdn': 'example.com', 'view': 'custom'}]" |
| 54 |
required: false |
| 55 |
type: list |
| 56 |
elements: raw |
| 57 |
patterns: |
| 58 |
description: |
| 59 |
- Specify LBDN wildcards for pattern match. |
| 60 |
required: false |
| 61 |
type: list |
| 62 |
elements: str |
| 63 |
types: |
| 64 |
description: |
| 65 |
- Specifies the list of resource record types supported by LBDN. |
| 66 |
- This option will work properly only if you set the C(wapi_version) |
| 67 |
variable on your C(provider) variable to a |
| 68 |
number higher than "2.6". |
| 69 |
required: false |
| 70 |
type: list |
| 71 |
elements: str |
| 72 |
choices: |
| 73 |
- A |
| 74 |
- AAAA |
| 75 |
- CNAME |
| 76 |
- NAPTR |
| 77 |
- SRV |
| 78 |
pools: |
| 79 |
description: |
| 80 |
- The pools used for load balancing. |
| 81 |
required: false |
| 82 |
type: list |
| 83 |
elements: dict |
| 84 |
suboptions: |
| 85 |
pool: |
| 86 |
description: |
| 87 |
- Provide the name of the pool to link with |
| 88 |
required: true |
| 89 |
type: str |
| 90 |
ratio: |
| 91 |
description: |
| 92 |
- Provide the weight of the pool |
| 93 |
default: 1 |
| 94 |
required: false |
| 95 |
type: int |
| 96 |
ttl: |
| 97 |
description: |
| 98 |
- The Time To Live (TTL) value for the DTC LBDN. A 32-bit unsigned |
| 99 |
integer that represents the duration, in seconds, for which the |
| 100 |
record is valid (cached). Zero indicates that the record should |
| 101 |
not be cached. |
| 102 |
type: int |
| 103 |
extattrs: |
| 104 |
description: |
| 105 |
- Allows for the configuration of Extensible Attributes on the |
| 106 |
instance of the object. This argument accepts a set of key / value |
| 107 |
pairs for configuration. |
| 108 |
type: dict |
| 109 |
comment: |
| 110 |
description: |
| 111 |
- Configures a text string comment to be associated with the instance |
| 112 |
of this object. The provided text string will be configured on the |
| 113 |
object instance. |
| 114 |
type: str |
| 115 |
state: |
| 116 |
description: |
| 117 |
- Configures the intended state of the instance of the object on |
| 118 |
the NIOS server. When this value is set to C(present), the object |
| 119 |
is configured on the device and when this value is set to C(absent) |
| 120 |
the value is removed (if necessary) from the device. |
| 121 |
default: present |
| 122 |
choices: |
| 123 |
- present |
| 124 |
- absent |
| 125 |
type: str |
| 126 |
''' |
| 127 |
|
| 128 |
EXAMPLES = ''' |
| 129 |
- name: Configure a DTC LBDN |
| 130 |
infoblox.nios_modules.nios_dtc_lbdn: |
| 131 |
name: web.ansible.com |
| 132 |
lb_method: ROUND_ROBIN |
| 133 |
pools: |
| 134 |
- pool: web_pool |
| 135 |
state: present |
| 136 |
provider: |
| 137 |
host: "{{ inventory_hostname_short }}" |
| 138 |
username: admin |
| 139 |
password: admin |
| 140 |
connection: local |
| 141 |
|
| 142 |
- name: Configure a DTC LBDN with zones from different views |
| 143 |
infoblox.nios_modules.nios_dtc_lbdn: |
| 144 |
name: web.ansible.com |
| 145 |
lb_method: ROUND_ROBIN |
| 146 |
auth_zones: |
| 147 |
- example.com # First matching zone in any view |
| 148 |
- {"fqdn": "test.com", "view": "default"} # Explicitly from default view |
| 149 |
- {"fqdn": "demo.com", "view": "custom"} # From custom view |
| 150 |
patterns: |
| 151 |
- "*.example.com" |
| 152 |
- "*.test.com" |
| 153 |
- "*.demo.com" |
| 154 |
state: present |
| 155 |
provider: |
| 156 |
host: "{{ inventory_hostname_short }}" |
| 157 |
username: admin |
| 158 |
password: admin |
| 159 |
connection: local |
| 160 |
|
| 161 |
- name: Add a comment to a DTC LBDN |
| 162 |
infoblox.nios_modules.nios_dtc_lbdn: |
| 163 |
name: web.ansible.com |
| 164 |
lb_method: ROUND_ROBIN |
| 165 |
comment: this is a test comment |
| 166 |
state: present |
| 167 |
provider: |
| 168 |
host: "{{ inventory_hostname_short }}" |
| 169 |
username: admin |
| 170 |
password: admin |
| 171 |
connection: local |
| 172 |
|
| 173 |
- name: Remove a DTC LBDN from the system |
| 174 |
infoblox.nios_modules.nios_dtc_lbdn: |
| 175 |
name: web.ansible.com |
| 176 |
lb_method: ROUND_ROBIN |
| 177 |
state: absent |
| 178 |
provider: |
| 179 |
host: "{{ inventory_hostname_short }}" |
| 180 |
username: admin |
| 181 |
password: admin |
| 182 |
connection: local |
| 183 |
''' |
| 184 |
|
| 185 |
RETURN = ''' # ''' |
| 186 |
|
| 187 |
from ..module_utils.api import NIOS_DTC_LBDN |
| 188 |
from ..module_utils.api import WapiModule |
| 189 |
from ..module_utils.api import normalize_ib_spec |
| 190 |
from ansible.module_utils.basic import AnsibleModule |
| 191 |
|
| 192 |
|
| 193 |
def main(): |
| 194 |
''' Main entry point for module execution |
| 195 |
''' |
| 196 |
|
| 197 |
def auth_zones_transform(module): |
| 198 |
"""Transform auth_zones parameter to WAPI-compatible zone references.""" |
| 199 |
auth_zones = module.params.get('auth_zones') |
| 200 |
if not auth_zones: |
| 201 |
return [] |
| 202 |
|
| 203 |
|
| 204 |
fqdn_counter = {} |
| 205 |
for zone in auth_zones: |
| 206 |
fqdn = zone.get('fqdn') if isinstance(zone, dict) else zone |
| 207 |
if not fqdn: |
| 208 |
module.fail_json(msg=f"Invalid auth_zone format: {zone}. Must contain FQDN.") |
| 209 |
fqdn_counter[fqdn] = fqdn_counter.get(fqdn, 0) + 1 |
| 210 |
|
| 211 |
|
| 212 |
refs = [] |
| 213 |
for zone in auth_zones: |
| 214 |
|
| 215 |
if isinstance(zone, dict): |
| 216 |
if 'fqdn' not in zone: |
| 217 |
module.fail_json(msg=f"Invalid auth_zone: {zone}. Missing 'fqdn' key.") |
| 218 |
|
| 219 |
fqdn = zone['fqdn'] |
| 220 |
view = zone.get('view') |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
if fqdn_counter[fqdn] > 1 and not view: |
| 225 |
module.fail_json(msg=f"Multiple '{fqdn}' zones require view specification.") |
| 226 |
|
| 227 |
|
| 228 |
query = {'fqdn': fqdn} |
| 229 |
if view: |
| 230 |
query['view'] = view |
| 231 |
display = f"{fqdn}:{view}" |
| 232 |
else: |
| 233 |
display = fqdn |
| 234 |
|
| 235 |
else: |
| 236 |
fqdn = zone |
| 237 |
display = fqdn |
| 238 |
query = {'fqdn': fqdn} |
| 239 |
|
| 240 |
if fqdn_counter[fqdn] > 1: |
| 241 |
module.fail_json(msg=f"Multiple '{fqdn}' zones require view specification.") |
| 242 |
|
| 243 |
try: |
| 244 |
|
| 245 |
zone_obj = wapi.get_object('zone_auth', query) |
| 246 |
if not zone_obj: |
| 247 |
module.fail_json(msg=f"Zone '{display}' not found.") |
| 248 |
|
| 249 |
refs.append(zone_obj[0]['_ref']) |
| 250 |
|
| 251 |
except Exception as e: |
| 252 |
module.fail_json(msg=f"Error processing zone '{display}': {e}") |
| 253 |
|
| 254 |
return refs |
| 255 |
|
| 256 |
def pools_transform(module): |
| 257 |
pool_list = list() |
| 258 |
if module.params['pools']: |
| 259 |
for pool in module.params['pools']: |
| 260 |
pool_obj = wapi.get_object('dtc:pool', |
| 261 |
{'name': pool['pool']}) |
| 262 |
if 'ratio' not in pool: |
| 263 |
pool['ratio'] = 1 |
| 264 |
if pool_obj: |
| 265 |
pool_list.append({'pool': pool_obj[0]['_ref'], |
| 266 |
'ratio': pool['ratio']}) |
| 267 |
else: |
| 268 |
module.fail_json(msg='pool %s cannot be found.' % pool) |
| 269 |
return pool_list |
| 270 |
|
| 271 |
def topology_transform(module): |
| 272 |
topology = module.params['topology'] |
| 273 |
if topology: |
| 274 |
topo_obj = wapi.get_object('dtc:topology', {'name': topology}) |
| 275 |
if topo_obj: |
| 276 |
return topo_obj[0]['_ref'] |
| 277 |
else: |
| 278 |
module.fail_json( |
| 279 |
msg='topology %s cannot be found.' % topology) |
| 280 |
|
| 281 |
auth_zones_spec = dict() |
| 282 |
|
| 283 |
pools_spec = dict( |
| 284 |
pool=dict(required=True), |
| 285 |
ratio=dict(type='int', default=1) |
| 286 |
) |
| 287 |
|
| 288 |
ib_spec = dict( |
| 289 |
name=dict(required=True, ib_req=True), |
| 290 |
lb_method=dict(required=True, choices=['GLOBAL_AVAILABILITY', |
| 291 |
'RATIO', 'ROUND_ROBIN', 'TOPOLOGY']), |
| 292 |
|
| 293 |
topology=dict(type='str', transform=topology_transform), |
| 294 |
auth_zones=dict(type='list', elements='raw', options=auth_zones_spec, |
| 295 |
transform=auth_zones_transform), |
| 296 |
patterns=dict(type='list', elements='str'), |
| 297 |
types=dict(type='list', elements='str', choices=['A', 'AAAA', 'CNAME', 'NAPTR', |
| 298 |
'SRV']), |
| 299 |
pools=dict(type='list', elements='dict', options=pools_spec, |
| 300 |
transform=pools_transform), |
| 301 |
ttl=dict(type='int'), |
| 302 |
|
| 303 |
extattrs=dict(type='dict'), |
| 304 |
comment=dict(), |
| 305 |
) |
| 306 |
|
| 307 |
argument_spec = dict( |
| 308 |
provider=dict(required=True), |
| 309 |
state=dict(default='present', choices=['present', 'absent']) |
| 310 |
) |
| 311 |
|
| 312 |
argument_spec.update(normalize_ib_spec(ib_spec)) |
| 313 |
argument_spec.update(WapiModule.provider_spec) |
| 314 |
|
| 315 |
module = AnsibleModule(argument_spec=argument_spec, |
| 316 |
supports_check_mode=True) |
| 317 |
|
| 318 |
wapi = WapiModule(module) |
| 319 |
result = wapi.run(NIOS_DTC_LBDN, ib_spec) |
| 320 |
|
| 321 |
module.exit_json(**result) |
| 322 |
|
| 323 |
|
| 324 |
if __name__ == '__main__': |
| 325 |
main() |