| 1 |
|
| 2 |
from __future__ import absolute_import, division, print_function |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
__metaclass__ = type |
| 13 |
|
| 14 |
ANSIBLE_METADATA = { |
| 15 |
"status": ["preview"], |
| 16 |
"supported_by": "community", |
| 17 |
"metadata_version": "1.1", |
| 18 |
} |
| 19 |
|
| 20 |
DOCUMENTATION = """ |
| 21 |
--- |
| 22 |
module: fortios_wanopt_settings |
| 23 |
short_description: Configure WAN optimization settings in Fortinet's FortiOS and FortiGate. |
| 24 |
description: |
| 25 |
- This module is able to configure a FortiGate or FortiOS (FOS) device by allowing the |
| 26 |
user to set and modify wanopt feature and settings category. |
| 27 |
Examples include all parameters and values need to be adjusted to datasources before usage. |
| 28 |
Tested with FOS v6.0.0 |
| 29 |
version_added: "2.0.0" |
| 30 |
author: |
| 31 |
- Link Zheng (@chillancezen) |
| 32 |
- Jie Xue (@JieX19) |
| 33 |
- Hongbin Lu (@fgtdev-hblu) |
| 34 |
- Frank Shen (@frankshen01) |
| 35 |
- Miguel Angel Munoz (@mamunozgonzalez) |
| 36 |
- Nicolas Thomas (@thomnico) |
| 37 |
notes: |
| 38 |
- Legacy fortiosapi has been deprecated, httpapi is the preferred way to run playbooks |
| 39 |
|
| 40 |
- The module supports check_mode. |
| 41 |
|
| 42 |
requirements: |
| 43 |
- ansible>=2.16 |
| 44 |
options: |
| 45 |
access_token: |
| 46 |
description: |
| 47 |
- Token-based authentication. |
| 48 |
Generated from GUI of Fortigate. |
| 49 |
type: str |
| 50 |
required: false |
| 51 |
enable_log: |
| 52 |
description: |
| 53 |
- Enable/Disable logging for task. |
| 54 |
type: bool |
| 55 |
required: false |
| 56 |
default: false |
| 57 |
vdom: |
| 58 |
description: |
| 59 |
- Virtual domain, among those defined previously. A vdom is a |
| 60 |
virtual instance of the FortiGate that can be configured and |
| 61 |
used as a different unit. |
| 62 |
type: str |
| 63 |
default: root |
| 64 |
member_path: |
| 65 |
type: str |
| 66 |
description: |
| 67 |
- Member attribute path to operate on. |
| 68 |
- Delimited by a slash character if there are more than one attribute. |
| 69 |
- Parameter marked with member_path is legitimate for doing member operation. |
| 70 |
member_state: |
| 71 |
type: str |
| 72 |
description: |
| 73 |
- Add or delete a member under specified attribute path. |
| 74 |
- When member_state is specified, the state option is ignored. |
| 75 |
choices: |
| 76 |
- 'present' |
| 77 |
- 'absent' |
| 78 |
|
| 79 |
wanopt_settings: |
| 80 |
description: |
| 81 |
- Configure WAN optimization settings. |
| 82 |
default: null |
| 83 |
type: dict |
| 84 |
suboptions: |
| 85 |
auto_detect_algorithm: |
| 86 |
description: |
| 87 |
- Auto detection algorithms used in tunnel negotiations. |
| 88 |
type: str |
| 89 |
choices: |
| 90 |
- 'simple' |
| 91 |
- 'diff-req-resp' |
| 92 |
host_id: |
| 93 |
description: |
| 94 |
- Local host ID (must also be entered in the remote FortiGate"s peer list). |
| 95 |
type: str |
| 96 |
tunnel_optimization: |
| 97 |
description: |
| 98 |
- WANOpt tunnel optimization option. |
| 99 |
type: str |
| 100 |
choices: |
| 101 |
- 'memory-usage' |
| 102 |
- 'balanced' |
| 103 |
- 'throughput' |
| 104 |
tunnel_ssl_algorithm: |
| 105 |
description: |
| 106 |
- Relative strength of encryption algorithms accepted during tunnel negotiation. |
| 107 |
type: str |
| 108 |
choices: |
| 109 |
- 'high' |
| 110 |
- 'medium' |
| 111 |
- 'low' |
| 112 |
""" |
| 113 |
|
| 114 |
EXAMPLES = """ |
| 115 |
- name: Configure WAN optimization settings. |
| 116 |
fortinet.fortios.fortios_wanopt_settings: |
| 117 |
vdom: "{{ vdom }}" |
| 118 |
wanopt_settings: |
| 119 |
auto_detect_algorithm: "simple" |
| 120 |
host_id: "myhostname" |
| 121 |
tunnel_optimization: "memory-usage" |
| 122 |
tunnel_ssl_algorithm: "high" |
| 123 |
""" |
| 124 |
|
| 125 |
RETURN = """ |
| 126 |
build: |
| 127 |
description: Build number of the fortigate image |
| 128 |
returned: always |
| 129 |
type: str |
| 130 |
sample: '1547' |
| 131 |
http_method: |
| 132 |
description: Last method used to provision the content into FortiGate |
| 133 |
returned: always |
| 134 |
type: str |
| 135 |
sample: 'PUT' |
| 136 |
http_status: |
| 137 |
description: Last result given by FortiGate on last operation applied |
| 138 |
returned: always |
| 139 |
type: str |
| 140 |
sample: "200" |
| 141 |
mkey: |
| 142 |
description: Master key (id) used in the last call to FortiGate |
| 143 |
returned: success |
| 144 |
type: str |
| 145 |
sample: "id" |
| 146 |
name: |
| 147 |
description: Name of the table used to fulfill the request |
| 148 |
returned: always |
| 149 |
type: str |
| 150 |
sample: "urlfilter" |
| 151 |
path: |
| 152 |
description: Path of the table used to fulfill the request |
| 153 |
returned: always |
| 154 |
type: str |
| 155 |
sample: "webfilter" |
| 156 |
revision: |
| 157 |
description: Internal revision number |
| 158 |
returned: always |
| 159 |
type: str |
| 160 |
sample: "17.0.2.10658" |
| 161 |
serial: |
| 162 |
description: Serial number of the unit |
| 163 |
returned: always |
| 164 |
type: str |
| 165 |
sample: "FGVMEVYYQT3AB5352" |
| 166 |
status: |
| 167 |
description: Indication of the operation's result |
| 168 |
returned: always |
| 169 |
type: str |
| 170 |
sample: "success" |
| 171 |
vdom: |
| 172 |
description: Virtual domain used |
| 173 |
returned: always |
| 174 |
type: str |
| 175 |
sample: "root" |
| 176 |
version: |
| 177 |
description: Version of the FortiGate |
| 178 |
returned: always |
| 179 |
type: str |
| 180 |
sample: "v5.6.3" |
| 181 |
""" |
| 182 |
from ansible.module_utils.basic import AnsibleModule |
| 183 |
from ansible.module_utils.connection import Connection |
| 184 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import ( |
| 185 |
FortiOSHandler, |
| 186 |
) |
| 187 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import ( |
| 188 |
check_legacy_fortiosapi, |
| 189 |
) |
| 190 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import ( |
| 191 |
schema_to_module_spec, |
| 192 |
) |
| 193 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.fortios import ( |
| 194 |
check_schema_versioning, |
| 195 |
) |
| 196 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortimanager.common import ( |
| 197 |
FAIL_SOCKET_MSG, |
| 198 |
) |
| 199 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.data_post_processor import ( |
| 200 |
remove_invalid_fields, |
| 201 |
) |
| 202 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import ( |
| 203 |
is_same_comparison, |
| 204 |
) |
| 205 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import ( |
| 206 |
serialize, |
| 207 |
) |
| 208 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import ( |
| 209 |
find_current_values, |
| 210 |
) |
| 211 |
from ansible_collections.fortinet.fortios.plugins.module_utils.fortios.comparison import ( |
| 212 |
unify_data_format, |
| 213 |
) |
| 214 |
|
| 215 |
|
| 216 |
def filter_wanopt_settings_data(json): |
| 217 |
option_list = [ |
| 218 |
"auto_detect_algorithm", |
| 219 |
"host_id", |
| 220 |
"tunnel_optimization", |
| 221 |
"tunnel_ssl_algorithm", |
| 222 |
] |
| 223 |
|
| 224 |
json = remove_invalid_fields(json) |
| 225 |
dictionary = {} |
| 226 |
|
| 227 |
for attribute in option_list: |
| 228 |
if attribute in json and json[attribute] is not None: |
| 229 |
dictionary[attribute] = json[attribute] |
| 230 |
|
| 231 |
return dictionary |
| 232 |
|
| 233 |
|
| 234 |
def underscore_to_hyphen(data): |
| 235 |
new_data = None |
| 236 |
if isinstance(data, list): |
| 237 |
new_data = [] |
| 238 |
for i, elem in enumerate(data): |
| 239 |
new_data.append(underscore_to_hyphen(elem)) |
| 240 |
elif isinstance(data, dict): |
| 241 |
new_data = {} |
| 242 |
for k, v in data.items(): |
| 243 |
new_data[k.replace("_", "-")] = underscore_to_hyphen(v) |
| 244 |
else: |
| 245 |
return data |
| 246 |
return new_data |
| 247 |
|
| 248 |
|
| 249 |
def wanopt_settings(data, fos, check_mode=False): |
| 250 |
|
| 251 |
state = None |
| 252 |
vdom = data["vdom"] |
| 253 |
parameters = None |
| 254 |
state = data.get("state", None) |
| 255 |
wanopt_settings_data = data["wanopt_settings"] |
| 256 |
|
| 257 |
filtered_data = filter_wanopt_settings_data(wanopt_settings_data) |
| 258 |
converted_data = underscore_to_hyphen(filtered_data) |
| 259 |
|
| 260 |
|
| 261 |
if check_mode: |
| 262 |
diff = { |
| 263 |
"before": "", |
| 264 |
"after": filtered_data, |
| 265 |
} |
| 266 |
mkeyname = fos.get_mkeyname(None, None) |
| 267 |
mkey = fos.get_mkey("wanopt", "settings", filtered_data, vdom=vdom) |
| 268 |
current_data = fos.get( |
| 269 |
"wanopt", "settings", vdom=vdom, mkey=mkey, parameters=parameters |
| 270 |
) |
| 271 |
is_existed = ( |
| 272 |
current_data |
| 273 |
and current_data.get("http_status") == 200 |
| 274 |
and ( |
| 275 |
mkeyname |
| 276 |
and isinstance(current_data.get("results"), list) |
| 277 |
and len(current_data["results"]) > 0 |
| 278 |
or not mkeyname |
| 279 |
and current_data["results"] |
| 280 |
) |
| 281 |
) |
| 282 |
|
| 283 |
|
| 284 |
if state == "present" or state is True or state is None: |
| 285 |
|
| 286 |
if mkeyname is not None and mkey is None: |
| 287 |
return False, True, filtered_data, diff |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
copied_filtered_data = filtered_data.copy() |
| 292 |
copied_filtered_data.pop(mkeyname, None) |
| 293 |
unified_filtered_data = unify_data_format(copied_filtered_data) |
| 294 |
|
| 295 |
current_data_results = current_data.get("results", {}) |
| 296 |
current_config = ( |
| 297 |
current_data_results[0] |
| 298 |
if mkeyname |
| 299 |
and isinstance(current_data_results, list) |
| 300 |
and len(current_data_results) > 0 |
| 301 |
else current_data_results |
| 302 |
) |
| 303 |
if is_existed: |
| 304 |
unified_current_values = find_current_values( |
| 305 |
unified_filtered_data, |
| 306 |
unify_data_format(current_config), |
| 307 |
) |
| 308 |
|
| 309 |
is_same = is_same_comparison( |
| 310 |
serialize(unified_current_values), serialize(unified_filtered_data) |
| 311 |
) |
| 312 |
|
| 313 |
return ( |
| 314 |
False, |
| 315 |
not is_same, |
| 316 |
filtered_data, |
| 317 |
{"before": unified_current_values, "after": unified_filtered_data}, |
| 318 |
) |
| 319 |
|
| 320 |
|
| 321 |
return False, True, filtered_data, diff |
| 322 |
|
| 323 |
if state == "absent": |
| 324 |
if mkey is None: |
| 325 |
return ( |
| 326 |
False, |
| 327 |
False, |
| 328 |
filtered_data, |
| 329 |
{"before": current_data["results"][0], "after": ""}, |
| 330 |
) |
| 331 |
|
| 332 |
if is_existed: |
| 333 |
return ( |
| 334 |
False, |
| 335 |
True, |
| 336 |
filtered_data, |
| 337 |
{"before": current_data["results"][0], "after": ""}, |
| 338 |
) |
| 339 |
return False, False, filtered_data, {} |
| 340 |
|
| 341 |
return True, False, {"reason: ": "Must provide state parameter"}, {} |
| 342 |
|
| 343 |
|
| 344 |
data_copy = data.copy() |
| 345 |
data_copy["wanopt_settings"] = filtered_data |
| 346 |
fos.do_member_operation( |
| 347 |
"wanopt", |
| 348 |
"settings", |
| 349 |
data_copy, |
| 350 |
) |
| 351 |
|
| 352 |
return fos.set( |
| 353 |
"wanopt", "settings", data=converted_data, vdom=vdom, parameters=parameters |
| 354 |
) |
| 355 |
|
| 356 |
|
| 357 |
def is_successful_status(resp): |
| 358 |
return ( |
| 359 |
"status" in resp |
| 360 |
and resp["status"] == "success" |
| 361 |
or "http_status" in resp |
| 362 |
and resp["http_status"] == 200 |
| 363 |
or "http_method" in resp |
| 364 |
and resp["http_method"] == "DELETE" |
| 365 |
and resp["http_status"] == 404 |
| 366 |
) |
| 367 |
|
| 368 |
|
| 369 |
def fortios_wanopt(data, fos, check_mode): |
| 370 |
|
| 371 |
if data["wanopt_settings"]: |
| 372 |
resp = wanopt_settings(data, fos, check_mode) |
| 373 |
else: |
| 374 |
fos._module.fail_json(msg="missing task body: %s" % ("wanopt_settings")) |
| 375 |
if isinstance(resp, tuple) and len(resp) == 4: |
| 376 |
return resp |
| 377 |
return ( |
| 378 |
not is_successful_status(resp), |
| 379 |
is_successful_status(resp) |
| 380 |
and (resp["revision_changed"] if "revision_changed" in resp else True), |
| 381 |
resp, |
| 382 |
{}, |
| 383 |
) |
| 384 |
|
| 385 |
|
| 386 |
versioned_schema = { |
| 387 |
"v_range": [["v6.0.0", ""]], |
| 388 |
"type": "dict", |
| 389 |
"children": { |
| 390 |
"host_id": {"v_range": [["v6.0.0", ""]], "type": "string"}, |
| 391 |
"tunnel_ssl_algorithm": { |
| 392 |
"v_range": [["v6.0.0", ""]], |
| 393 |
"type": "string", |
| 394 |
"options": [{"value": "high"}, {"value": "medium"}, {"value": "low"}], |
| 395 |
}, |
| 396 |
"auto_detect_algorithm": { |
| 397 |
"v_range": [["v6.0.0", ""]], |
| 398 |
"type": "string", |
| 399 |
"options": [{"value": "simple"}, {"value": "diff-req-resp"}], |
| 400 |
}, |
| 401 |
"tunnel_optimization": { |
| 402 |
"v_range": [["v7.0.6", "v7.0.12"], ["v7.2.1", ""]], |
| 403 |
"type": "string", |
| 404 |
"options": [ |
| 405 |
{"value": "memory-usage"}, |
| 406 |
{"value": "balanced"}, |
| 407 |
{"value": "throughput"}, |
| 408 |
], |
| 409 |
}, |
| 410 |
}, |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
def main(): |
| 415 |
module_spec = schema_to_module_spec(versioned_schema) |
| 416 |
mkeyname = None |
| 417 |
fields = { |
| 418 |
"access_token": {"required": False, "type": "str", "no_log": True}, |
| 419 |
"enable_log": {"required": False, "type": "bool", "default": False}, |
| 420 |
"vdom": {"required": False, "type": "str", "default": "root"}, |
| 421 |
"member_path": {"required": False, "type": "str"}, |
| 422 |
"member_state": { |
| 423 |
"type": "str", |
| 424 |
"required": False, |
| 425 |
"choices": ["present", "absent"], |
| 426 |
}, |
| 427 |
"wanopt_settings": { |
| 428 |
"required": False, |
| 429 |
"type": "dict", |
| 430 |
"default": None, |
| 431 |
"options": {}, |
| 432 |
}, |
| 433 |
} |
| 434 |
for attribute_name in module_spec["options"]: |
| 435 |
fields["wanopt_settings"]["options"][attribute_name] = module_spec["options"][ |
| 436 |
attribute_name |
| 437 |
] |
| 438 |
if mkeyname and mkeyname == attribute_name: |
| 439 |
fields["wanopt_settings"]["options"][attribute_name]["required"] = True |
| 440 |
|
| 441 |
module = AnsibleModule(argument_spec=fields, supports_check_mode=True) |
| 442 |
check_legacy_fortiosapi(module) |
| 443 |
|
| 444 |
is_error = False |
| 445 |
has_changed = False |
| 446 |
result = None |
| 447 |
diff = None |
| 448 |
|
| 449 |
versions_check_result = None |
| 450 |
if module._socket_path: |
| 451 |
connection = Connection(module._socket_path) |
| 452 |
if "access_token" in module.params: |
| 453 |
connection.set_custom_option("access_token", module.params["access_token"]) |
| 454 |
|
| 455 |
if "enable_log" in module.params: |
| 456 |
connection.set_custom_option("enable_log", module.params["enable_log"]) |
| 457 |
else: |
| 458 |
connection.set_custom_option("enable_log", False) |
| 459 |
fos = FortiOSHandler(connection, module, mkeyname, admin_passwd_header=False) |
| 460 |
versions_check_result = check_schema_versioning( |
| 461 |
fos, versioned_schema, "wanopt_settings" |
| 462 |
) |
| 463 |
|
| 464 |
is_error, has_changed, result, diff = fortios_wanopt( |
| 465 |
module.params, fos, module.check_mode |
| 466 |
) |
| 467 |
|
| 468 |
else: |
| 469 |
module.fail_json(**FAIL_SOCKET_MSG) |
| 470 |
|
| 471 |
if versions_check_result and versions_check_result["matched"] is False: |
| 472 |
module.warn( |
| 473 |
"Ansible has detected version mismatch between FortOS system and your playbook, see more details by specifying option -vvv" |
| 474 |
) |
| 475 |
|
| 476 |
if not is_error: |
| 477 |
if versions_check_result and versions_check_result["matched"] is False: |
| 478 |
module.exit_json( |
| 479 |
changed=has_changed, |
| 480 |
version_check_warning=versions_check_result, |
| 481 |
meta=result, |
| 482 |
diff=diff, |
| 483 |
) |
| 484 |
else: |
| 485 |
module.exit_json(changed=has_changed, meta=result, diff=diff) |
| 486 |
else: |
| 487 |
if versions_check_result and versions_check_result["matched"] is False: |
| 488 |
module.fail_json( |
| 489 |
msg="Error in repo", |
| 490 |
version_check_warning=versions_check_result, |
| 491 |
meta=result, |
| 492 |
) |
| 493 |
else: |
| 494 |
module.fail_json(msg="Error in repo", meta=result) |
| 495 |
|
| 496 |
|
| 497 |
if __name__ == "__main__": |
| 498 |
main() |