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

查看:252
本文介绍了如何在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') }}"

推荐答案

设计了一种不太好的方法:

figured a not so good method:

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

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

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