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

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

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

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

我已经尝试将变量更改为列表,但我仍然无法获得我需要的信息。 我搜索了论坛,看看是否有一种方法可以将变量从 json 结果或字典变量转换为字符串,然后 grep 获取信息但没有成功。 理想情况下,我想在不安装额外 'apps'.

的情况下提取信息

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

- name: Access VMM provider Information
  hosts: apics
  gather_facts: false
  connection: local
#
  vars:
    ansible_python_interpreter: /usr/bin/python3
#
  tasks:

     - name: Play 1 Obtain VMM Provider Information
       aci_epg_to_domain:
        hostname: "{{ apics.hostname }}"
        username: "{{ apics.username }}"
        password: "{{ apics.password }}"
        tenant: Tenant_A
        ap: AP_Test
        epg: EPG_Test
        domain: DVS_Dell
        domain_type: vmm
        vm_provider: vmware
        state: query
        validate_certs: no
       register: DVS_Result
#
     - 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"}

使用json_query。例如

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