| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = r''' |
| 11 |
--- |
| 12 |
module: bigip_cli_alias |
| 13 |
short_description: Manage CLI aliases on a BIG-IP |
| 14 |
description: |
| 15 |
- Allows for managing both private and shared aliases on a BIG-IP. |
| 16 |
version_added: "1.0.0" |
| 17 |
options: |
| 18 |
name: |
| 19 |
description: |
| 20 |
- Specifies the name of the alias. |
| 21 |
type: str |
| 22 |
required: True |
| 23 |
scope: |
| 24 |
description: |
| 25 |
- The scope of the alias; whether it is shared on the system, or usable only |
| 26 |
for the user who created it. |
| 27 |
type: str |
| 28 |
default: shared |
| 29 |
choices: |
| 30 |
- private |
| 31 |
- shared |
| 32 |
command: |
| 33 |
description: |
| 34 |
- The command to alias. |
| 35 |
type: str |
| 36 |
description: |
| 37 |
description: |
| 38 |
- Description of the alias. |
| 39 |
type: str |
| 40 |
partition: |
| 41 |
description: |
| 42 |
- Device partition on which to manage resources. |
| 43 |
- This parameter is disregarded when the C(scope) is C(private). |
| 44 |
type: str |
| 45 |
default: Common |
| 46 |
state: |
| 47 |
description: |
| 48 |
- When C(present), ensures the resource exists. |
| 49 |
- When C(absent), ensures the resource is removed. |
| 50 |
type: str |
| 51 |
default: present |
| 52 |
choices: |
| 53 |
- present |
| 54 |
- absent |
| 55 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 56 |
author: |
| 57 |
- Tim Rupp (@caphrim007) |
| 58 |
- Wojciech Wypior (@wojtek0806) |
| 59 |
''' |
| 60 |
|
| 61 |
EXAMPLES = r''' |
| 62 |
- name: Create a new alias |
| 63 |
bigip_cli_alias: |
| 64 |
name: sync_device_to_bside |
| 65 |
scope: shared |
| 66 |
command: save /sys config partitions all; run /cm config-sync to-group device-group-1 |
| 67 |
provider: |
| 68 |
password: secret |
| 69 |
server: lb.mydomain.com |
| 70 |
user: admin |
| 71 |
delegate_to: localhost |
| 72 |
''' |
| 73 |
|
| 74 |
RETURN = r''' |
| 75 |
command: |
| 76 |
description: The new command that is aliased. |
| 77 |
returned: changed |
| 78 |
type: str |
| 79 |
sample: run /util bash |
| 80 |
description: |
| 81 |
description: The new description of the alias. |
| 82 |
returned: changed |
| 83 |
type: str |
| 84 |
sample: Run the bash shell |
| 85 |
''' |
| 86 |
from datetime import datetime |
| 87 |
|
| 88 |
from ansible.module_utils.basic import ( |
| 89 |
AnsibleModule, env_fallback |
| 90 |
) |
| 91 |
|
| 92 |
from ..module_utils.bigip import F5RestClient |
| 93 |
from ..module_utils.common import ( |
| 94 |
F5ModuleError, AnsibleF5Parameters, f5_argument_spec, transform_name |
| 95 |
) |
| 96 |
from ..module_utils.icontrol import tmos_version |
| 97 |
from ..module_utils.teem import send_teem |
| 98 |
|
| 99 |
|
| 100 |
class Parameters(AnsibleF5Parameters): |
| 101 |
api_map = { |
| 102 |
'tmCommand': 'command' |
| 103 |
} |
| 104 |
|
| 105 |
api_attributes = [ |
| 106 |
'tmCommand', |
| 107 |
'description', |
| 108 |
] |
| 109 |
|
| 110 |
returnables = [ |
| 111 |
'command', |
| 112 |
'description', |
| 113 |
] |
| 114 |
|
| 115 |
updatables = [ |
| 116 |
'command', |
| 117 |
'description', |
| 118 |
] |
| 119 |
|
| 120 |
@property |
| 121 |
def full_name(self): |
| 122 |
if self.scope == 'shared': |
| 123 |
return transform_name(self.partition, self.name) |
| 124 |
else: |
| 125 |
return self.name |
| 126 |
|
| 127 |
|
| 128 |
class ApiParameters(Parameters): |
| 129 |
pass |
| 130 |
|
| 131 |
|
| 132 |
class ModuleParameters(Parameters): |
| 133 |
pass |
| 134 |
|
| 135 |
|
| 136 |
class Changes(Parameters): |
| 137 |
def to_return(self): |
| 138 |
result = {} |
| 139 |
try: |
| 140 |
for returnable in self.returnables: |
| 141 |
result[returnable] = getattr(self, returnable) |
| 142 |
result = self._filter_params(result) |
| 143 |
except Exception: |
| 144 |
raise |
| 145 |
return result |
| 146 |
|
| 147 |
|
| 148 |
class UsableChanges(Changes): |
| 149 |
pass |
| 150 |
|
| 151 |
|
| 152 |
class ReportableChanges(Changes): |
| 153 |
pass |
| 154 |
|
| 155 |
|
| 156 |
class Difference(object): |
| 157 |
def __init__(self, want, have=None): |
| 158 |
self.want = want |
| 159 |
self.have = have |
| 160 |
|
| 161 |
def compare(self, param): |
| 162 |
try: |
| 163 |
result = getattr(self, param) |
| 164 |
return result |
| 165 |
except AttributeError: |
| 166 |
return self.__default(param) |
| 167 |
|
| 168 |
def __default(self, param): |
| 169 |
attr1 = getattr(self.want, param) |
| 170 |
try: |
| 171 |
attr2 = getattr(self.have, param) |
| 172 |
if attr1 != attr2: |
| 173 |
return attr1 |
| 174 |
except AttributeError: |
| 175 |
return attr1 |
| 176 |
|
| 177 |
|
| 178 |
class ModuleManager(object): |
| 179 |
def __init__(self, *args, **kwargs): |
| 180 |
self.module = kwargs.get('module', None) |
| 181 |
self.client = F5RestClient(**self.module.params) |
| 182 |
self.want = ModuleParameters(params=self.module.params) |
| 183 |
self.have = ApiParameters() |
| 184 |
self.changes = UsableChanges() |
| 185 |
|
| 186 |
def _set_changed_options(self): |
| 187 |
changed = {} |
| 188 |
for key in Parameters.returnables: |
| 189 |
if getattr(self.want, key) is not None: |
| 190 |
changed[key] = getattr(self.want, key) |
| 191 |
if changed: |
| 192 |
self.changes = UsableChanges(params=changed) |
| 193 |
|
| 194 |
def _update_changed_options(self): |
| 195 |
diff = Difference(self.want, self.have) |
| 196 |
updatables = Parameters.updatables |
| 197 |
changed = dict() |
| 198 |
for k in updatables: |
| 199 |
change = diff.compare(k) |
| 200 |
if change is None: |
| 201 |
continue |
| 202 |
else: |
| 203 |
if isinstance(change, dict): |
| 204 |
changed.update(change) |
| 205 |
else: |
| 206 |
changed[k] = change |
| 207 |
if changed: |
| 208 |
self.changes = UsableChanges(params=changed) |
| 209 |
return True |
| 210 |
return False |
| 211 |
|
| 212 |
def should_update(self): |
| 213 |
result = self._update_changed_options() |
| 214 |
if result: |
| 215 |
return True |
| 216 |
return False |
| 217 |
|
| 218 |
def exec_module(self): |
| 219 |
start = datetime.now().isoformat() |
| 220 |
version = tmos_version(self.client) |
| 221 |
changed = False |
| 222 |
result = dict() |
| 223 |
state = self.want.state |
| 224 |
|
| 225 |
if state == "present": |
| 226 |
changed = self.present() |
| 227 |
elif state == "absent": |
| 228 |
changed = self.absent() |
| 229 |
|
| 230 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 231 |
changes = reportable.to_return() |
| 232 |
result.update(**changes) |
| 233 |
result.update(dict(changed=changed)) |
| 234 |
self._announce_deprecations(result) |
| 235 |
send_teem(start, self.client, self.module, version) |
| 236 |
return result |
| 237 |
|
| 238 |
def _announce_deprecations(self, result): |
| 239 |
warnings = result.pop('__warnings', []) |
| 240 |
for warning in warnings: |
| 241 |
self.client.module.deprecate( |
| 242 |
msg=warning['msg'], |
| 243 |
version=warning['version'] |
| 244 |
) |
| 245 |
|
| 246 |
def present(self): |
| 247 |
if self.exists(): |
| 248 |
return self.update() |
| 249 |
else: |
| 250 |
return self.create() |
| 251 |
|
| 252 |
def exists(self): |
| 253 |
errors = [401, 403, 409, 500, 501, 502, 503, 504] |
| 254 |
uri = "https://{0}:{1}/mgmt/tm/cli/alias/{2}/{3}".format( |
| 255 |
self.client.provider['server'], |
| 256 |
self.client.provider['server_port'], |
| 257 |
self.want.scope, |
| 258 |
self.want.full_name |
| 259 |
) |
| 260 |
resp = self.client.api.get(uri) |
| 261 |
try: |
| 262 |
response = resp.json() |
| 263 |
except ValueError as ex: |
| 264 |
raise F5ModuleError(str(ex)) |
| 265 |
|
| 266 |
if resp.status == 404 or 'code' in response and response['code'] == 404: |
| 267 |
return False |
| 268 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 269 |
return True |
| 270 |
|
| 271 |
if resp.status in errors or 'code' in response and response['code'] in errors: |
| 272 |
if 'message' in response: |
| 273 |
raise F5ModuleError(response['message']) |
| 274 |
else: |
| 275 |
raise F5ModuleError(resp.content) |
| 276 |
|
| 277 |
def update(self): |
| 278 |
self.have = self.read_current_from_device() |
| 279 |
if not self.should_update(): |
| 280 |
return False |
| 281 |
if self.module.check_mode: |
| 282 |
return True |
| 283 |
self.update_on_device() |
| 284 |
return True |
| 285 |
|
| 286 |
def remove(self): |
| 287 |
if self.module.check_mode: |
| 288 |
return True |
| 289 |
self.remove_from_device() |
| 290 |
if self.exists(): |
| 291 |
raise F5ModuleError("Failed to delete the resource.") |
| 292 |
return True |
| 293 |
|
| 294 |
def create(self): |
| 295 |
self._set_changed_options() |
| 296 |
if self.module.check_mode: |
| 297 |
return True |
| 298 |
self.create_on_device() |
| 299 |
return True |
| 300 |
|
| 301 |
def create_on_device(self): |
| 302 |
params = self.changes.api_params() |
| 303 |
params['name'] = self.want.name |
| 304 |
params['partition'] = self.want.partition |
| 305 |
uri = "https://{0}:{1}/mgmt/tm/cli/alias/{2}/".format( |
| 306 |
self.client.provider['server'], |
| 307 |
self.client.provider['server_port'], |
| 308 |
self.want.scope |
| 309 |
) |
| 310 |
resp = self.client.api.post(uri, json=params) |
| 311 |
try: |
| 312 |
response = resp.json() |
| 313 |
except ValueError as ex: |
| 314 |
raise F5ModuleError(str(ex)) |
| 315 |
|
| 316 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 317 |
return True |
| 318 |
raise F5ModuleError(resp.content) |
| 319 |
|
| 320 |
def update_on_device(self): |
| 321 |
params = self.changes.api_params() |
| 322 |
uri = "https://{0}:{1}/mgmt/tm/cli/alias/{2}/{3}".format( |
| 323 |
self.client.provider['server'], |
| 324 |
self.client.provider['server_port'], |
| 325 |
self.want.scope, |
| 326 |
self.want.full_name |
| 327 |
) |
| 328 |
resp = self.client.api.patch(uri, json=params) |
| 329 |
try: |
| 330 |
response = resp.json() |
| 331 |
except ValueError as ex: |
| 332 |
raise F5ModuleError(str(ex)) |
| 333 |
|
| 334 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 335 |
return True |
| 336 |
raise F5ModuleError(resp.content) |
| 337 |
|
| 338 |
def absent(self): |
| 339 |
if self.exists(): |
| 340 |
return self.remove() |
| 341 |
return False |
| 342 |
|
| 343 |
def remove_from_device(self): |
| 344 |
uri = "https://{0}:{1}/mgmt/tm/cli/alias/{2}/{3}".format( |
| 345 |
self.client.provider['server'], |
| 346 |
self.client.provider['server_port'], |
| 347 |
self.want.scope, |
| 348 |
self.want.full_name |
| 349 |
) |
| 350 |
response = self.client.api.delete(uri) |
| 351 |
|
| 352 |
if response.status in [200, 201]: |
| 353 |
return True |
| 354 |
raise F5ModuleError(response.content) |
| 355 |
|
| 356 |
def read_current_from_device(self): |
| 357 |
uri = "https://{0}:{1}/mgmt/tm/cli/alias/{2}/{3}".format( |
| 358 |
self.client.provider['server'], |
| 359 |
self.client.provider['server_port'], |
| 360 |
self.want.scope, |
| 361 |
self.want.full_name |
| 362 |
) |
| 363 |
resp = self.client.api.get(uri) |
| 364 |
try: |
| 365 |
response = resp.json() |
| 366 |
except ValueError as ex: |
| 367 |
raise F5ModuleError(str(ex)) |
| 368 |
|
| 369 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 370 |
return ApiParameters(params=response) |
| 371 |
raise F5ModuleError(resp.content) |
| 372 |
|
| 373 |
|
| 374 |
class ArgumentSpec(object): |
| 375 |
def __init__(self): |
| 376 |
self.supports_check_mode = True |
| 377 |
argument_spec = dict( |
| 378 |
name=dict(required=True), |
| 379 |
scope=dict( |
| 380 |
choices=['private', 'shared'], |
| 381 |
default='shared' |
| 382 |
), |
| 383 |
description=dict(), |
| 384 |
command=dict(), |
| 385 |
state=dict( |
| 386 |
default='present', |
| 387 |
choices=['present', 'absent'] |
| 388 |
), |
| 389 |
partition=dict( |
| 390 |
default='Common', |
| 391 |
fallback=(env_fallback, ['F5_PARTITION']) |
| 392 |
) |
| 393 |
) |
| 394 |
self.argument_spec = {} |
| 395 |
self.argument_spec.update(f5_argument_spec) |
| 396 |
self.argument_spec.update(argument_spec) |
| 397 |
|
| 398 |
|
| 399 |
def main(): |
| 400 |
spec = ArgumentSpec() |
| 401 |
|
| 402 |
module = AnsibleModule( |
| 403 |
argument_spec=spec.argument_spec, |
| 404 |
supports_check_mode=spec.supports_check_mode, |
| 405 |
) |
| 406 |
|
| 407 |
try: |
| 408 |
mm = ModuleManager(module=module) |
| 409 |
results = mm.exec_module() |
| 410 |
module.exit_json(**results) |
| 411 |
except F5ModuleError as ex: |
| 412 |
module.fail_json(msg=str(ex)) |
| 413 |
|
| 414 |
|
| 415 |
if __name__ == '__main__': |
| 416 |
main() |