From e05c8aa5e3a73f88e0a5e563480cd14439a07296 Mon Sep 17 00:00:00 2001 From: chatton Date: Fri, 7 Jul 2023 14:41:24 +0100 Subject: [PATCH] wip --- plugins/modules/portainer_stack.py | 35 ++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/plugins/modules/portainer_stack.py b/plugins/modules/portainer_stack.py index dc9a8a8..631d2b5 100644 --- a/plugins/modules/portainer_stack.py +++ b/plugins/modules/portainer_stack.py @@ -16,6 +16,8 @@ except ImportError: _query_params_to_string, ) +import yaml + DOCUMENTATION = r""" --- module: portainer_stack @@ -127,7 +129,6 @@ def _update_stack(client, module, stack_id): }, ) - def handle_state_present(client, module): result = dict(changed=False, stack_name=module.params["stack_name"]) @@ -135,8 +136,7 @@ def handle_state_present(client, module): stacks = client.get("stacks") result["stacks"] = stacks - with open(module.params["docker_compose_file_path"]) as f: - file_contents = f.read() + contents = _get_stack_contents(module.params) target_stack_name = module.params["stack_name"] for stack in stacks: @@ -146,7 +146,7 @@ def handle_state_present(client, module): break if not already_exists: - stack = _create_stack(client, module, file_contents) + stack = _create_stack(client, module, contents) result["changed"] = True result["stack_id"] = stack["Id"] module.exit_json(**result) @@ -158,7 +158,7 @@ def handle_state_present(client, module): ) result["are_equal"] = ( - current_file_contents_resp["StackFileContent"] == file_contents + current_file_contents_resp["StackFileContent"] == contents ) if result["are_equal"]: module.exit_json(**result) @@ -191,13 +191,22 @@ def handle_state_absent(client, module): ) result["changed"] = True module.exit_json(**result) +def _get_stack_contents(params): + if params.get("docker_compose_file_path"): + with open(params["docker_compose_file_path"]) as f: + return f.read() + + if params.get("definition"): + return yaml.dump(yaml.safe_load(params["definition"]), default_flow_style=False) + raise ValueError("No docker_compose_file_path or definition provided.") def run_module(): # define available arguments/parameters a user can pass to the module module_args = dict( stack_name=dict(type="str", required=True), docker_compose_file_path=dict(type="str"), + definition=dict(type=dict), username=dict(type="str", default="admin"), password=dict(type="str", required=True, no_log=True), endpoint_id=dict(type="int", required=True), @@ -205,11 +214,6 @@ def run_module(): state=dict(type="str", default="present", choices=["present", "absent"]), ) - required_if = [ - # docker compose file is only required if we are ensuring the stack is present. - ["state", "present", ("docker_compose_file_path",)], - ] - state_fns = {"present": handle_state_present, "absent": handle_state_absent} # the AnsibleModule object will be our abstraction working with Ansible @@ -218,7 +222,16 @@ def run_module(): # supports check mode module = AnsibleModule( argument_spec=module_args, - required_if=required_if, + required_if = [ + # docker compose file is only required if we are ensuring the stack is present. + ["state", "present", ("docker_compose_file_path",)], + ], + mutually_exclusive=[ + ('docker_compose_file_path', 'definition'), + ], + required_one_of=[ + ('docker_compose_file_path', 'definition'), + ], # TODO: support check mode supports_check_mode=False, )