简体   繁体   中英

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

I query the Cisco ACI to attain the advanced vmm provider details for a specific EPG. The result is successful. I then register the result to a variable. I try to search that variable and obtain\\extract a single specific piece of information such as 'dn' or 'encap' as this would allow me to use the information in other plays.

Unfortunately i'm unable to extract the information as the result comes back in an usual format. Looking at a debug on the register variable, it would appear it's a dictionary variable but no matter what I try the only item i'm able to access is the 'current' item. All other items are not registered as dictionary items.

I have tried to change the variable to a list but still i'm unable to attain the information I require. I've searched forums to see if the there is a methodology to convert the variable from a json result or dictionary variable to a string and then grep for the information but no success. Ideally I would like to extract the information without installing additional 'apps'.

Will be very grateful if someone can advise how to search for a specific result from an irregular nested result which doesn't list the items in a correct dictionary format.

- 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_Result

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

Use json_query. For example

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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