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

Ansible读取CSV后debug打印失败,请求排查指导

问题分析与解决方法

核心原因

read_csv模块读取CSV后,注册变量topics里的list是由字典构成的列表——每个字典对应CSV中的一行数据(表头作为字典的键,行内容对应值)。你的CSV只有1行有效数据,所以topics.list是长度为1的列表,列表索引从0开始,不是1。

你遇到的两个错误:

  • {{ topics.list.name }}:直接对列表调用.name属性,列表本身没有这个属性,所以报错'list object' has no attribute 'name'。
  • {{ topics.list.1.name }}:列表只有索引0的元素,索引1超出范围,自然无效。

解决方案

1. 获取单行数据的name字段

如果CSV只有一行数据,直接用索引0访问列表元素,再取name键:

- name: Login to cluster
  shell: /home/srikanth/ls.sh
- name: Read the CSV file
  read_csv:
    path: /home/srikanth/template1.csv
  register: topics
- debug:
    msg: 'Name {{ topics.list.0.name }}'

2. 遍历所有行的name字段(适配多行CSV)

如果以后CSV会有多行数据,用loop循环遍历列表中的每个字典:

- name: Login to cluster
  shell: /home/srikanth/ls.sh
- name: Read the CSV file
  read_csv:
    path: /home/srikanth/template1.csv
  register: topics
- debug:
    msg: 'Name {{ item.name }}'
  loop: "{{ topics.list }}"

内容的提问来源于stack exchange,提问作者Srikanth Venugopalan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 22:45:45