如何在ansible中迭代csv文件 [英] how to iterate csv file in ansible

查看:35
本文介绍了如何在ansible中迭代csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 jinja2 模板,其中有一个部分需要来自 csv 文件的数据如何读取 csv 文件并将其拆分为列表,然后在 jinja2 模板中对其进行迭代?……像这样:

i have a jinja2 template including a section that need data from a csv file how can i read a csv file and split it into a list then iterate it in the jinja2 template? sth. like this:

{% for line in csv_data %}
    {{ line[0] }} = {{ line[1] }}
{% endfor %}

在我的任务文件中,我试图使用查找将 csv 文件读入 csv_data,但似乎查找只能查询并获取一行而不是整个文件,或者只是原始格式的整个文件

in my task file, i am trying to use lookup to read the csv file into csv_data, but it seems lookup can only query and get one line not the whole file, or just the whole file in raw format

vars:
  csv_data: "{{ lookup('file', 'test.csv') }}"

推荐答案

想了个不太好的方法:

{% for line in csv_data.split("\n") %}
       {% set list = line.split(",") %}
       {{ list[0] }}={{ list[1] }}
{% endfor %}

这篇关于如何在ansible中迭代csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