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

Ansible read_csv模块未按行读取且结果重复问题排查

Ansible read_csv模块数据重复及模板循环问题

问题现象

使用read_csv模块读取CSV文件后,每台目标服务器(testserver1、testserver2)的newrelic.list均包含CSV全部两行数据,而非仅匹配自身Hostname的行;后续执行模板复制任务时,循环数据格式报错,尝试dict2items转换后模板无法读取对应数据。

执行的Playbook代码

读取CSV的任务

- name: read csv
  read_csv:
    path: /u00/app/monitor/newrelic_test.csv
    key: Application   
  register: newrelic

- name: Print newrelic var   
  ansible.builtin.debug:
    var: newrelic.list

模板复制任务

- name: copy template
  template:
    src: /u00/ansible/Playbooks/files/infra-config.yml_template
    dest: /u00/app/monitor/infra-config.yml
  loop: "{{ newrelic.list }}"
  loop_control:
    loop_var: item

CSV文件内容

Application,env,Datacenter,Hostname
Microsoft,Test,DC1,testserver1
Apple,Test,DC2,testserver2

遇到的错误

不使用newrelic.list时触发循环数据格式错误:

[testserver1]: FAILED! => {"msg": "Invalid data passed to 'loop', it requires a list, got this instead: {'dict': 

尝试使用dict2items转换后,模板无法读取对应数据。

内容的提问来源于stack exchange,提问作者Lucky Ameta

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 14:15:26