Ansible 将注册的 json 变量保存到 json 文件

Ansible save registered json variable to json file

我有 json 输出,我将其保存在注册变量 output_bgp_raw 中。原始输出(在我将其保存在变量中之前)如下所示:

{
    "as": 65011,
    "bestPath": {
        "multiPathRelax": "false"
    },
    "dynamicPeers": 0,
    "peerCount": 2,
    "peerGroupCount": 2,
    "peerGroupMemory": 128,
    "peerMemory": 42352,
    "peers": {
        "swp1": {
            "hostname": "Spine-01",
            "idType": "interface",
            "inq": 0,
            "msgRcvd": 140386,
            "msgSent": 140432,
            "outq": 0,
            "peerUptime": "4d17h35m",
            "peerUptimeEstablishedEpoch": 1643304458,
            "peerUptimeMsec": 408925000,
            "pfxRcd": 17,
            "pfxSnt": 27,
            "prefixReceivedCount": 17,
            "remoteAs": 65001,
            "state": "Established",
            "tableVersion": 0,
            "version": 4
        },
        "swp2": {
            "hostname": "Spine-02",
            "idType": "interface",
            "inq": 0,
            "msgRcvd": 140383,
            "msgSent": 140430,
            "outq": 0,
            "peerUptime": "4d17h35m",
            "peerUptimeEstablishedEpoch": 1643304466,
            "peerUptimeMsec": 408917000,
            "pfxRcd": 17,
            "pfxSnt": 27,
            "prefixReceivedCount": 17,
            "remoteAs": 65001,
            "state": "Established",
            "tableVersion": 0,
            "version": 4
        }
    },
    "ribCount": 19,
    "ribMemory": 3496,
    "routerId": "10.0.0.3",
    "tableVersion": 0,
    "totalPeers": 2,
    "vrfId": 0,
    "vrfName": "default"
}

我想将 json 输出保存在具有相同状态的 json 文件中(不加或减)。我正在使用复制模块和过滤任务 to_nice_json。任务看起来像这样:

  tasks:
  - name: VERIFICATION // BGP STATUS
    nclu:
      commands:
          - show bgp l2vpn evpn summary json
    register: output_bgp_raw

  - name: VERIFICATION // SAVE BGP VARIABLE TO FILE
    local_action:
      module: copy
      content: "{{ output_bgp_raw.msg | to_nice_json }}"
      dest: "containers/verification/{{ instance_name }}-bgp-raw.json"

但是,生成的 json 文件中的文本变为单行 json 并且解析方式不同(开头和结尾有 ",以及不可读的 /n 作为输入)。 json 文件如下所示:

"{\n  \"routerId\":\"10.0.0.3\",\n  \"as\":65011,\n  \"vrfId\":0,\n  \"vrfName\":\"default\",\n  \"tableVersion\":0,\n  \"ribCount\":19,\n  \"ribMemory\":3496,\n  \"peerCount\":2,\n  \"peerMemory\":42352,\n  \"peerGroupCount\":2,\n  \"peerGroupMemory\":128,\n  \"peers\":{\n    \"swp1\":{\n      \"hostname\":\"Spine-01\",\n      \"remoteAs\":65001,\n      \"version\":4,\n      \"msgRcvd\":141122,\n      \"msgSent\":141171,\n      \"tableVersion\":0,\n      \"outq\":0,\n      \"inq\":0,\n      \"peerUptime\":\"4d18h11m\",\n      \"peerUptimeMsec\":411069000,\n      \"peerUptimeEstablishedEpoch\":1643304458,\n      \"prefixReceivedCount\":17,\n      \"pfxRcd\":17,\n      \"pfxSnt\":27,\n      \"state\":\"Established\",\n      \"idType\":\"interface\"\n    },\n    \"swp2\":{\n      \"hostname\":\"Spine-02\",\n      \"remoteAs\":65001,\n      \"version\":4,\n      \"msgRcvd\":141118,\n      \"msgSent\":141169,\n      \"tableVersion\":0,\n      \"outq\":0,\n      \"inq\":0,\n      \"peerUptime\":\"4d18h11m\",\n      \"peerUptimeMsec\":411061000,\n      \"peerUptimeEstablishedEpoch\":1643304466,\n      \"prefixReceivedCount\":17,\n      \"pfxRcd\":17,\n      \"pfxSnt\":27,\n      \"state\":\"Established\",\n      \"idType\":\"interface\"\n    }\n  },\n  \"totalPeers\":2,\n  \"dynamicPeers\":0,\n  \"bestPath\":{\n    \"multiPathRelax\":\"false\"\n  }\n} \n"

我希望生成的 json 文件与实际输出完全相同。您能否建议需要进行哪些更改?

您的输出是 json 数据的字符串表示(当您调试它时,由 jinja2 模板自动转换为 json 输出....那是另一回事)。

您需要将该字符串作为 json 解析到一个变量中,然后将其作为好的 json 序列化到您的文件中。

(注意:帮自己一个忙,使用delegate_to: localhost而不是旧的且不太可读的local_action

  - name: VERIFICATION // SAVE BGP VARIABLE TO FILE
    copy:
      content: "{{ output_bgp_raw.msg | from_json | to_nice_json }}"
      dest: "containers/verification/{{ instance_name }}-bgp-raw.json"
    delegate_to: localhost