Question

I'm writing an plugin to return log results as a json file for ansible-playbook.

I'm not super familiar with python, but I've hacked something together that seems to work:

def json_log(res, host):
    if type(res) == type(dict()):
        if 'verbose_override' not in res:
          host_json = JSONEncoder().encode({'host':host})
          result_json = JSONEncoder().encode(res)
          combined_json = host_json + result_json
          combined_json = combined_json.replace("}{", ',')
          print(combined_json)

host_json would be something like: {"host": "centos65"}

result_json would be something like: {"cmd": "echo \"Hello World\" ", "end": "2014-08-01 19:32:38.714584", "stdout": "Hello World", "changed": true, "start": "2014-08-01 19:32:38.707510", "delta": "0:00:00.007074", "stderr": "", "rc": 0, "invocation": {"module_name": "shell", "module_args": "echo \"Hello World\""}}

So I've gone for the brute-force route and just combined the strings and removed the }{ it gets where they join, so it will be in the format I want as valid json:

{"host": "centos65","cmd": "echo \"Hello World\" ", "end": "2014-08-01 19:32:38.714584", "stdout": "Hello World", "changed": true, "start": "2014-08-01 19:32:38.707510", "delta": "0:00:00.007074", "stderr": "", "rc": 0, "invocation": {"module_name": "shell", "module_args": "echo \"Hello World\""}}

So right now I'm just mashing together the two strings and then replacing the join with a comma, is there a smarter way to combine them with the host part being at the start of the json?

Was it helpful?

Solution

res = {"cmd": "echo \"Hello World\" ", "end": "2014-08-01 19:32:38.714584", "stdout": "Hello World", "changed": True, "start": "2014-08-01 19:32:38.707510", "delta": "0:00:00.007074", "stderr": "", "rc": 0, "invocation": {"module_name": "shell", "module_args": "echo \"Hello World\""}}

def json_log(res, host):
    if isinstance(res,dict) and 'verbose_override' not in res:  
           res.update({"host": host})        
           combined_json  = JSONEncoder().encode(res)
           print(combined_json)

In [73]: json_log(res,"centos")
{"cmd": "echo \"Hello World\" ", "end": "2014-08-01 19:32:38.714584", "stdout": "Hello World", "changed": true, "rc": 0, "start": "2014-08-01 19:32:38.707510", "host": "centos", "stderr": "", "delta": "0:00:00.007074", "invocation": {"module_name": "shell", "module_args": "echo \"Hello World\""}}

You can update a dict with the contents of another, the only issue you would ever have is if you had duplicate keys and did not want the values overwritten.

OTHER TIPS

Since they are both dictionaries, you can update one of the two dictionaries with the other one. Ex:

>>> a = {"host": "centos65"}

>>> b = {"cmd": "echo \"Hello World\" ", "end": "2014-08-01 19:32:38.714584", "stdout": "Hello World", "changed": True, "start": "2014-08-01 19:32:38.707510", "delta": "0:00:00.007074", "stderr": "", "rc": 0, "invocation": {"module_name": "shell", "module_args": "echo \"Hello World\""}}

>>> a.update(b)
>>> a
{'cmd': 'echo "Hello World" ', 'end': '2014-08-01 19:32:38.714584', 'stdout': 'Hello World', 'changed': True, 'rc': 0, 'start': '2014-08-01 19:32:38.707510', 'host': 'centos65', 'stderr': '', 'delta': '0:00:00.007074', 'invocation': {'module_name': 'shell', 'module_args': 'echo "Hello World"'}}
>>> a["host"]
'centos65'
>>> a["cmd"]
'echo "Hello World" '
>>> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top