繁体   English   中英

Ansible - 注册变量,然后将变量搜索到 set_fact (Cisco Aci)

[英]Ansible - Register variable and then search the variable to set_fact (Cisco Aci)

我查询 Cisco ACI 以获取特定 EPG 的高级 vmm 提供程序详细信息。 结果成功了。 然后我将结果注册到一个变量。 我尝试搜索该变量并获取\\提取单个特定信息,例如“dn”或“encap”,因为这将允许我在其他剧中使用这些信息。

不幸的是,我无法提取信息,因为结果以通常的格式返回。 查看对 register 变量的调试,它看起来是一个字典变量,但无论我尝试什么,我唯一能够访问的项目是“当前”项目。 所有其他项目均未注册为字典项目。

我试图将变量更改为列表,但仍然无法获得所需的信息。 我搜索了论坛,看看是否有一种方法可以将变量从 json 结果或字典变量转换为字符串,然后 grep 获取信息,但没有成功。 理想情况下,我想在不安装其他“应用程序”的情况下提取信息。

如果有人可以建议如何从不以正确的字典格式列出项目的不规则嵌套结果中搜索特定结果,我们将不胜感激。

- name: Access VMM provider Information
  hosts: apics
  gather_facts: false
  connection: local
#
     - set_fact:
        aci_result1: "{{ DVS_Result.current }}"

     - set_fact:
        aci_result2: "{{ DVS_Result.fvRsDomAtt.attributes.dn }}"
#
ok: [apic1r] => {
    "msg": {
        "changed": false,
        "current": [
            {
                "fvRsDomAtt": {
                    "attributes": {
                        "annotation": "",
                        "bindingType": "none",
                        "childAction": "",
                        "classPref": "encap",
                        "configIssues": "",
                        "delimiter": "",
                        "dn": "uni/tn-TN_prod/ap-AP_Test/epg-EPG_Test/rsdomAtt-[uni/vmmp-VMware/dom-DVS_Dell]",
                        "encap": "unknown",
                        "encapMode": "auto",
                        "epgCos": "Cos0",
                        "epgCosPref": "disabled",
                        "extMngdBy": "",
                        "forceResolve": "yes",
                        "instrImedcy": "lazy",
                        "lagPolicyName": "",
                        "lcOwn": "local",
                        "modTs": "2019-08-18T20:52:13.570+00:00",
                        "mode": "default",
                        "monPolDn": "uni/tn-common/monepg-default",
                        "netflowDir": "both",
                        "netflowPref": "disabled",
                        "numPorts": "0",
                        "portAllocation": "none",
                        "primaryEncap": "unknown",
                        "primaryEncapInner": "unknown",
                        "rType": "mo",
                        "resImedcy": "lazy",
                        "secondaryEncapInner": "unknown",
                        "state": "missing-target",
                        "stateQual": "none",
                        "status": "",
                        "switchingMode": "native",
                        "tCl": "infraDomP",
                        "tDn": "uni/vmmp-VMware/dom-DVS_Dell",
                        "tType": "mo",
                        "triggerSt": "triggerable",
                        "txId": "8646911284551354729",
                        "uid": "15374"
                    }
                }
            }
        ],
        "failed": false
    }
}

######################################


### aci_result1


ok: [apic1r] => {
    "aci_result1": [
        {
            "fvRsDomAtt": {
                "attributes": {
                    "annotation": "",
                    "bindingType": "none",
                    "childAction": "",
                    "classPref": "encap",
                    "configIssues": "",
                    "delimiter": "",
                    "dn": "uni/tn-TN_prod/ap-AP_Test/epg-EPG_Test/rsdomAtt-[uni/vmmp-VMware/dom-DVS_Dell]",
                    "encap": "unknown",
                    "encapMode": "auto",
                    "epgCos": "Cos0",
                    "epgCosPref": "disabled",
                    "extMngdBy": "",
                    "forceResolve": "yes",
                    "instrImedcy": "lazy",
                    "lagPolicyName": "",
                    "lcOwn": "local",
                    "modTs": "2019-08-18T20:52:13.570+00:00",
                    "mode": "default",
                    "monPolDn": "uni/tn-common/monepg-default",
                    "netflowDir": "both",
                    "netflowPref": "disabled",
                    "numPorts": "0",
                    "portAllocation": "none",
                    "primaryEncap": "unknown",
                    "primaryEncapInner": "unknown",
                    "rType": "mo",
                    "resImedcy": "lazy",
                    "secondaryEncapInner": "unknown",
                    "state": "missing-target",
                    "stateQual": "none",
                    "status": "",
                    "switchingMode": "native",
                    "tCl": "infraDomP",
                    "tDn": "uni/vmmp-VMware/dom-DVS_Dell",
                    "tType": "mo",
                    "triggerSt": "triggerable",
                    "txId": "8646911284551354729",
                    "uid": "15374"
                }
            }
        }
    ]
}




