Ansible 在循环中的模板查找中使用项变量
我正在努力用子元素迭代Ansible 在循环中的模板查找中使用项变量,ansible,templating,Ansible,Templating,我正在努力用子元素迭代,并在模板文件中使用项.0.id,该模板文件是通过查找('template',…)加载的 似乎在模板化期间该变量不可用 # data: grafana_app_data: pKWGOV9mk: boardFiles: [ "server-ping.json.j2" ], id: "2", title: "SERVER" kju... # working: - name: debug merge result de
,并在模板文件中使用项.0.id
,该模板文件是通过查找('template',…)加载的
似乎在模板化期间该变量不可用
# data:
grafana_app_data:
pKWGOV9mk:
boardFiles: [
"server-ping.json.j2"
],
id: "2",
title: "SERVER"
kju...
# working:
- name: debug merge result
debug:
msg: "{{item.0.id}} --- {{item.1}}"
with_subelements:
- "{{ grafana_app_data }}"
- boardFiles
# not working:
- name: iterate over folders and create respective dashboards
win_uri:
url: '{{ grafana_app_external_url }}api/dashboards/db'
validate_certs: false
method: POST
status_code: '200,409'
headers:
Accept: 'application/json'
Content-Type: 'application/json'
Authorization: '{{ grafana_app_authorization_header }}'
body: "{{ lookup('template', 'dashboards/' + item.1 ) }}"
with_subelements:
- "{{ grafana_app_data }}"
- boardFiles
win\u uri
alsways返回错误:
fatal: [....net]: FAILED! => {"msg": "The task includes an option with an
undefined variable. The error was: 'env' is undefined\n\nThe error appears
to have been in '/.../roles/...inject/tasks/main.yml': line 123, column 3,
but may\nbe elsewhere in the file depending on the exact syntax
problem.\n\nThe offending line appears to be:\n\n\n- name: iterate over
folders and create respective dashboards\n ^ here\n"}
更新
找到根本原因:我的模板文件是一个JSON文件。这意味着:
可能出现“}}”
这些值有时已经包含“{…}”,这意味着我必须使用不同的开始-结束边界
Ansible 2.8()计划支持lookup+template+variable\u start/end\u string,其外观如下:正文:{{lookup('template',dashboards/'+item.1,variable\u start\u string='[%',variable\u end\u string='%]')}
看起来您没有定义grafana\u app\u external\u url
和grafana\u app\u authorization\u header
董事会文件的内容是什么?是env吗?