basic: fix ValueError if value of a type='int' is not an int

With this fix, we get a friendly error message:

    failed: [localhost] => {"failed": true}
    msg: value of argument start_port is not of type int and we were unable to automatically convert
pull/11032/head
Rene Moser 10 years ago
parent da6d15d1f9
commit 8f29ca23ae

@ -1015,57 +1015,60 @@ class AnsibleModule(object):
value = self.params[k] value = self.params[k]
is_invalid = False is_invalid = False
if wanted == 'str': try:
if not isinstance(value, basestring): if wanted == 'str':
self.params[k] = str(value) if not isinstance(value, basestring):
elif wanted == 'list': self.params[k] = str(value)
if not isinstance(value, list): elif wanted == 'list':
if isinstance(value, basestring): if not isinstance(value, list):
self.params[k] = value.split(",") if isinstance(value, basestring):
elif isinstance(value, int) or isinstance(value, float): self.params[k] = value.split(",")
self.params[k] = [ str(value) ] elif isinstance(value, int) or isinstance(value, float):
else: self.params[k] = [ str(value) ]
is_invalid = True
elif wanted == 'dict':
if not isinstance(value, dict):
if isinstance(value, basestring):
if value.startswith("{"):
try:
self.params[k] = json.loads(value)
except:
(result, exc) = self.safe_eval(value, dict(), include_exceptions=True)
if exc is not None:
self.fail_json(msg="unable to evaluate dictionary for %s" % k)
self.params[k] = result
elif '=' in value:
self.params[k] = dict([x.strip().split("=", 1) for x in value.split(",")])
else: else:
self.fail_json(msg="dictionary requested, could not parse JSON or key=value") is_invalid = True
else: elif wanted == 'dict':
is_invalid = True if not isinstance(value, dict):
elif wanted == 'bool': if isinstance(value, basestring):
if not isinstance(value, bool): if value.startswith("{"):
if isinstance(value, basestring): try:
self.params[k] = self.boolean(value) self.params[k] = json.loads(value)
else: except:
is_invalid = True (result, exc) = self.safe_eval(value, dict(), include_exceptions=True)
elif wanted == 'int': if exc is not None:
if not isinstance(value, int): self.fail_json(msg="unable to evaluate dictionary for %s" % k)
if isinstance(value, basestring): self.params[k] = result
self.params[k] = int(value) elif '=' in value:
else: self.params[k] = dict([x.strip().split("=", 1) for x in value.split(",")])
is_invalid = True else:
elif wanted == 'float': self.fail_json(msg="dictionary requested, could not parse JSON or key=value")
if not isinstance(value, float): else:
if isinstance(value, basestring): is_invalid = True
self.params[k] = float(value) elif wanted == 'bool':
else: if not isinstance(value, bool):
is_invalid = True if isinstance(value, basestring):
else: self.params[k] = self.boolean(value)
self.fail_json(msg="implementation error: unknown type %s requested for %s" % (wanted, k)) else:
is_invalid = True
elif wanted == 'int':
if not isinstance(value, int):
if isinstance(value, basestring):
self.params[k] = int(value)
else:
is_invalid = True
elif wanted == 'float':
if not isinstance(value, float):
if isinstance(value, basestring):
self.params[k] = float(value)
else:
is_invalid = True
else:
self.fail_json(msg="implementation error: unknown type %s requested for %s" % (wanted, k))
if is_invalid: if is_invalid:
self.fail_json(msg="argument %s is of invalid type: %s, required: %s" % (k, type(value), wanted)) self.fail_json(msg="argument %s is of invalid type: %s, required: %s" % (k, type(value), wanted))
except ValueError, e:
self.fail_json(msg="value of argument %s is not of type %s and we were unable to automatically convert" % (k, wanted))
def _set_defaults(self, pre=True): def _set_defaults(self, pre=True):
for (k,v) in self.argument_spec.iteritems(): for (k,v) in self.argument_spec.iteritems():

Loading…
Cancel
Save