jinja在json中的特定变量之后匹配字符串

时间:2019-01-10 06:01:15

标签: json ansible jinja2

我试图让jinja检查json文件,然后获取输出或执行某些操作,我可以做完全一样的操作,但是使用yaml格式,不知道json是怎么回事

{
  "failed": false,
  "changed": false,
  "stdout_lines": [
    {
      "vrfs": {
        "default": {
          "instList": {
            "1": {
              "ospfNeighborEntries": [
                {
                  "inactivity": 1547099204.137163,
                  "priority": 1,
                  "drState": "DR",
                  "routerId": "1.1.1.2",
                  "interfaceName": "Ethernet2",
                  "adjacencyState": "full",
                  "interfaceAddress": "1.1.1.2",
                  "options": {
                    "multitopologyCapability": false,
                    "doNotUseInRouteCalc": false,
                    "demandCircuitsSupport": false,
                    "nssaCapability": false,
                    "externalRoutingCapability": true,
                    "opaqueLsaSupport": false,
                    "linkLocalSignaling": false,
                    "multicastCapability": false
                  },
                  "details": {
                    "grLastRestartTime": null,
                    "backupDesignatedRouter": "1.1.1.1",
                    "areaId": "0.0.0.0",
                    "grHelperTimer": null,
                    "bfdRequestSent": false,
                    "numberOfStateChanges": 7,
                    "inactivityDefers": 0,
                    "bfdState": "adminDown",
                    "stateTime": 1547098942.137163,
                    "retransmissionCount": 0,
                    "designatedRouter": "1.1.1.2",
                    "grNumAttempts": 0
                  }
                }
              ]
            }
          }
        }
      }
    }
  ],
  "stdout": [
    {
      "vrfs": {
        "default": {
          "instList": {
            "1": {
              "ospfNeighborEntries": [
                {
                  "inactivity": 1547099204.137163,
                  "priority": 1,
                  "drState": "DR",
                  "routerId": "1.1.1.2",
                  "interfaceName": "Ethernet2",
                  "adjacencyState": "full",
                  "interfaceAddress": "1.1.1.2",
                  "options": {
                    "multitopologyCapability": false,
                    "doNotUseInRouteCalc": false,
                    "demandCircuitsSupport": false,
                    "nssaCapability": false,
                    "externalRoutingCapability": true,
                    "opaqueLsaSupport": false,
                    "linkLocalSignaling": false,
                    "multicastCapability": false
                  },
                  "details": {
                    "grLastRestartTime": null,
                    "backupDesignatedRouter": "1.1.1.1",
                    "areaId": "0.0.0.0",
                    "grHelperTimer": null,
                    "bfdRequestSent": false,
                    "numberOfStateChanges": 7,
                    "inactivityDefers": 0,
                    "bfdState": "adminDown",
                    "stateTime": 1547098942.137163,
                    "retransmissionCount": 0,
                    "designatedRouter": "1.1.1.2",
                    "grNumAttempts": 0
                  }
                }
              ]
            }
          }
        }
      }
    }
  ]
}

Jinja模板

InterfaceName:  "{{ ospfresult.stdout[0]['vrfs']['default']['instList']['1']['ospfNeighborEntries']['interfaceName'] }}"

预期结果

InterfaceName: Ethernet2

收到错误

FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'list object' has no attribute 'interfaceName'"}
        to retry, use: --limit @/etc/ansible/vxlan-arista/arista-test/testfacts.retry

请问我使用的是Ansible 2.5.4吗?

预先感谢

1 个答案:

答案 0 :(得分:0)

列表中的元素丢失

1 && 0 && false // 0
1 && 2 && 3     // 3