mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Modules don't have to return JSON, key=value pairs is ok.
This commit is contained in:
parent
40fd778e2c
commit
4bde4926c3
4 changed files with 29 additions and 21 deletions
|
@ -18,11 +18,6 @@
|
||||||
|
|
||||||
################################################
|
################################################
|
||||||
|
|
||||||
try:
|
|
||||||
import json
|
|
||||||
except ImportError:
|
|
||||||
import simplejson as json
|
|
||||||
|
|
||||||
import fnmatch
|
import fnmatch
|
||||||
import multiprocessing
|
import multiprocessing
|
||||||
import signal
|
import signal
|
||||||
|
@ -209,7 +204,7 @@ class Runner(object):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# try to parse the JSON response
|
# try to parse the JSON response
|
||||||
return [ host, True, json.loads(result) ]
|
return [ host, True, parse_json(result) ]
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
# it failed, say so, but return the string anyway
|
# it failed, say so, but return the string anyway
|
||||||
return [ host, False, "%s/%s" % (str(e), result) ]
|
return [ host, False, "%s/%s" % (str(e), result) ]
|
||||||
|
@ -324,7 +319,7 @@ class Runner(object):
|
||||||
if self.module_name == 'setup':
|
if self.module_name == 'setup':
|
||||||
host = conn.host
|
host = conn.host
|
||||||
try:
|
try:
|
||||||
var_result = json.loads(result)
|
var_result = parse_json(result)
|
||||||
except:
|
except:
|
||||||
var_result = {}
|
var_result = {}
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,9 @@
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
import shlex
|
||||||
|
from ansible.errors import *
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import json
|
import json
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
@ -183,4 +186,26 @@ def async_poll_status(jid, host, clock, result):
|
||||||
else:
|
else:
|
||||||
return "<job %s> polling on %s, %s remaining" % (jid, host, clock)
|
return "<job %s> polling on %s, %s remaining" % (jid, host, clock)
|
||||||
|
|
||||||
|
def parse_json(data):
|
||||||
|
try:
|
||||||
|
return json.loads(data)
|
||||||
|
except:
|
||||||
|
# not JSON, but try "Baby JSON" which allows many of our modules to not
|
||||||
|
# require JSON and makes writing modules in bash much simpler
|
||||||
|
results = {}
|
||||||
|
tokens = shlex.split(data)
|
||||||
|
for t in tokens:
|
||||||
|
if t.find("=") == -1:
|
||||||
|
raise AnsibleException("failed to parse: %s" % data)
|
||||||
|
(key,value) = t.split("=", 1)
|
||||||
|
if key == 'changed' or 'failed':
|
||||||
|
if value.lower() in [ 'true', '1' ] :
|
||||||
|
value = True
|
||||||
|
elif value.lower() in [ 'false', '0' ]:
|
||||||
|
value = False
|
||||||
|
if key == 'rc':
|
||||||
|
value = int(value)
|
||||||
|
results[key] = value
|
||||||
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
|
15
library/copy
15
library/copy
|
@ -22,11 +22,6 @@ import sys
|
||||||
import os
|
import os
|
||||||
import shlex
|
import shlex
|
||||||
|
|
||||||
try:
|
|
||||||
import json
|
|
||||||
except ImportError:
|
|
||||||
import simplejson as json
|
|
||||||
|
|
||||||
# ===========================================
|
# ===========================================
|
||||||
# convert arguments of form a=b c=d
|
# convert arguments of form a=b c=d
|
||||||
# to a dictionary
|
# to a dictionary
|
||||||
|
@ -51,10 +46,7 @@ dest = params['dest']
|
||||||
|
|
||||||
# raise an error if there is no src file
|
# raise an error if there is no src file
|
||||||
if not os.path.exists(src):
|
if not os.path.exists(src):
|
||||||
print json.dumps({
|
print "failed=1 msg='Source %s failed to transfer'" % src
|
||||||
"failed" : 1,
|
|
||||||
"msg" : "Source %s failed to transfer" % src
|
|
||||||
})
|
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
md5sum = None
|
md5sum = None
|
||||||
|
@ -70,9 +62,6 @@ if md5sum != md5sum2:
|
||||||
changed = True
|
changed = True
|
||||||
|
|
||||||
# mission accomplished
|
# mission accomplished
|
||||||
print json.dumps({
|
print "md5sum=%s changed=%s" % (md5sum2, changed)
|
||||||
"md5sum" : md5sum2,
|
|
||||||
"changed" : changed
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -148,7 +148,6 @@ class TestRunner(unittest.TestCase):
|
||||||
result = self._run('shell', [ "/bin/echo", "$HOME" ])
|
result = self._run('shell', [ "/bin/echo", "$HOME" ])
|
||||||
assert 'failed' not in result
|
assert 'failed' not in result
|
||||||
assert result['rc'] == 0
|
assert result['rc'] == 0
|
||||||
raise Exception(result['stdout'])
|
|
||||||
|
|
||||||
|
|
||||||
def test_setup(self):
|
def test_setup(self):
|
||||||
|
|
Loading…
Add table
Reference in a new issue