From 125dc6f5e208eccedba6e3b0aee40b8aaa70953c Mon Sep 17 00:00:00 2001 From: shreyash bhosale Date: Mon, 8 Jun 2026 16:50:31 +0530 Subject: [PATCH] move common argument specs to shared module_utils --- plugins/module_utils/_xml.py | 13 ++++++++++++ plugins/modules/xml.py | 38 +++++++++++++++--------------------- plugins/modules/xml_info.py | 17 ++++++---------- 3 files changed, 35 insertions(+), 33 deletions(-) diff --git a/plugins/module_utils/_xml.py b/plugins/module_utils/_xml.py index fc9b5103cc..de70b8cc72 100644 --- a/plugins/module_utils/_xml.py +++ b/plugins/module_utils/_xml.py @@ -29,6 +29,19 @@ with deps.declare("lxml"): from lxml import etree # type: ignore[no-redef] +COMMON_ARGUMENT_SPEC = dict( + path=dict(type="path", aliases=["dest", "file"]), + xmlstring=dict(type="str"), + xpath=dict(type="str"), + namespaces=dict(type="dict", default={}), + count=dict(type="bool", default=False), + print_match=dict(type="bool", default=False), + content=dict(type="str", choices=["attribute", "text"]), + strip_cdata_tags=dict(type="bool", default=False), + huge_tree=dict(type="bool", default=False), +) + + def check_lxml(module: AnsibleModule) -> None: deps.validate(module, "lxml") version_str = ".".join(str(f) for f in etree.LXML_VERSION) diff --git a/plugins/modules/xml.py b/plugins/modules/xml.py index 165a8c7f4a..04489745a4 100644 --- a/plugins/modules/xml.py +++ b/plugins/modules/xml.py @@ -323,6 +323,7 @@ from ansible.module_utils.basic import AnsibleModule, json_dict_bytes_to_unicode from ansible.module_utils.common.text.converters import to_bytes from ansible_collections.community.general.plugins.module_utils._xml import ( + COMMON_ARGUMENT_SPEC, check_lxml, collect_element_attr, collect_element_text, @@ -796,29 +797,22 @@ def finish(module, tree, xpath, namespaces, changed=False, msg="", hitcount=0, m def main(): + argument_spec = copy.deepcopy(COMMON_ARGUMENT_SPEC) + argument_spec.update( + state=dict(type="str", default="present", choices=["absent", "present"], aliases=["ensure"]), + value=dict(type="raw"), + attribute=dict(type="raw"), + add_children=dict(type="list", elements="raw"), + set_children=dict(type="list", elements="raw"), + pretty_print=dict(type="bool", default=False), + input_type=dict(type="str", default="yaml", choices=["xml", "yaml"]), + backup=dict(type="bool", default=False), + insertbefore=dict(type="bool", default=False), + insertafter=dict(type="bool", default=False), + create_if_missing=dict(type="bool", default=True), + ) module = AnsibleModule( - argument_spec=dict( - path=dict(type="path", aliases=["dest", "file"]), - xmlstring=dict(type="str"), - xpath=dict(type="str"), - namespaces=dict(type="dict", default={}), - state=dict(type="str", default="present", choices=["absent", "present"], aliases=["ensure"]), - value=dict(type="raw"), - attribute=dict(type="raw"), - add_children=dict(type="list", elements="raw"), - set_children=dict(type="list", elements="raw"), - count=dict(type="bool", default=False), - print_match=dict(type="bool", default=False), - pretty_print=dict(type="bool", default=False), - content=dict(type="str", choices=["attribute", "text"]), - input_type=dict(type="str", default="yaml", choices=["xml", "yaml"]), - backup=dict(type="bool", default=False), - strip_cdata_tags=dict(type="bool", default=False), - huge_tree=dict(type="bool", default=False), - insertbefore=dict(type="bool", default=False), - insertafter=dict(type="bool", default=False), - create_if_missing=dict(type="bool", default=True), - ), + argument_spec=argument_spec, supports_check_mode=True, required_by=dict( add_children=["xpath"], diff --git a/plugins/modules/xml_info.py b/plugins/modules/xml_info.py index 282cc8fb31..c5396ce1ac 100644 --- a/plugins/modules/xml_info.py +++ b/plugins/modules/xml_info.py @@ -135,9 +135,12 @@ matches: returned: when parameter O(print_match) or O(content) is set """ +import copy + from ansible.module_utils.basic import AnsibleModule from ansible_collections.community.general.plugins.module_utils._xml import ( + COMMON_ARGUMENT_SPEC, check_lxml, collect_element_attr, collect_element_text, @@ -149,18 +152,10 @@ from ansible_collections.community.general.plugins.module_utils._xml import ( def main(): + argument_spec = copy.deepcopy(COMMON_ARGUMENT_SPEC) + argument_spec["xpath"]["required"] = True module = AnsibleModule( - argument_spec=dict( - path=dict(type="path", aliases=["dest", "file"]), - xmlstring=dict(type="str"), - xpath=dict(type="str", required=True), - namespaces=dict(type="dict", default={}), - count=dict(type="bool", default=False), - print_match=dict(type="bool", default=False), - content=dict(type="str", choices=["attribute", "text"]), - strip_cdata_tags=dict(type="bool", default=False), - huge_tree=dict(type="bool", default=False), - ), + argument_spec=argument_spec, supports_check_mode=True, required_one_of=[ ["path", "xmlstring"],