| 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_config |
| 13 |
short_description: Manage BIG-IP configuration sections |
| 14 |
description: |
| 15 |
- Manages a BIG-IP configuration by allowing TMSH commands that |
| 16 |
modify the running configuration, or merge SCF formatted files into |
| 17 |
the running configuration. Additionally, this module is of |
| 18 |
significant importance because it allows you to save your running |
| 19 |
configuration to disk. Since all F5 modules manipulate the running |
| 20 |
configuration, it is important you use this module to save |
| 21 |
that running config. |
| 22 |
version_added: "1.0.0" |
| 23 |
options: |
| 24 |
save: |
| 25 |
description: |
| 26 |
- The C(save) argument instructs the module to save the |
| 27 |
running-config to startup-config. |
| 28 |
- This operation is performed after any changes are made to the |
| 29 |
current running config. If no changes are made, the configuration |
| 30 |
is still saved to the startup config. |
| 31 |
- This option always causes the module to return B(changed). |
| 32 |
type: bool |
| 33 |
default: true |
| 34 |
reset: |
| 35 |
description: |
| 36 |
- Loads the default configuration on the device. |
| 37 |
- If this option is specified, the default configuration is |
| 38 |
loaded before any commands or other provided configuration is run. |
| 39 |
type: bool |
| 40 |
default: false |
| 41 |
merge_content: |
| 42 |
description: |
| 43 |
- Loads the specified configuration that you want to merge into |
| 44 |
the running configuration. This is equivalent to using the |
| 45 |
C(tmsh) command C(load sys config from-terminal merge). |
| 46 |
- If you need to read the configuration from a file or template, use |
| 47 |
Ansible's C(file) or C(template) lookup plugins respectively. |
| 48 |
type: str |
| 49 |
verify: |
| 50 |
description: |
| 51 |
- Validates the specified configuration to see whether it is |
| 52 |
valid to replace the running configuration. |
| 53 |
- The running configuration will not be changed. |
| 54 |
- When this parameter is set to C(true), no change is reported |
| 55 |
by the module. |
| 56 |
type: bool |
| 57 |
default: false |
| 58 |
notes: |
| 59 |
- This module requires that sys db variable on device C(systemauth.disablebash) is set to C(false). |
| 60 |
extends_documentation_fragment: f5networks.f5_modules.f5 |
| 61 |
author: |
| 62 |
- Tim Rupp (@caphrim007) |
| 63 |
''' |
| 64 |
|
| 65 |
EXAMPLES = r''' |
| 66 |
- name: Save the running configuration of the BIG-IP |
| 67 |
bigip_config: |
| 68 |
save: true |
| 69 |
provider: |
| 70 |
server: lb.mydomain.com |
| 71 |
password: secret |
| 72 |
user: admin |
| 73 |
delegate_to: localhost |
| 74 |
|
| 75 |
- name: Reset the BIG-IP configuration, for example, to RMA the device |
| 76 |
bigip_config: |
| 77 |
reset: true |
| 78 |
save: true |
| 79 |
provider: |
| 80 |
server: lb.mydomain.com |
| 81 |
password: secret |
| 82 |
user: admin |
| 83 |
delegate_to: localhost |
| 84 |
|
| 85 |
- name: Load an SCF configuration |
| 86 |
bigip_config: |
| 87 |
merge_content: "{{ lookup('file', '/path/to/config.scf') }}" |
| 88 |
provider: |
| 89 |
server: lb.mydomain.com |
| 90 |
password: secret |
| 91 |
user: admin |
| 92 |
delegate_to: localhost |
| 93 |
''' |
| 94 |
|
| 95 |
RETURN = r''' |
| 96 |
stdout: |
| 97 |
description: The set of responses from the options. |
| 98 |
returned: always |
| 99 |
type: list |
| 100 |
sample: ['...', '...'] |
| 101 |
stdout_lines: |
| 102 |
description: The value of stdout split into a list. |
| 103 |
returned: always |
| 104 |
type: list |
| 105 |
sample: [['...', '...'], ['...'], ['...']] |
| 106 |
''' |
| 107 |
from datetime import datetime |
| 108 |
|
| 109 |
try: |
| 110 |
from StringIO import StringIO |
| 111 |
except ImportError: |
| 112 |
from io import StringIO |
| 113 |
|
| 114 |
import os |
| 115 |
import tempfile |
| 116 |
|
| 117 |
from ansible.module_utils.basic import AnsibleModule |
| 118 |
|
| 119 |
from ..module_utils.bigip import F5RestClient |
| 120 |
from ..module_utils.common import ( |
| 121 |
F5ModuleError, AnsibleF5Parameters, f5_argument_spec |
| 122 |
) |
| 123 |
from ..module_utils.icontrol import ( |
| 124 |
upload_file, tmos_version |
| 125 |
) |
| 126 |
from ..module_utils.teem import send_teem |
| 127 |
|
| 128 |
|
| 129 |
class Parameters(AnsibleF5Parameters): |
| 130 |
returnables = ['stdout', 'stdout_lines'] |
| 131 |
|
| 132 |
def to_return(self): |
| 133 |
result = {} |
| 134 |
for returnable in self.returnables: |
| 135 |
result[returnable] = getattr(self, returnable) |
| 136 |
result = self._filter_params(result) |
| 137 |
return result |
| 138 |
|
| 139 |
|
| 140 |
class ModuleManager(object): |
| 141 |
def __init__(self, *args, **kwargs): |
| 142 |
self.module = kwargs.get('module', None) |
| 143 |
self.client = F5RestClient(**self.module.params) |
| 144 |
self.want = Parameters(params=self.module.params) |
| 145 |
self.changes = Parameters() |
| 146 |
|
| 147 |
def _set_changed_options(self): |
| 148 |
changed = {} |
| 149 |
for key in Parameters.returnables: |
| 150 |
if getattr(self.want, key) is not None: |
| 151 |
changed[key] = getattr(self.want, key) |
| 152 |
if changed: |
| 153 |
self.changes = Parameters(params=changed) |
| 154 |
|
| 155 |
def _to_lines(self, stdout): |
| 156 |
lines = list() |
| 157 |
for item in stdout: |
| 158 |
if isinstance(item, str): |
| 159 |
item = str(item).split('\n') |
| 160 |
lines.append(item) |
| 161 |
return lines |
| 162 |
|
| 163 |
def exec_module(self): |
| 164 |
start = datetime.now().isoformat() |
| 165 |
version = tmos_version(self.client) |
| 166 |
result = {} |
| 167 |
|
| 168 |
changed = self.execute() |
| 169 |
|
| 170 |
result.update(**self.changes.to_return()) |
| 171 |
result.update(dict(changed=changed)) |
| 172 |
send_teem(start, self.client, self.module, version) |
| 173 |
return result |
| 174 |
|
| 175 |
def execute(self): |
| 176 |
responses = [] |
| 177 |
if self.want.reset: |
| 178 |
response = self.reset() |
| 179 |
responses.append(response) |
| 180 |
|
| 181 |
if self.want.merge_content: |
| 182 |
if self.want.verify: |
| 183 |
response = self.merge(verify=True) |
| 184 |
responses.append(response) |
| 185 |
else: |
| 186 |
response = self.merge(verify=False) |
| 187 |
responses.append(response) |
| 188 |
|
| 189 |
if self.want.save: |
| 190 |
response = self.save() |
| 191 |
responses.append(response) |
| 192 |
if not self.module.check_mode: |
| 193 |
self._detect_errors(responses) |
| 194 |
changes = { |
| 195 |
'stdout': responses, |
| 196 |
'stdout_lines': self._to_lines(responses) |
| 197 |
} |
| 198 |
self.changes = Parameters(params=changes) |
| 199 |
if self.want.verify: |
| 200 |
return False |
| 201 |
return True |
| 202 |
|
| 203 |
def _detect_errors(self, stdout): |
| 204 |
errors = [ |
| 205 |
'Unexpected Error:' |
| 206 |
] |
| 207 |
|
| 208 |
msg = [x for x in stdout for y in errors if y in x] |
| 209 |
if msg: |
| 210 |
|
| 211 |
raise F5ModuleError(' '.join(msg)) |
| 212 |
|
| 213 |
def reset(self): |
| 214 |
if self.module.check_mode: |
| 215 |
return True |
| 216 |
return self.reset_device() |
| 217 |
|
| 218 |
def reset_device(self): |
| 219 |
command = 'tmsh load sys config default' |
| 220 |
uri = "https://{0}:{1}/mgmt/tm/util/bash".format( |
| 221 |
self.client.provider['server'], |
| 222 |
self.client.provider['server_port'], |
| 223 |
) |
| 224 |
args = dict( |
| 225 |
command='run', |
| 226 |
utilCmdArgs='-c "{0}"'.format(command) |
| 227 |
) |
| 228 |
resp = self.client.api.post(uri, json=args) |
| 229 |
|
| 230 |
try: |
| 231 |
response = resp.json() |
| 232 |
except ValueError as ex: |
| 233 |
raise F5ModuleError(str(ex)) |
| 234 |
|
| 235 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 236 |
if 'commandResult' in response: |
| 237 |
return str(response['commandResult']) |
| 238 |
else: |
| 239 |
return |
| 240 |
raise F5ModuleError(resp.content) |
| 241 |
|
| 242 |
def merge(self, verify=True): |
| 243 |
temp_name = next(tempfile._get_candidate_names()) |
| 244 |
remote_path = "/var/config/rest/downloads/{0}".format(temp_name) |
| 245 |
temp_path = '/tmp/' + temp_name |
| 246 |
|
| 247 |
if self.module.check_mode: |
| 248 |
return True |
| 249 |
|
| 250 |
self.upload_to_device(temp_name) |
| 251 |
self.move_on_device(remote_path) |
| 252 |
response = self.merge_on_device( |
| 253 |
remote_path=temp_path, verify=verify |
| 254 |
) |
| 255 |
self.remove_temporary_file(remote_path=temp_path) |
| 256 |
return response |
| 257 |
|
| 258 |
def merge_on_device(self, remote_path, verify=True): |
| 259 |
command = 'tmsh load sys config file {0} merge'.format( |
| 260 |
remote_path |
| 261 |
) |
| 262 |
if verify: |
| 263 |
command += ' verify' |
| 264 |
|
| 265 |
uri = "https://{0}:{1}/mgmt/tm/util/bash".format( |
| 266 |
self.client.provider['server'], |
| 267 |
self.client.provider['server_port'], |
| 268 |
) |
| 269 |
args = dict( |
| 270 |
command='run', |
| 271 |
utilCmdArgs='-c "{0}"'.format(command) |
| 272 |
) |
| 273 |
resp = self.client.api.post(uri, json=args) |
| 274 |
|
| 275 |
try: |
| 276 |
response = resp.json() |
| 277 |
except ValueError as ex: |
| 278 |
raise F5ModuleError(str(ex)) |
| 279 |
|
| 280 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 281 |
if 'commandResult' in response: |
| 282 |
return str(response['commandResult']) |
| 283 |
else: |
| 284 |
return |
| 285 |
raise F5ModuleError(resp.content) |
| 286 |
|
| 287 |
def remove_temporary_file(self, remote_path): |
| 288 |
uri = "https://{0}:{1}/mgmt/tm/util/unix-rm".format( |
| 289 |
self.client.provider['server'], |
| 290 |
self.client.provider['server_port'], |
| 291 |
) |
| 292 |
args = dict( |
| 293 |
command='run', |
| 294 |
utilCmdArgs=remote_path |
| 295 |
) |
| 296 |
resp = self.client.api.post(uri, json=args) |
| 297 |
|
| 298 |
try: |
| 299 |
response = resp.json() |
| 300 |
except ValueError as ex: |
| 301 |
raise F5ModuleError(str(ex)) |
| 302 |
|
| 303 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 304 |
return True |
| 305 |
raise F5ModuleError(resp.content) |
| 306 |
|
| 307 |
def move_on_device(self, remote_path): |
| 308 |
uri = "https://{0}:{1}/mgmt/tm/util/unix-mv".format( |
| 309 |
self.client.provider['server'], |
| 310 |
self.client.provider['server_port'], |
| 311 |
) |
| 312 |
args = dict( |
| 313 |
command='run', |
| 314 |
utilCmdArgs='{0} /tmp/{1}'.format( |
| 315 |
remote_path, os.path.basename(remote_path) |
| 316 |
) |
| 317 |
) |
| 318 |
resp = self.client.api.post(uri, json=args) |
| 319 |
|
| 320 |
try: |
| 321 |
response = resp.json() |
| 322 |
except ValueError as ex: |
| 323 |
raise F5ModuleError(str(ex)) |
| 324 |
|
| 325 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 326 |
return True |
| 327 |
raise F5ModuleError(resp.content) |
| 328 |
|
| 329 |
def upload_to_device(self, temp_name): |
| 330 |
template = StringIO(self.want.merge_content) |
| 331 |
url = 'https://{0}:{1}/mgmt/shared/file-transfer/uploads'.format( |
| 332 |
self.client.provider['server'], |
| 333 |
self.client.provider['server_port'] |
| 334 |
) |
| 335 |
try: |
| 336 |
upload_file(self.client, url, template, temp_name) |
| 337 |
except F5ModuleError: |
| 338 |
raise F5ModuleError( |
| 339 |
"Failed to upload the file." |
| 340 |
) |
| 341 |
|
| 342 |
def save(self): |
| 343 |
if self.module.check_mode: |
| 344 |
return True |
| 345 |
return self.save_on_device() |
| 346 |
|
| 347 |
def save_on_device(self): |
| 348 |
command = 'tmsh save sys config' |
| 349 |
uri = "https://{0}:{1}/mgmt/tm/util/bash".format( |
| 350 |
self.client.provider['server'], |
| 351 |
self.client.provider['server_port'], |
| 352 |
) |
| 353 |
args = dict( |
| 354 |
command='run', |
| 355 |
utilCmdArgs='-c "{0}"'.format(command) |
| 356 |
) |
| 357 |
resp = self.client.api.post(uri, json=args) |
| 358 |
|
| 359 |
try: |
| 360 |
response = resp.json() |
| 361 |
except ValueError as ex: |
| 362 |
raise F5ModuleError(str(ex)) |
| 363 |
|
| 364 |
if resp.status in [200, 201] or 'code' in response and response['code'] in [200, 201]: |
| 365 |
if 'commandResult' in response: |
| 366 |
return str(response['commandResult']) |
| 367 |
else: |
| 368 |
return |
| 369 |
raise F5ModuleError(resp.content) |
| 370 |
|
| 371 |
|
| 372 |
class ArgumentSpec(object): |
| 373 |
def __init__(self): |
| 374 |
self.supports_check_mode = True |
| 375 |
argument_spec = dict( |
| 376 |
reset=dict( |
| 377 |
type='bool', |
| 378 |
default=False |
| 379 |
), |
| 380 |
merge_content=dict(), |
| 381 |
verify=dict( |
| 382 |
type='bool', |
| 383 |
default=False |
| 384 |
), |
| 385 |
save=dict( |
| 386 |
type='bool', |
| 387 |
default='yes' |
| 388 |
) |
| 389 |
) |
| 390 |
self.argument_spec = {} |
| 391 |
self.argument_spec.update(f5_argument_spec) |
| 392 |
self.argument_spec.update(argument_spec) |
| 393 |
|
| 394 |
|
| 395 |
def main(): |
| 396 |
spec = ArgumentSpec() |
| 397 |
|
| 398 |
module = AnsibleModule( |
| 399 |
argument_spec=spec.argument_spec, |
| 400 |
supports_check_mode=spec.supports_check_mode |
| 401 |
) |
| 402 |
|
| 403 |
try: |
| 404 |
mm = ModuleManager(module=module) |
| 405 |
results = mm.exec_module() |
| 406 |
module.exit_json(**results) |
| 407 |
except F5ModuleError as ex: |
| 408 |
module.fail_json(msg=str(ex)) |
| 409 |
|
| 410 |
|
| 411 |
if __name__ == '__main__': |
| 412 |
main() |