You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于application_stop_time_output生成含item与整数型stdout的字典列表

需求说明

需要从给定的application_stop_time_output输出结果中创建一个字典列表,每个字典仅包含item和stdout字段,且stdout需为整数类型。

执行的Ansible代码

- debug:
     msg: "{{ application_stop_time_output }}"

实际输出结果

ok: [ebppdoxs10] => {
    "msg": {
        "changed": true, 
        "msg": "All items completed", 
        "results": [
            {
                "ansible_loop_var": "item", 
                "changed": true, 
                "cmd": "application_stop_time_format=$(echo \"[1/19/23 21:05:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war\"|awk '{print $1\" \"$2}'|cut -c 2-|rev | sed 's/:/ /' | rev| awk '{print $1\" \"$2}')\n\napplication_stop_time=$(date -d \"$application_stop_time_format\" +%s)\necho $application_stop_time\n", 
                "delta": "0:00:00.009048", 
                "end": "2023-01-20 23:26:19.995453", 
                "failed": false, 
                "invocation": {
                    "module_args": {
                        "_raw_params": "application_stop_time_format=$(echo \"[1/19/23 21:05:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war\"|awk '{print $1\" \"$2}'|cut -c 2-|rev | sed 's/:/ /' | rev| awk '{print $1\" \"$2}')\n\napplication_stop_time=$(date -d \"$application_stop_time_format\" +%s)\necho $application_stop_time\n", 
                        "_uses_shell": true, 
                        "argv": null, 
                        "chdir": null, 
                        "creates": null, 
                        "executable": null, 
                        "removes": null, 
                        "stdin": null, 
                        "stdin_add_newline": true, 
                        "strip_empty_ends": true, 
                        "warn": true
                    }
                }, 
                "item": "[1/19/23 21:05:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war", 
                "rc": 0, 
                "start": "2023-01-20 23:26:19.986405", 
                "stderr": "", 
                "stderr_lines": [], 
                "stdout": "1674142504", 
                "stdout_lines": [
                    "1674142504"
                ]
            }, 
            {
                "ansible_loop_var": "item", 
                "changed": true, 
                "cmd": "application_stop_time_format=$(echo \"[1/19/23 20:04:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war\"|awk '{print $1\" \"$2}'|cut -c 2-|rev | sed 's/:/ /' | rev| awk '{print $1\" \"$2}')\n\napplication_stop_time=$(date -d \"$application_stop_time_format\" +%s)\necho $application_stop_time\n", 
                "delta": "0:00:00.009918", 
                "end": "2023-01-20 23:26:23.887901", 
                "failed": false, 
                "invocation": {
                    "module_args": {
                        "_raw_params": "application_stop_time_format=$(echo \"[1/19/23 20:04:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war\"|awk '{print $1\" \"$2}'|cut -c 2-|rev | sed 's/:/ /' | rev| awk '{print $1\" \"$2}')\n\napplication_stop_time=$(date -d \"$application_stop_time_format\" +%s)\necho $application_stop_time\n", 
                        "_uses_shell": true, 
                        "argv": null, 
                        "chdir": null, 
                        "creates": null, 
                        "executable": null, 
                        "removes": null, 
                        "stdin": null, 
                        "stdin_add_newline": true, 
                        "strip_empty_ends": true, 
                        "warn": true
                    }
                }, 
                "item": "[1/19/23 20:04:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war", 
                "rc": 0, 
                "start": "2023-01-20 23:26:23.877983", 
                "stderr": "", 
                "stderr_lines": [], 
                "stdout": "1674138844", 
                "stdout_lines": [
                    "1674138844"
                ]
            }
        ]
    }
}

期望输出结果

ok: [ebppdoxs10] => {
    "msg": {
          "results": [
            {
               "item": "[1/19/23 21:05:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war", 
               "stdout": 1674142504, 
            }, 
            {
              "item": "[1/19/23 20:04:04:013 GMT+5:30] 0000 ApplicationMg A WSVR220I: Application stopped: ActivePackProdTest_war", 
              "stdout": 1674138844, 
           }
        ]
    }
}

实现代码

使用Ansible的过滤器组合即可完成需求,示例代码如下:

- debug:
    msg:
      results: "{{ application_stop_time_output.results | map('combine', {'stdout': item.stdout | int}) | map('pick', 'item', 'stdout') | list }}"

代码说明

  • map('pick', 'item', 'stdout'):从每个结果字典中仅提取item和stdout两个字段
  • map('combine', {'stdout': item.stdout | int}):将原字典中的字符串类型stdout转为整数后,合并回原字典

内容的提问来源于stack exchange,提问作者user10109375

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 22:01:03