问题描述
我正在使用 Google Sheets V4 Values 集合,但无法弄清楚如何将每一行解析为 {{ item }}
I am using the Google Sheets V4 Values collection and I am having trouble figuring out how to get each row to parse in to an {{ item }}
我的 Ansible ymal 看起来像.
My Ansible ymal looks like.
tasks:
- name: debug url
debug:
msg: "{{ g_url }}"
- name: GET data from google spead sheet api
uri:
url: "{{ g_url }}"
return_content: yes
dest: /tmp/o_gd_form.json
register: google_data
- name: whats the dump?
debug:
var: "{{ item.0 |to_json }}"
with_items: "{{ google_data.json.values() }}" # this is the line that needs to be fixed
响应的json看起来像:
And the the responding json looks like:
{
"range": "Sheet1!A1:D5",
"majorDimension": "ROWS",
"values": [
["Item", "Cost", "Stocked", "Ship Date"],
["Wheel", "$20.50", "4", "3/1/2016"],
["Door", "$15", "2", "3/15/2016"],
["Engine", "$100", "1", "30/20/2016"],
["Totals", "$135.5", "7", "3/20/2016"]
],
}
我似乎不知道如何编写 with_items
以返回 json 的 {{ item }}
,例如 ["Engine", "$100", "1", "30/20/2016"]
.
I can't seem figure out how to write the the with_items
to return an {{ item }}
of json like ["Engine", "$100", "1", "30/20/2016"]
.
有什么帮助还是我需要将此任务拆分为一些中间件解析器?
any help or do I need to split this task out to some middleware parser?
Google 表格 API 文档位于:
The Google sheets api documentation is at:
https://developers.google.com/sheets/samples/reading
推荐答案
要获得您想要的,请使用:
To get what you want, use:
- debug: msg="Name={{ item.0 }} Cost={{ item.1 }}"
with_list: "{{ google_data.json['values'] }}"
您的代码中有两个问题:
There are two problems in your code:
values
是一个特殊的关键字,所以你不能访问json.values
,而应该使用json['values']代码>代替.
values
is a special keyword, so you can't accessjson.values
, but should usejson['values']
instead.
with_items
使列表列表变平,因此最终会得到很长的字符串列表.您应该使用 with_list
迭代外部列表(行)并获取内部列表(列值)作为项目.
with_items
flattens the list of lists, so you end up with long list of strings. You should use with_list
to iterate over outer list (rows) and get inner lists (column values) as items.
这篇关于我如何使用ansible的with_items解析来自google sheet api的每一行数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!