|
71 | 71 | type: int |
72 | 72 | convert_values_to_str: |
73 | 73 | description: |
74 | | - - When O(convert_values_to_str=true), this attribute converts input object values to strings. |
75 | | - - This ensures compatibility with Jinja2 version 3.1.6 and above. |
| 74 | + - This parameter enforces the conversion of integer and float values to strings in Ansible Core v2.19.0 and later, as well as Jinja2 v3.1.6 and later. |
| 75 | + - This parameter defaults to O(convert_values_to_str=true) to maintain compatibility with Ansible Core versions earlier than 2.19.0. |
76 | 76 | - To disable this conversion, set O(convert_values_to_str=false). |
77 | 77 | type: bool |
78 | 78 | default: true |
| 79 | + aliases: [ convert_none_int_float_to_str, values_to_str ] |
79 | 80 | extends_documentation_fragment: |
80 | 81 | - cisco.aci.aci |
81 | 82 | - cisco.aci.annotation |
|
314 | 315 | HAS_YAML = False |
315 | 316 |
|
316 | 317 | from ansible.module_utils.basic import AnsibleModule |
317 | | -from ansible_collections.cisco.aci.plugins.module_utils.aci import ACIModule, aci_argument_spec, aci_annotation_spec |
| 318 | +from ansible_collections.cisco.aci.plugins.module_utils.aci import ( |
| 319 | + ACIModule, |
| 320 | + aci_argument_spec, |
| 321 | + aci_annotation_spec, |
| 322 | + recursive_convert_none_int_float_to_str_in_dict_list, |
| 323 | +) |
318 | 324 | from ansible.module_utils._text import to_text |
319 | 325 | from ansible_collections.cisco.aci.plugins.module_utils.annotation_unsupported import ( |
320 | 326 | ANNOTATION_UNSUPPORTED, |
@@ -405,7 +411,9 @@ def main(): |
405 | 411 | rsp_subtree_preserve=dict(type="bool", default=False), |
406 | 412 | page_size=dict(type="int"), |
407 | 413 | page=dict(type="int"), |
408 | | - convert_values_to_str=dict(type="bool", default=True), # To support Jinja2 3.1.6 and later versions. |
| 414 | + # To support Ansible Core 2.19.0 and later, Jinja2 3.1.6 and later versions. |
| 415 | + # The convert_values_to_str parameter defaults to True to maintain compatibility with Ansible Core versions earlier than 2.19.0. |
| 416 | + convert_values_to_str=dict(type="bool", default=True, aliases=["convert_none_int_float_to_str", "values_to_str"]), |
409 | 417 | ) |
410 | 418 |
|
411 | 419 | module = AnsibleModule( |
@@ -460,13 +468,21 @@ def main(): |
460 | 468 | if content and isinstance(content, dict): |
461 | 469 | # Validate inline YAML/JSON |
462 | 470 | add_annotation(annotation, payload) |
463 | | - payload = json.dumps(process_rest_payload(payload, convert_values_to_str)) |
| 471 | + if convert_values_to_str: |
| 472 | + payload = json.dumps(recursive_convert_none_int_float_to_str_in_dict_list(payload)) |
| 473 | + else: |
| 474 | + payload = json.dumps(payload) |
| 475 | + |
464 | 476 | elif payload and isinstance(payload, str) and HAS_YAML: |
465 | 477 | try: |
466 | 478 | # Validate YAML/JSON string |
467 | 479 | payload = yaml.safe_load(payload) |
468 | 480 | add_annotation(annotation, payload) |
469 | | - payload = json.dumps(process_rest_payload(payload, convert_values_to_str)) |
| 481 | + if convert_values_to_str: |
| 482 | + payload = json.dumps(recursive_convert_none_int_float_to_str_in_dict_list(payload)) |
| 483 | + else: |
| 484 | + payload = json.dumps(payload) |
| 485 | + |
470 | 486 | except Exception as e: |
471 | 487 | module.fail_json(msg="Failed to parse provided JSON/YAML payload: {0}".format(to_text(e)), exception=to_text(e), payload=payload) |
472 | 488 | elif rest_type == "xml" and HAS_LXML_ETREE: |
@@ -546,27 +562,5 @@ def main(): |
546 | 562 | aci.exit_json(**aci.result) |
547 | 563 |
|
548 | 564 |
|
549 | | -def process_rest_payload(payload, convert_values_to_str): |
550 | | - if convert_values_to_str: |
551 | | - if isinstance(payload, str): |
552 | | - payload = convert_payload_values_to_str(json.loads(payload)) |
553 | | - else: |
554 | | - payload = convert_payload_values_to_str(payload) |
555 | | - return payload |
556 | | - |
557 | | - |
558 | | -def convert_payload_values_to_str(data): |
559 | | - if data is None: |
560 | | - return "" |
561 | | - if isinstance(data, dict): |
562 | | - return {k: convert_payload_values_to_str(v) for k, v in data.items()} |
563 | | - elif isinstance(data, list): |
564 | | - return [convert_payload_values_to_str(item) for item in data] |
565 | | - elif (isinstance(data, int) and not isinstance(data, bool)) or isinstance(data, float): |
566 | | - return str(data) |
567 | | - else: |
568 | | - return data |
569 | | - |
570 | | - |
571 | 565 | if __name__ == "__main__": |
572 | 566 | main() |
0 commit comments