############################################

### aci_result2

fatal: [apic1r]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'fvRsDomAtt'\n\nThe error appears to be in '/etc/ansible/playbooks/cisco/aci/create_bd_ap_epg3.yml': line 37, column 8, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n     - set_fact:\n       ^ here\n"}

#
 - set_fact: aci_result1: "{{ DVS_Result.current }}" - set_fact: aci_result2: "{{ DVS_Result.fvRsDomAtt.attributes.dn }}"
#
 - debug: msg: "{{ DVS_Result }}" - debug: var=aci_result1 - debug: var=aci_result2

DVS_结果

ok: [apic1r] => { "msg": { "changed": false, "current": [ { "fvRsDomAtt": { "attributes": { "annotation": "", "bindingType": "none", "childAction": "", "classPref": "encap", "configIssues": "", "delimiter": "", "dn": "uni/tn-TN_prod/ap-AP_Test/epg-EPG_Test/rsdomAtt-[uni/vmmp-VMware/dom-DVS_Dell]", "encap": "unknown", "encapMode": "auto", "epgCos": "Cos0", "epgCosPref": "disabled", "extMngdBy": "", "forceResolve": "yes", "instrImedcy": "lazy", "lagPolicyName": "", "lcOwn": "local", "modTs": "2019-08-18T20:52:13.570+00:00", "mode": "default", "monPolDn": "uni/tn-common/monepg-default", "netflowDir": "both", "netflowPref": "disabled", "numPorts": "0", "portAllocation": "none", "primaryEncap": "unknown", "primaryEncapInner": "unknown", "rType": "mo", "resImedcy": "lazy", "secondaryEncapInner": "unknown", "state": "missing-target", "stateQual": "none", "status": "", "switchingMode": "native", "tCl": "infraDomP", "tDn": "uni/vmmp-VMware/dom-DVS_Dell", "tType": "mo", "triggerSt": "triggerable", "txId": "8646911284551354729", "uid": "15374" } } } ], "failed": false } } ###################################### ### aci_result1 ok: [apic1r] => { "aci_result1": [ { "fvRsDomAtt": { "attributes": { "annotation": "", "bindingType": "none", "childAction": "", "classPref": "encap", "configIssues": "", "delimiter": "", "dn": "uni/tn-TN_prod/ap-AP_Test/epg-EPG_Test/rsdomAtt-[uni/vmmp-VMware/dom-DVS_Dell]", "encap": "unknown", "encapMode": "auto", "epgCos": "Cos0", "epgCosPref": "disabled", "extMngdBy": "", "forceResolve": "yes", "instrImedcy": "lazy", "lagPolicyName": "", "lcOwn": "local", "modTs": "2019-08-18T20:52:13.570+00:00", "mode": "default", "monPolDn": "uni/tn-common/monepg-default", "netflowDir": "both", "netflowPref": "disabled", "numPorts": "0", "portAllocation": "none", "primaryEncap": "unknown", "primaryEncapInner": "unknown", "rType": "mo", "resImedcy": "lazy", "secondaryEncapInner": "unknown", "state": "missing-target", "stateQual": "none", "status": "", "switchingMode": "native", "tCl": "infraDomP", "tDn": "uni/vmmp-VMware/dom-DVS_Dell", "tType": "mo", "triggerSt": "triggerable", "txId": "8646911284551354729", "uid": "15374" } } } ] } ############################################ ### aci_result2 fatal: [apic1r]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'fvRsDomAtt'\\n\\nThe error appears to be in '/etc/ansible/playbooks/cisco/aci/create_bd_ap_epg3.yml': line 37, column 8, but may\\nbe elsewhere in the file depending on the exact syntax problem.\\n\\nThe offending line appears to be:\\n\\n\\n - set_fact:\\n ^ here\\n"}

使用json_query。 例如

- debug:
    msg: "{{ DVS_Result.current|
             json_query('[].fvRsDomAtt.attributes.dn') }}"

非常感谢弗拉基米尔(Vladimir),效果很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM