Change Docker Stack modules to use common CLI module framework. (#745)

This commit is contained in:
Felix Fontein
2024-01-14 08:54:06 +01:00
committed by GitHub
parent 5adac5216a
commit 1c8272f821
8 changed files with 249 additions and 161 deletions
+54 -29
View File
@@ -18,23 +18,50 @@ description:
- Retrieve information on docker stacks tasks using the C(docker stack) command
on the target node (see examples).
extends_documentation_fragment:
- community.docker.docker.cli_documentation
- community.docker.attributes
- community.docker.attributes.actiongroup_docker
- community.docker.attributes.info_module
attributes:
action_group:
version_added: 3.6.0
options:
name:
description:
- Stack name.
type: str
required: true
docker_cli:
version_added: 3.6.0
docker_host:
version_added: 3.6.0
tls_hostname:
version_added: 3.6.0
api_version:
version_added: 3.6.0
ca_path:
version_added: 3.6.0
client_cert:
version_added: 3.6.0
client_key:
version_added: 3.6.0
tls:
version_added: 3.6.0
validate_certs:
version_added: 3.6.0
cli_context:
version_added: 3.6.0
requirements:
- Docker CLI tool C(docker)
'''
RETURN = '''
results:
description: |
List of dictionaries containing the list of tasks associated
to a stack name.
sample: >
[{"CurrentState":"Running","DesiredState":"Running","Error":"","ID":"7wqv6m02ugkw","Image":"busybox","Name":"test_stack.1","Node":"swarm","Ports":""}]
description:
- List of dictionaries containing the list of tasks associated
to a stack name.
sample:
- {"CurrentState":"Running","DesiredState":"Running","Error":"","ID":"7wqv6m02ugkw","Image":"busybox","Name":"test_stack.1","Node":"swarm","Ports":""}
returned: always
type: list
elements: dict
@@ -52,7 +79,14 @@ EXAMPLES = '''
'''
import json
from ansible.module_utils.basic import AnsibleModule
import traceback
from ansible.module_utils.common.text.converters import to_native
from ansible_collections.community.docker.plugins.module_utils.common_cli import (
AnsibleModuleDockerClient,
DockerException,
)
def docker_stack_task(module, stack_name):
@@ -64,34 +98,25 @@ def docker_stack_task(module, stack_name):
def main():
module = AnsibleModule(
client = AnsibleModuleDockerClient(
argument_spec={
'name': dict(type='str', required=True)
},
supports_check_mode=True
supports_check_mode=True,
)
name = module.params['name']
rc, out, err = docker_stack_task(module, name)
if rc != 0:
module.fail_json(msg="Error running docker stack. {0}".format(err),
rc=rc, stdout=out, stderr=err)
else:
if out:
ret = list(
json.loads(outitem)
for outitem in out.splitlines())
else:
ret = []
module.exit_json(changed=False,
rc=rc,
stdout=out,
stderr=err,
results=ret)
try:
name = client.module.params['name']
rc, ret, stderr = client.call_cli_json_stream('stack', 'ps', name, '--format={{json .}}', check_rc=True)
client.module.exit_json(
changed=False,
rc=rc,
stdout='\n'.join([json.dumps(entry) for entry in ret]),
stderr=to_native(stderr).strip(),
results=ret,
)
except DockerException as e:
client.fail('An unexpected Docker error occurred: {0}'.format(to_native(e)), exception=traceback.format_exc())
if __name__ == "__main__":