-1

I am trying hard to iterate with_subelements and use the item.0.id INSIDE the template file which is loaded via lookup('template',...).

It seems as if the variable is not available during templating.

# 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

The win_uri alsways returns with the error:

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"}

Update

Found the root cause: My file for templating is a JSON file. This means:

  1. There may occur "}}"
  2. The values sometimes already contain "{{...}}" which means that I have to use a different start-end border.
  3. The support for lookup+template+variable_start/end_string is planned for Ansible 2.8 (https://github.com/ansible/ansible/pull/49711) and should the look like: body: "{{ lookup('template', 'dashboards/' + item.1, variable_start_string='[%', variable_end_string='%]' ) }}".
halfer
  • 19,824
  • 17
  • 99
  • 186
eventhorizon
  • 2,977
  • 8
  • 33
  • 57

1 Answers1

0

It looks like you did not define grafana_app_external_url and grafana_app_authorization_header

Oliver Gaida
  • 1,722
  • 7
  • 14