我正在努力迭代with_subelements
并在通过item.0.id
加载的模板文件中使用lookup('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_uri
始终返回并显示错误:
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"}
有什么想法吗?
提前THX!
更新
找到了根本原因:用于模板化的我的文件是JSON文件。这意味着:
body: "{{ lookup('template', 'dashboards/' + item.1, variable_start_string='[%', variable_end_string='%]' ) }}"
。THX !!!
答案 0 :(得分:0)
您似乎没有定义grafana_app_external_url
和grafana_app_authorization_header