From d40ba28fb43a15eac0c604647e6535fb8fba4939 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Sun, 8 Sep 2019 20:30:18 +0200 Subject: [PATCH] docker_container: improve port range parsing error behavior (#61740) * Improve port range parsing. * Add changelog. --- .../61740-docker_container-port-range-parsing.yml | 2 ++ lib/ansible/modules/cloud/docker/docker_container.py | 12 +++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/61740-docker_container-port-range-parsing.yml diff --git a/changelogs/fragments/61740-docker_container-port-range-parsing.yml b/changelogs/fragments/61740-docker_container-port-range-parsing.yml new file mode 100644 index 00000000000..817beec8bfa --- /dev/null +++ b/changelogs/fragments/61740-docker_container-port-range-parsing.yml @@ -0,0 +1,2 @@ +bugfixes: +- "docker_container - improve error behavior when parsing port ranges fails." diff --git a/lib/ansible/modules/cloud/docker/docker_container.py b/lib/ansible/modules/cloud/docker/docker_container.py index a6743a84235..4e150e11062 100644 --- a/lib/ansible/modules/cloud/docker/docker_container.py +++ b/lib/ansible/modules/cloud/docker/docker_container.py @@ -1078,12 +1078,18 @@ def parse_port_range(range_or_port, client): Returns a list of integers for each port in the list. ''' if '-' in range_or_port: - start, end = [int(port) for port in range_or_port.split('-')] + try: + start, end = [int(port) for port in range_or_port.split('-')] + except Exception: + client.fail('Invalid port range: "{0}"'.format(range_or_port)) if end < start: - client.fail('Invalid port range: {0}'.format(range_or_port)) + client.fail('Invalid port range: "{0}"'.format(range_or_port)) return list(range(start, end + 1)) else: - return [int(range_or_port)] + try: + return [int(range_or_port)] + except Exception: + client.fail('Invalid port: "{0}"'.format(range_or_port)) def split_colon_ipv6(text, client):