You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Ansible遍历Cisco ACI返回的复杂字典提取EPG的dn值

首先你不需要使用dict2items过滤器,cisco.aci.aci_epg模块的查询返回结果中,所有EPG业务数据都存储在epg_list.current列表字段中,直接遍历该列表即可提取嵌套的dn值。你之前遍历整个epg_list变量的方式不对,该变量还包含了Ansible任务执行的元数据(如执行状态、返回码等),不是纯EPG数据集,所以无法拿到目标字段。

1. 调试提取EPG dn值的代码示例

- name: 提取并打印所有EPG的dn值
  ansible.builtin.debug:
    msg: "EPG DN: {{ item.fvAEPg.attributes.dn }},当前shutdown状态: {{ item.fvAEPg.attributes.shutdown }}"
  loop: "{{ epg_list.current }}"
  delegate_to: localhost

2. 批量关闭所有EPG的完整任务示例

你可以直接用aci_epg模块结合提取到的dn值批量设置shutdown状态,不需要额外调用aci_rest模块:

- name: 批量关闭所有未关闭的EPG
  cisco.aci.aci_epg:
    host: "{{ inventory_hostname }}"
    username: "{{ ansible_user }}"
    password: "{{ ansible_password }}"
    validate_certs: no
    dn: "{{ item.fvAEPg.attributes.dn }}"
    shutdown: "yes"
    state: present
  loop: "{{ epg_list.current }}"
  # 可选过滤:仅处理当前状态为未关闭的EPG,减少无效请求
  when: item.fvAEPg.attributes.shutdown == 'no'
  delegate_to: localhost

内容的提问来源于stack exchange,提问作者Patrick Brown

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 12:48:05