【问题标题】:Better way of merging two json strings in Python for ansible callback_plugin在 Python 中为 ansible callback_plugin 合并两个 json 字符串的更好方法
【发布时间】:2014-08-14 21:23:00
【问题描述】:

我正在编写一个插件以将日志结果作为 ansible-playbook 的 json 文件返回。

我对 python 不是很熟悉,但我已经一起破解了一些似乎可以工作的东西:

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 类似于:{"host": "centos65"}

result_json 类似于:{"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\""}}

所以我选择了蛮力路线,只是组合了字符串并删除了它们加入的}{,所以它将采用我想要的有效 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\""}}

所以现在我只是将两个字符串混合在一起,然后用逗号替换连接,有没有更聪明的方法将它们与 json 开头的主机部分结合起来?

【问题讨论】:

    标签: python json ansible ansible-playbook


    【解决方案1】:
    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\""}}
    

    你可以用另一个字典的内容更新一个字典,唯一的问题是你有重复的键并且不想覆盖值。

    【讨论】:

    • 我试过这个但得到:File "/Users/peterso/Projects/ansible-json/callback_plugins/json_logs.py", line 13, in json_log combined_json = result_json.update(host_json) AttributeError: 'str' object has no attribute 'update'
    【解决方案2】:

    由于它们都是字典,因此您可以将两个字典中的一个更新为另一个。例如:

    >>> 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" '
    >>> 
    

    【讨论】:

      猜你喜欢
      • 2020-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多