在循环中的模板查找中使用item变量

问题描述 投票:0回答:1

我正在努力迭代with_subelements并使用item.0.id INSIDE模板文件,该文件通过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提前!

UPDATE

找到根本原因:我的模板文件是一个JSON文件。这意味着:

  1. 可能会出现“}}”
  2. 这些值有时已包含“{{...}}”,这意味着我必须使用不同的起始边框。
  3. 查找+ template + variable_start / end_string的支持计划用于ansible 2.8(https://github.com/ansible/ansible/pull/49711),看起来应该是:body: "{{ lookup('template', 'dashboards/' + item.1, variable_start_string='[%', variable_end_string='%]' ) }}"

谢谢!!!

ansible templating
1个回答
0
投票

看起来你没有定义grafana_app_external_urlgrafana_app_authorization_header

© www.soinside.com 2019 - 2024. All rights reserved.