Skip to content

Commit

Permalink
docker_service: add an option to try to cast potential integers and f…
Browse files Browse the repository at this point in the history
…loats
  • Loading branch information
s-hertel committed Oct 30, 2017
1 parent 75a6f9d commit 559c498
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions lib/ansible/modules/cloud/docker/docker_service.py
Expand Up @@ -148,6 +148,12 @@
required: false
type: bool
default: false
numbers_in_definition:
description:
- If there are floats or integers in C(definition) and this option is set to True,
any strings that are integers or floats will be cast as such (with the exception of version).
type: bool
default: false
extends_documentation_fragment:
- docker
Expand Down Expand Up @@ -470,7 +476,7 @@
DEFAULT_TIMEOUT = 10

from ansible.module_utils.docker_common import AnsibleDockerClient, DockerBaseClass

from ansible.module_utils.six import string_types

AUTH_PARAM_MAPPING = {
u'docker_host': u'--host',
Expand Down Expand Up @@ -632,6 +638,8 @@ def __init__(self, client):
self.project_src = tempfile.mkdtemp(prefix="ansible")
compose_file = os.path.join(self.project_src, "docker-compose.yml")
try:
if client.module.params.get('numbers_in_definition'):
self.definition = self.convert_to_numbers(dict(self.definition))
self.log('writing: ')
self.log(yaml.dump(self.definition, default_flow_style=False))
with open(compose_file, 'w') as f:
Expand All @@ -648,6 +656,25 @@ def __init__(self, client):
except Exception as exc:
self.client.fail("Configuration error - %s" % str(exc))

def convert_to_numbers(self, definition):
for (key, value) in definition.items():
if key == "version":
pass
elif isinstance(value, string_types):
if '.' in value:
try:
definition[key] = float(value)
except ValueError:
pass
else:
try:
definition[key] = int(value)
except ValueError:
pass
elif isinstance(value, dict):
definition[key] = self.convert_to_numbers(dict(value))
return definition

def exec_module(self):
result = dict()

Expand Down Expand Up @@ -1044,7 +1071,8 @@ def main():
pull=dict(type='bool', default=False),
nocache=dict(type='bool', default=False),
debug=dict(type='bool', default=False),
timeout=dict(type='int', default=DEFAULT_TIMEOUT)
timeout=dict(type='int', default=DEFAULT_TIMEOUT),
numbers_in_definition=dict(type='bool', default=False),
)

mutually_exclusive = [
Expand Down

0 comments on commit 559c498

Please sign in to comment.