jinja syntax error in lookup template says "can't read file"

I have template that contains

`

  • job_name: nodes
    consul_sd_configs:
    {% for cenv in prometheus_consul_dcs %}
    {% endfor %}
    `

This loads fine with

`
prometheus_yml: “{{lookup(‘template’, ‘…/files/prometheus/prometheus.yml.j2’)}}”

`

But when I add a loop

`

  • job_name: nodes
    consul_sd_configs:
    {% for cenv in prometheus_consul_dcs %}
    {% endfor %}
    `

I get an error saying the file can’t read.

`
TASK: [prometheus | Create prometheus configuration file] *********************

fatal: [10.0.196.193] => Failed to template {{lookup(‘template’, ‘…/files/prometheus/prometheus.yml.j2’)}}: unable to read /Users/bkaplan/si/ops/ansible/roles/prometheus/files/prometheus/prometheus.yml.j2
`

I guess one could argue that “can’t read” is not the same as “can’t find” and this should be interpreted as an error in the file. Is there any way to get the actual error from jinja?

Seems the trick is to run the template thru the template action directly:

template: src: ../files/prometheus/prometheus.yml.j2 dest: /tmp/p.yml

Then you get a proper error message:

TASK: [TEST TEST] ************************************************************* fatal: [10.0.196.193] => {'msg': "AnsibleUndefinedVariable: One or more undefined variables: 'prometheus_consul_dcs' is undefined", 'failed': True}