Ansible save registered json variable to json file

Viewed 593

I have json output which i save in a registered variable output_bgp_raw. The original output (before i save it in variable) looks like this:

{
    "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"
}

I want to save the json output in a json file with the same state (without adding or subtracting). I'm using tasks with the copy module and filtering to_nice_json. The tasks looks like this :

  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"

However, the text in the resulting json file becomes single-line json and is parsed differently (there are " at the beginning and end, and an unreadable /n as enter). The json file can be seen below :

"{\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"

I want the resulting json file to be the exact same as the actual output. Can you please suggest what changes are needed?

1 Answers

Your output is a string representation of a json data (transformed automatically to json output by jinja2 templating when you debug it.... that's an other story).

You need to parse that string as json into a variable to then serialize it as nice json into your file.

(Note: do yourself a favor, use delegate_to: localhost rather than the old and not really readable 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
Related