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

Ansible如何匹配added_bundle_images列表值获取对应index_image

问题原因

你之前使用equalto匹配的逻辑是要求added_bundle_images字段严格和[lastcsv]列表完全相等,虽然示例中该字段都是单元素列表,但该写法不通用,也容易因为列表元素顺序、隐式类型转换等问题匹配失败,因此返回空列表。

正确写法

要判断added_bundle_images列表是否包含指定的镜像值,直接使用Jinja2内置的contains测试即可,筛选完成后再通过map过滤器提取对应的index_image属性:

- name: 输出匹配到的index_image
  debug:
    msg: "{{ allcsv | selectattr('added_bundle_images', 'contains', lastcsv) | map(attribute='index_image') | first }}"

执行后输出就是你需要的目标值:locuz.qe.pnq.local/eng-build/iib:297497

如果需要将结果保存为变量,可以改成set_fact写法:

- name: 保存匹配到的index_image到变量
  set_fact:
    target_index_image: "{{ allcsv | selectattr('added_bundle_images', 'contains', lastcsv) | map(attribute='index_image') | first }}"

如果存在多个匹配条目,去掉末尾的first过滤器,即可得到所有匹配的index_image组成的列表。

内容的提问来源于stack exchange,提问作者Niranjan M.R

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 03:36:03