| 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_imish_config |
| 13 |
short_description: Manage BIG-IP advanced routing configuration sections |
| 14 |
description: |
| 15 |
- This module provides an implementation for working with advanced routing |
| 16 |
configuration sections in a deterministic way. |
| 17 |
version_added: "1.0.0" |
| 18 |
options: |
| 19 |
route_domain: |
| 20 |
description: |
| 21 |
- Route domain on which to manage the BGP configuration. |
| 22 |
type: str |
| 23 |
default: "0" |
| 24 |
lines: |
| 25 |
description: |
| 26 |
- The ordered set of commands that should be configured in the |
| 27 |
section. |
| 28 |
- The commands must be the exact same as those found in the device |
| 29 |
running-config. |
| 30 |
- Be sure to note the configuration command syntax, as some commands |
| 31 |
are automatically modified by the device config parser. |
| 32 |
type: list |
| 33 |
elements: str |
| 34 |
aliases: ['commands'] |
| 35 |
parents: |
| 36 |
description: |
| 37 |
- The ordered set of parents that uniquely identify the section or hierarchy |
| 38 |
the commands should be checked against. |
| 39 |
- If the C(parents) argument is omitted, the commands are checked against |
| 40 |
the set of top level or global commands. |
| 41 |
type: list |
| 42 |
elements: str |
| 43 |
src: |
| 44 |
description: |
| 45 |
- The I(src) argument provides a path to the configuration file |
| 46 |
to load into the remote system. |
| 47 |
- The path can either be a full system path to the configuration |
| 48 |
file if the value starts with /, or relative to the root of the |
| 49 |
implemented role or playbook. |
| 50 |
- This argument is mutually exclusive with the I(lines) and |
| 51 |
I(parents) arguments. |
| 52 |
type: path |
| 53 |
before: |
| 54 |
description: |
| 55 |
- The ordered set of commands to push onto the command stack if |
| 56 |
a change needs to be made. |
| 57 |
- This allows the playbook designer the opportunity to perform |
| 58 |
configuration commands prior to pushing any changes, without |
| 59 |
affecting how the set of commands are matched against the system. |
| 60 |
type: list |
| 61 |
elements: str |
| 62 |
after: |
| 63 |
description: |
| 64 |
- The ordered set of commands to append to the end of the command |
| 65 |
stack if a change needs to be made. |
| 66 |
- Just like with I(before), this allows the playbook designer to |
| 67 |
append a set of commands to be executed after the command set. |
| 68 |
type: list |
| 69 |
elements: str |
| 70 |
match: |
| 71 |
description: |
| 72 |
- Instructs the module on the way to perform the matching of |
| 73 |
the set of commands against the current device config. |
| 74 |
- If match is set to I(line), commands are matched line by line. |
| 75 |
- If match is set to I(strict), command lines are matched with respect |
| 76 |
to position. |
| 77 |
- If match is set to I(exact), command lines must be an equal match. |
| 78 |
- Finally, if match is set to I(none), the module will not attempt to |
| 79 |
compare the source configuration with the running configuration on |
| 80 |
the remote device. |
| 81 |
type: str |
| 82 |
choices: |
| 83 |
- line |
| 84 |
- strict |
| 85 |
- exact |
| 86 |
- none |
| 87 |
default: line |
| 88 |
replace: |
| 89 |
description: |
| 90 |
- Instructs the module on the way to perform the configuration |
| 91 |
on the device. |
| 92 |
- If the replace argument is set to I(line), the modified lines |
| 93 |
are pushed to the device in configuration mode. |
| 94 |
- If the replace argument is set to I(block), the entire |
| 95 |
command block is pushed to the device in configuration mode if any |
| 96 |
line is not correct. |
| 97 |
type: str |
| 98 |
choices: |
| 99 |
- line |
| 100 |
- block |
| 101 |
default: line |
| 102 |
backup: |
| 103 |
description: |
| 104 |
- This argument will cause the module to create a full backup of |
| 105 |
the current C(running-config) from the remote device before any |
| 106 |
changes are made. |
| 107 |
- The backup file is written to the C(backup) folder in the playbook |
| 108 |
root directory or role root directory, if playbook is part of an |
| 109 |
Ansible role. If the directory does not exist, it is created. |
| 110 |
type: bool |
| 111 |
default: no |
| 112 |
running_config: |
| 113 |
description: |
| 114 |
- By default, the module will connect to the remote device and |
| 115 |
retrieve the current running-config to use as a base for comparing |
| 116 |
against the contents of source. |
| 117 |
- There are times when you do not want to have the task get the |
| 118 |
current running-config for every task in a playbook. |
| 119 |
- The I(running_config) argument allows the implementer to pass in |
| 120 |
the configuration to use as the base config for comparison. |
| 121 |
type: str |
| 122 |
aliases: ['config'] |
| 123 |
save_when: |
| 124 |
description: |
| 125 |
- When changes are made to the device running-configuration, the |
| 126 |
changes are not copied to non-volatile storage by default. |
| 127 |
- If the argument is set to I(always), the running-config will |
| 128 |
always be copied to the startup-config and the I(modified) flag will |
| 129 |
always be set to C(True). |
| 130 |
- If the argument is set to I(modified), the running-config |
| 131 |
will only be copied to the startup-config if it has changed since |
| 132 |
the last save to startup-config. |
| 133 |
- If the argument is set to I(never), the running-config will never be |
| 134 |
copied to the startup-config. |
| 135 |
- If the argument is set to I(changed), the running-config |
| 136 |
will only be copied to the startup-config if the task has made a change. |
| 137 |
type: str |
| 138 |
choices: |
| 139 |
- always |
| 140 |
- never |
| 141 |
- modified |
| 142 |
- changed |
| 143 |
default: never |
| 144 |
diff_against: |
| 145 |
description: |
| 146 |
- When using the C(ansible-playbook --diff) command line argument, |
| 147 |
the module can generate diffs against different sources. |
| 148 |
- When this option is configured as I(startup), the module will return |
| 149 |
the diff of the running-config against the startup-config. |
| 150 |
- When this option is configured as I(intended), the module will |
| 151 |
return the diff of the running-config against the configuration |
| 152 |
provided in the C(intended_config) argument. |
| 153 |
- When this option is configured as I(running), the module will |
| 154 |
return the before and after diff of the running-config with respect |
| 155 |
to any changes made to the device configuration. |
| 156 |
type: str |
| 157 |
choices: |
| 158 |
- startup |
| 159 |
- intended |
| 160 |
- running |
| 161 |
default: startup |
| 162 |
diff_ignore_lines: |
| 163 |
description: |
| 164 |
- Use this argument to specify one or more lines that should be |
| 165 |
ignored during the diff. |
| 166 |
- This is used for lines in the configuration that are automatically |
| 167 |
updated by the system. |
| 168 |
- This argument takes a list of regular expressions or exact line matches. |
| 169 |
type: list |
| 170 |
elements: str |
| 171 |
intended_config: |
| 172 |
description: |
| 173 |
- The C(intended_config) provides the master configuration |
| 174 |
the node should conform to and is used to check the final |
| 175 |
running-config against. |
| 176 |
- This argument will not modify any settings on the remote device and |
| 177 |
is strictly used to check the compliance of the current device's |
| 178 |
configuration against. |
| 179 |
- When specifying this argument, the task should also modify the |
| 180 |
C(diff_against) value and set it to I(intended). |
| 181 |
type: str |
| 182 |
backup_options: |
| 183 |
description: |
| 184 |
- This is a dict object containing configurable options related to backup file path. |
| 185 |
The value of this option is read-only when C(backup) is set to I(yes). If C(backup) is set |
| 186 |
to I(no), this option will be silently ignored. |
| 187 |
suboptions: |
| 188 |
filename: |
| 189 |
description: |
| 190 |
- The filename to be used to store the backup configuration. If the filename |
| 191 |
is not given, it will be generated based on the hostname, current time and date |
| 192 |
in the format defined by <hostname>_config.<current-date>@<current-time> |
| 193 |
type: str |
| 194 |
dir_path: |
| 195 |
description: |
| 196 |
- This option provides the path ending with directory name in which the backup |
| 197 |
configuration file will be stored. If the directory does not exist, it will be first |
| 198 |
created and the filename is either the value of C(filename) or default filename |
| 199 |
as described in C(filename) options description. If the path value is not given, |
| 200 |
a I(backup) directory will be created in the current working directory |
| 201 |
and backup configuration will be copied in C(filename) within the I(backup) directory. |
| 202 |
type: path |
| 203 |
type: dict |
| 204 |
allow_duplicates: |
| 205 |
description: |
| 206 |
- Allows duplicate commands to be sent to the device. This is to accommodate scenarios where |
| 207 |
address families are configured. |
| 208 |
- Only used with the C(lines) parameter. |
| 209 |
type: bool |
| 210 |
default: false |
| 211 |
version_added: "1.2.0" |
| 212 |
notes: |
| 213 |
- Abbreviated commands are NOT idempotent |
| 214 |
extends_documentation_fragment: |
| 215 |
- f5networks.f5_modules.f5 |
| 216 |
- ansible.builtin.files |
| 217 |
author: |
| 218 |
- Tim Rupp (@caphrim007) |
| 219 |
- Wojciech Wypior (@wojtek0806) |
| 220 |
''' |
| 221 |
|
| 222 |
EXAMPLES = r''' |
| 223 |
- name: configure top level configuration and save it |
| 224 |
bigip_imish_config: |
| 225 |
lines: bfd slow-timer 2000 |
| 226 |
save_when: modified |
| 227 |
provider: |
| 228 |
user: admin |
| 229 |
password: secret |
| 230 |
server: lb.mydomain.com |
| 231 |
delegate_to: localhost |
| 232 |
|
| 233 |
- name: diff the running-config against a provided config |
| 234 |
bigip_imish_config: |
| 235 |
diff_against: intended |
| 236 |
intended_config: "{{ lookup('file', 'master.cfg') }}" |
| 237 |
provider: |
| 238 |
user: admin |
| 239 |
password: secret |
| 240 |
server: lb.mydomain.com |
| 241 |
delegate_to: localhost |
| 242 |
|
| 243 |
- name: Add config to a parent block |
| 244 |
bigip_imish_config: |
| 245 |
lines: |
| 246 |
- bgp graceful-restart restart-time 120 |
| 247 |
- redistribute kernel route-map rhi |
| 248 |
- neighbor 10.10.10.11 remote-as 65000 |
| 249 |
- neighbor 10.10.10.11 fall-over bfd |
| 250 |
- neighbor 10.10.10.11 remote-as 65000 |
| 251 |
- neighbor 10.10.10.11 fall-over bfd |
| 252 |
parents: router bgp 64664 |
| 253 |
match: exact |
| 254 |
provider: |
| 255 |
user: admin |
| 256 |
password: secret |
| 257 |
server: lb.mydomain.com |
| 258 |
delegate_to: localhost |
| 259 |
|
| 260 |
- name: Remove an existing acl before writing it |
| 261 |
bigip_imish_config: |
| 262 |
lines: |
| 263 |
- access-list 10 permit 20.20.20.20 |
| 264 |
- access-list 10 permit 20.20.20.21 |
| 265 |
- access-list 10 deny any |
| 266 |
before: no access-list 10 |
| 267 |
provider: |
| 268 |
user: admin |
| 269 |
password: secret |
| 270 |
server: lb.mydomain.com |
| 271 |
delegate_to: localhost |
| 272 |
|
| 273 |
- name: for idempotency, use full-form commands |
| 274 |
bigip_imish_config: |
| 275 |
lines: |
| 276 |
# - desc My interface |
| 277 |
- description My Interface |
| 278 |
# parents: int ANYCAST-P2P-2 |
| 279 |
parents: interface ANYCAST-P2P-2 |
| 280 |
provider: |
| 281 |
user: admin |
| 282 |
password: secret |
| 283 |
server: lb.mydomain.com |
| 284 |
delegate_to: localhost |
| 285 |
|
| 286 |
- name: configurable backup path |
| 287 |
bigip_imish_config: |
| 288 |
lines: bfd slow-timer 2000 |
| 289 |
backup: true |
| 290 |
provider: |
| 291 |
user: admin |
| 292 |
password: secret |
| 293 |
server: lb.mydomain.com |
| 294 |
backup_options: |
| 295 |
filename: backup.cfg |
| 296 |
dir_path: /home/user |
| 297 |
delegate_to: localhost |
| 298 |
''' |
| 299 |
|
| 300 |
RETURN = r''' |
| 301 |
commands: |
| 302 |
description: The set of commands that will be pushed to the remote device. |
| 303 |
returned: always |
| 304 |
type: list |
| 305 |
sample: ['interface ANYCAST-P2P-2', 'neighbor 20.20.20.21 remote-as 65000', 'neighbor 20.20.20.21 fall-over bfd'] |
| 306 |
updates: |
| 307 |
description: The set of commands that will be pushed to the remote device. |
| 308 |
returned: always |
| 309 |
type: list |
| 310 |
sample: ['interface ANYCAST-P2P-2', 'neighbor 20.20.20.21 remote-as 65000', 'neighbor 20.20.20.21 fall-over bfd'] |
| 311 |
backup_path: |
| 312 |
description: The full path to the backup file. |
| 313 |
returned: when backup is yes |
| 314 |
type: str |
| 315 |
sample: /playbooks/ansible/backup/bigip_imish_config.2016-07-16@22:28:34 |
| 316 |
''' |
| 317 |
|
| 318 |
|
| 319 |
try: |
| 320 |
from StringIO import StringIO |
| 321 |
except ImportError: |
| 322 |
from io import StringIO |
| 323 |
|
| 324 |
|
| 325 |
import os |
| 326 |
import tempfile |
| 327 |
import time |
| 328 |
from datetime import datetime |
| 329 |
|
| 330 |
from ansible.module_utils.basic import AnsibleModule |
| 331 |
|
| 332 |
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.config import dumps |
| 333 |
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import to_list |
| 334 |
|
| 335 |
from ..module_utils.bigip import F5RestClient |
| 336 |
from ..module_utils.common import ( |
| 337 |
F5ModuleError, AnsibleF5Parameters, ImishConfig, f5_argument_spec |
| 338 |
) |
| 339 |
from ..module_utils.icontrol import ( |
| 340 |
upload_file, tmos_version |
| 341 |
) |
| 342 |
from ..module_utils.teem import send_teem |
| 343 |
|
| 344 |
|
| 345 |
class Parameters(AnsibleF5Parameters): |
| 346 |
api_map = { |
| 347 |
|
| 348 |
} |
| 349 |
|
| 350 |
api_attributes = [ |
| 351 |
|
| 352 |
] |
| 353 |
|
| 354 |
returnables = [ |
| 355 |
'__backup__', |
| 356 |
'commands', |
| 357 |
'updates' |
| 358 |
] |
| 359 |
|
| 360 |
updatables = [ |
| 361 |
|
| 362 |
] |
| 363 |
|
| 364 |
|
| 365 |
class ApiParameters(Parameters): |
| 366 |
pass |
| 367 |
|
| 368 |
|
| 369 |
class ModuleParameters(Parameters): |
| 370 |
pass |
| 371 |
|
| 372 |
|
| 373 |
class Changes(Parameters): |
| 374 |
def to_return(self): |
| 375 |
result = {} |
| 376 |
try: |
| 377 |
for returnable in self.returnables: |
| 378 |
result[returnable] = getattr(self, returnable) |
| 379 |
result = self._filter_params(result) |
| 380 |
except Exception: |
| 381 |
raise |
| 382 |
return result |
| 383 |
|
| 384 |
|
| 385 |
class UsableChanges(Changes): |
| 386 |
pass |
| 387 |
|
| 388 |
|
| 389 |
class ReportableChanges(Changes): |
| 390 |
pass |
| 391 |
|
| 392 |
|
| 393 |
class Difference(object): |
| 394 |
def __init__(self, want, have=None): |
| 395 |
self.want = want |
| 396 |
self.have = have |
| 397 |
|
| 398 |
def compare(self, param): |
| 399 |
try: |
| 400 |
result = getattr(self, param) |
| 401 |
return result |
| 402 |
except AttributeError: |
| 403 |
return self.__default(param) |
| 404 |
|
| 405 |
def __default(self, param): |
| 406 |
attr1 = getattr(self.want, param) |
| 407 |
try: |
| 408 |
attr2 = getattr(self.have, param) |
| 409 |
if attr1 != attr2: |
| 410 |
return attr1 |
| 411 |
except AttributeError: |
| 412 |
return attr1 |
| 413 |
|
| 414 |
|
| 415 |
class ModuleManager(object): |
| 416 |
def __init__(self, *args, **kwargs): |
| 417 |
self.module = kwargs.get('module', None) |
| 418 |
self.client = F5RestClient(**self.module.params) |
| 419 |
self.want = ModuleParameters(params=self.module.params) |
| 420 |
self.have = ApiParameters() |
| 421 |
self.changes = UsableChanges() |
| 422 |
|
| 423 |
def _set_changed_options(self): |
| 424 |
changed = {} |
| 425 |
for key in Parameters.returnables: |
| 426 |
if getattr(self.want, key) is not None: |
| 427 |
changed[key] = getattr(self.want, key) |
| 428 |
if changed: |
| 429 |
self.changes = UsableChanges(params=changed) |
| 430 |
|
| 431 |
def _update_changed_options(self): |
| 432 |
diff = Difference(self.want, self.have) |
| 433 |
updatables = Parameters.updatables |
| 434 |
changed = dict() |
| 435 |
for k in updatables: |
| 436 |
change = diff.compare(k) |
| 437 |
if change is None: |
| 438 |
continue |
| 439 |
else: |
| 440 |
if isinstance(change, dict): |
| 441 |
changed.update(change) |
| 442 |
else: |
| 443 |
changed[k] = change |
| 444 |
if changed: |
| 445 |
self.changes = UsableChanges(params=changed) |
| 446 |
return True |
| 447 |
return False |
| 448 |
|
| 449 |
def should_update(self): |
| 450 |
result = self._update_changed_options() |
| 451 |
if result: |
| 452 |
return True |
| 453 |
return False |
| 454 |
|
| 455 |
def exec_module(self): |
| 456 |
start = datetime.now().isoformat() |
| 457 |
version = tmos_version(self.client) |
| 458 |
result = dict() |
| 459 |
changed = self.present() |
| 460 |
|
| 461 |
reportable = ReportableChanges(params=self.changes.to_return()) |
| 462 |
changes = reportable.to_return() |
| 463 |
result.update(**changes) |
| 464 |
result.update(dict(changed=changed)) |
| 465 |
send_teem(start, self.client, self.module, version) |
| 466 |
return result |
| 467 |
|
| 468 |
def present(self): |
| 469 |
result = dict(changed=False) |
| 470 |
config = None |
| 471 |
contents = None |
| 472 |
|
| 473 |
if self.want.backup or (self.module._diff and self.want.diff_against == 'running'): |
| 474 |
contents = self.read_current_from_device() |
| 475 |
config = ImishConfig(indent=1, contents=contents) |
| 476 |
if self.want.backup: |
| 477 |
|
| 478 |
|
| 479 |
result['__backup__'] = contents |
| 480 |
|
| 481 |
if any((self.want.src, self.want.lines)): |
| 482 |
match = self.want.match |
| 483 |
replace = self.want.replace |
| 484 |
|
| 485 |
candidate = self.get_candidate() |
| 486 |
running = self.get_running_config(contents) |
| 487 |
|
| 488 |
response = self.get_diff( |
| 489 |
candidate=candidate, |
| 490 |
running=running, |
| 491 |
diff_match=match, |
| 492 |
diff_ignore_lines=self.want.diff_ignore_lines, |
| 493 |
path=self.want.parents, |
| 494 |
diff_replace=replace |
| 495 |
) |
| 496 |
|
| 497 |
config_diff = response['config_diff'] |
| 498 |
|
| 499 |
if config_diff: |
| 500 |
commands = config_diff.split('\n') |
| 501 |
|
| 502 |
if self.want.before: |
| 503 |
commands[:0] = self.want.before |
| 504 |
|
| 505 |
if self.want.after: |
| 506 |
commands.extend(self.want.after) |
| 507 |
|
| 508 |
result['commands'] = commands |
| 509 |
result['updates'] = commands |
| 510 |
|
| 511 |
if not self.module.check_mode: |
| 512 |
self.load_config(commands) |
| 513 |
|
| 514 |
result['changed'] = True |
| 515 |
|
| 516 |
running_config = self.want.running_config |
| 517 |
startup_config = None |
| 518 |
|
| 519 |
if self.want.save_when == 'always': |
| 520 |
self.save_config(result) |
| 521 |
elif self.want.save_when == 'modified': |
| 522 |
output = self.execute_show_commands(['show running-config', 'show startup-config']) |
| 523 |
|
| 524 |
running_config = ImishConfig(indent=1, contents=output[0], ignore_lines=self.want.diff_ignore_lines) |
| 525 |
startup_config = ImishConfig(indent=1, contents=output[1], ignore_lines=self.want.diff_ignore_lines) |
| 526 |
|
| 527 |
if running_config.sha1 != startup_config.sha1: |
| 528 |
self.save_config(result) |
| 529 |
elif self.want.save_when == 'changed' and result['changed']: |
| 530 |
self.save_on_device() |
| 531 |
|
| 532 |
if self.module._diff: |
| 533 |
if not running_config: |
| 534 |
output = self.execute_show_commands('show running-config') |
| 535 |
contents = output[0] |
| 536 |
else: |
| 537 |
contents = running_config |
| 538 |
|
| 539 |
|
| 540 |
running_config = ImishConfig(indent=1, contents=contents, ignore_lines=self.want.diff_ignore_lines) |
| 541 |
|
| 542 |
if self.want.diff_against == 'running': |
| 543 |
if self.module.check_mode: |
| 544 |
self.module.warn("unable to perform diff against running-config due to check mode") |
| 545 |
contents = None |
| 546 |
else: |
| 547 |
contents = config.config_text |
| 548 |
|
| 549 |
elif self.want.diff_against == 'startup': |
| 550 |
if not startup_config: |
| 551 |
output = self.execute_show_commands('show startup-config') |
| 552 |
contents = output[0] |
| 553 |
else: |
| 554 |
contents = startup_config.config_text |
| 555 |
|
| 556 |
elif self.want.diff_against == 'intended': |
| 557 |
contents = self.want.intended_config |
| 558 |
|
| 559 |
if contents is not None: |
| 560 |
base_config = ImishConfig(indent=1, contents=contents, ignore_lines=self.want.diff_ignore_lines) |
| 561 |
|
| 562 |
if running_config.sha1 != base_config.sha1: |
| 563 |
if self.want.diff_against == 'intended': |
| 564 |
before = running_config |
| 565 |
after = base_config |
| 566 |
elif self.want.diff_against in ('startup', 'running'): |
| 567 |
before = base_config |
| 568 |
after = running_config |
| 569 |
|
| 570 |
result.update({ |
| 571 |
'changed': True, |
| 572 |
'diff': {'before': str(before), 'after': str(after)} |
| 573 |
}) |
| 574 |
self.changes.update(result) |
| 575 |
return result['changed'] |
| 576 |
|
| 577 |
def load_config(self, commands): |
| 578 |
|
| 579 |
commands = ['{0} '.format(x) for x in commands] |
| 580 |
content = StringIO("\n".join(commands)) |
| 581 |
|
| 582 |
file = tempfile.NamedTemporaryFile() |
| 583 |
name = os.path.basename(file.name) |
| 584 |
|
| 585 |
self.upload_file_to_device(content, name) |
| 586 |
self.load_config_on_device(name) |
| 587 |
self.remove_uploaded_file_from_device(name) |
| 588 |
|
| 589 |
def remove_uploaded_file_from_device(self, name): |
| 590 |
filepath = '/var/config/rest/downloads/{0}'.format(name) |
| 591 |
params = { |
| 592 |
"command": "run", |
| 593 |
"utilCmdArgs": filepath |
| 594 |
} |
| 595 |
uri = "https://{0}:{1}/mgmt/tm/util/unix-rm".format( |
| 596 |
self.client.provider['server'], |
| 597 |
self.client.provider['server_port'] |
| 598 |
) |
| 599 |
resp = self.client.api.post(uri, json=params) |
| 600 |
try: |
| 601 |
response = resp.json() |
| 602 |
except ValueError as ex: |
| 603 |
raise F5ModuleError(str(ex)) |
| 604 |
|
| 605 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 606 |
return True |
| 607 |
raise F5ModuleError(resp.content) |
| 608 |
|
| 609 |
def upload_file_to_device(self, content, name): |
| 610 |
url = 'https://{0}:{1}/mgmt/shared/file-transfer/uploads'.format( |
| 611 |
self.client.provider['server'], |
| 612 |
self.client.provider['server_port'] |
| 613 |
) |
| 614 |
try: |
| 615 |
upload_file(self.client, url, content, name) |
| 616 |
except F5ModuleError: |
| 617 |
raise F5ModuleError( |
| 618 |
"Failed to upload the file." |
| 619 |
) |
| 620 |
|
| 621 |
def load_config_on_device(self, name): |
| 622 |
filepath = '/var/config/rest/downloads/{0}'.format(name) |
| 623 |
command = 'imish -r {0} -f {1}'.format(self.want.route_domain, filepath) |
| 624 |
|
| 625 |
params = { |
| 626 |
"command": "run", |
| 627 |
"utilCmdArgs": '-c "{0}"'.format(command) |
| 628 |
} |
| 629 |
uri = "https://{0}:{1}/mgmt/tm/util/bash".format( |
| 630 |
self.client.provider['server'], |
| 631 |
self.client.provider['server_port'] |
| 632 |
) |
| 633 |
|
| 634 |
x = 0 |
| 635 |
while x < 3: |
| 636 |
resp = self.client.api.post(uri, json=params) |
| 637 |
try: |
| 638 |
response = resp.json() |
| 639 |
except ValueError as ex: |
| 640 |
raise F5ModuleError(str(ex)) |
| 641 |
|
| 642 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 643 |
if 'commandResult' in response: |
| 644 |
if 'Dynamic routing is not enabled' in response['commandResult']: |
| 645 |
raise F5ModuleError(response['commandResult']) |
| 646 |
if 'Protocol daemon is not running' in response['commandResult']: |
| 647 |
x += 1 |
| 648 |
time.sleep(5) |
| 649 |
continue |
| 650 |
return True |
| 651 |
|
| 652 |
raise F5ModuleError(resp.content) |
| 653 |
|
| 654 |
def read_current_from_device(self): |
| 655 |
command = 'imish -r {0} -e \\\"show running-config\\\"'.format(self.want.route_domain) |
| 656 |
|
| 657 |
params = { |
| 658 |
"command": "run", |
| 659 |
"utilCmdArgs": '-c "{0}"'.format(command) |
| 660 |
} |
| 661 |
uri = "https://{0}:{1}/mgmt/tm/util/bash".format( |
| 662 |
self.client.provider['server'], |
| 663 |
self.client.provider['server_port'] |
| 664 |
) |
| 665 |
resp = self.client.api.post(uri, json=params) |
| 666 |
try: |
| 667 |
response = resp.json() |
| 668 |
except ValueError as ex: |
| 669 |
raise F5ModuleError(str(ex)) |
| 670 |
|
| 671 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 672 |
if 'commandResult' in response: |
| 673 |
if 'Dynamic routing is not enabled' in response['commandResult']: |
| 674 |
raise F5ModuleError(response['commandResult']) |
| 675 |
return response['commandResult'] |
| 676 |
raise F5ModuleError(resp.content) |
| 677 |
|
| 678 |
def save_on_device(self): |
| 679 |
command = 'imish -r {0} -e write'.format(self.want.route_domain) |
| 680 |
params = { |
| 681 |
"command": "run", |
| 682 |
"utilCmdArgs": '-c "{0}"'.format(command) |
| 683 |
} |
| 684 |
uri = "https://{0}:{1}/mgmt/tm/util/bash".format( |
| 685 |
self.client.provider['server'], |
| 686 |
self.client.provider['server_port'] |
| 687 |
) |
| 688 |
resp = self.client.api.post(uri, json=params) |
| 689 |
try: |
| 690 |
response = resp.json() |
| 691 |
except ValueError as ex: |
| 692 |
raise F5ModuleError(str(ex)) |
| 693 |
|
| 694 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 695 |
return True |
| 696 |
raise F5ModuleError(resp.content) |
| 697 |
|
| 698 |
def get_diff(self, candidate=None, running=None, diff_match='line', |
| 699 |
diff_ignore_lines=None, path=None, diff_replace='line'): |
| 700 |
diff = {} |
| 701 |
|
| 702 |
|
| 703 |
candidate_obj = ImishConfig(indent=1) |
| 704 |
candidate_obj.load(candidate) |
| 705 |
|
| 706 |
if running and diff_match != 'none' and diff_replace != 'config': |
| 707 |
|
| 708 |
running_obj = ImishConfig(indent=1, contents=running, ignore_lines=diff_ignore_lines) |
| 709 |
configdiffobjs = candidate_obj.difference(running_obj, path=path, match=diff_match, replace=diff_replace) |
| 710 |
else: |
| 711 |
configdiffobjs = candidate_obj.items |
| 712 |
|
| 713 |
diff['config_diff'] = dumps(configdiffobjs, 'commands') if configdiffobjs else '' |
| 714 |
return diff |
| 715 |
|
| 716 |
def get_running_config(self, config=None): |
| 717 |
contents = self.want.running_config |
| 718 |
if not contents: |
| 719 |
if config: |
| 720 |
contents = config |
| 721 |
else: |
| 722 |
contents = self.read_current_from_device() |
| 723 |
return contents |
| 724 |
|
| 725 |
def get_candidate(self): |
| 726 |
candidate = '' |
| 727 |
if self.want.src: |
| 728 |
candidate = self.want.src |
| 729 |
|
| 730 |
elif self.want.lines: |
| 731 |
candidate_obj = ImishConfig(indent=1) |
| 732 |
parents = self.want.parents or list() |
| 733 |
if self.want.allow_duplicates: |
| 734 |
candidate_obj.add(self.want.lines, parents=parents, duplicates=True) |
| 735 |
else: |
| 736 |
candidate_obj.add(self.want.lines, parents=parents) |
| 737 |
candidate = dumps(candidate_obj, 'raw') |
| 738 |
return candidate |
| 739 |
|
| 740 |
def execute_show_commands(self, commands): |
| 741 |
body = [] |
| 742 |
|
| 743 |
uri = "https://{0}:{1}/mgmt/tm/util/bash".format( |
| 744 |
self.client.provider['server'], |
| 745 |
self.client.provider['server_port'] |
| 746 |
) |
| 747 |
for command in to_list(commands): |
| 748 |
command = 'imish -r {0} -e \\\"{1}\\\"'.format(self.want.route_domain, command) |
| 749 |
params = { |
| 750 |
"command": "run", |
| 751 |
"utilCmdArgs": '-c "{0}"'.format(command) |
| 752 |
} |
| 753 |
resp = self.client.api.post(uri, json=params) |
| 754 |
try: |
| 755 |
response = resp.json() |
| 756 |
except ValueError as ex: |
| 757 |
raise F5ModuleError(str(ex)) |
| 758 |
|
| 759 |
if resp.status not in [200, 201] or 'code' in response and response['code'] not in [200, 201]: |
| 760 |
raise F5ModuleError(resp.content) |
| 761 |
|
| 762 |
if 'commandResult' in response: |
| 763 |
if 'Dynamic routing is not enabled' in response['commandResult']: |
| 764 |
raise F5ModuleError(response['commandResult']) |
| 765 |
body.append(response['commandResult']) |
| 766 |
|
| 767 |
return body |
| 768 |
|
| 769 |
def save_config(self, result): |
| 770 |
result['changed'] = True |
| 771 |
if self.module.check_mode: |
| 772 |
self.module.warn( |
| 773 |
'Skipping command `copy running-config startup-config` ' |
| 774 |
'due to check_mode. Configuration not copied to ' |
| 775 |
'non-volatile storage' |
| 776 |
) |
| 777 |
return |
| 778 |
self.save_on_device() |
| 779 |
|
| 780 |
|
| 781 |
class ArgumentSpec(object): |
| 782 |
def __init__(self): |
| 783 |
self.supports_check_mode = True |
| 784 |
backup_spec = dict( |
| 785 |
filename=dict(), |
| 786 |
dir_path=dict(type='path') |
| 787 |
) |
| 788 |
argument_spec = dict( |
| 789 |
route_domain=dict(default="0"), |
| 790 |
src=dict(type='path'), |
| 791 |
lines=dict( |
| 792 |
type='list', |
| 793 |
elements='str', |
| 794 |
aliases=['commands'], |
| 795 |
), |
| 796 |
parents=dict( |
| 797 |
type='list', |
| 798 |
elements='str', |
| 799 |
), |
| 800 |
before=dict( |
| 801 |
type='list', |
| 802 |
elements='str', |
| 803 |
), |
| 804 |
after=dict( |
| 805 |
type='list', |
| 806 |
elements='str', |
| 807 |
), |
| 808 |
match=dict(default='line', choices=['line', 'strict', 'exact', 'none']), |
| 809 |
replace=dict(default='line', choices=['line', 'block']), |
| 810 |
running_config=dict(aliases=['config']), |
| 811 |
intended_config=dict(), |
| 812 |
backup=dict(type='bool', default='no'), |
| 813 |
backup_options=dict(type='dict', options=backup_spec), |
| 814 |
save_when=dict(choices=['always', 'never', 'modified', 'changed'], default='never'), |
| 815 |
diff_against=dict(choices=['running', 'startup', 'intended'], default='startup'), |
| 816 |
diff_ignore_lines=dict( |
| 817 |
type='list', |
| 818 |
elements='str', |
| 819 |
), |
| 820 |
allow_duplicates=dict(type='bool', default='no') |
| 821 |
) |
| 822 |
self.argument_spec = {} |
| 823 |
self.argument_spec.update(f5_argument_spec) |
| 824 |
self.argument_spec.update(argument_spec) |
| 825 |
self.mutually_exclusive = [ |
| 826 |
('lines', 'src'), |
| 827 |
('parents', 'src'), |
| 828 |
] |
| 829 |
self.required_if = [ |
| 830 |
('match', 'strict', ['lines']), |
| 831 |
('match', 'exact', ['lines']), |
| 832 |
('replace', 'block', ['lines']), |
| 833 |
('diff_against', 'intended', ['intended_config']) |
| 834 |
] |
| 835 |
self.add_file_common_args = True |
| 836 |
|
| 837 |
|
| 838 |
def main(): |
| 839 |
spec = ArgumentSpec() |
| 840 |
|
| 841 |
module = AnsibleModule( |
| 842 |
argument_spec=spec.argument_spec, |
| 843 |
supports_check_mode=spec.supports_check_mode, |
| 844 |
mutually_exclusive=spec.mutually_exclusive, |
| 845 |
required_if=spec.required_if, |
| 846 |
add_file_common_args=spec.add_file_common_args, |
| 847 |
) |
| 848 |
|
| 849 |
try: |
| 850 |
mm = ModuleManager(module=module) |
| 851 |
results = mm.exec_module() |
| 852 |
module.exit_json(**results) |
| 853 |
except F5ModuleError as ex: |
| 854 |
module.fail_json(msg=str(ex)) |
| 855 |
|
| 856 |
|
| 857 |
if __name__ == '__main__': |
| 858 |
main() |