I have a template in Ansible to make a config file for Prometheus. I would like to add hosts dynamically, using variable prometheus_hosts
. This variable is defined in host_vars per each host, but Ansible has probably some problem with the template.
Variable is set like: prometheus_hosts: [ host1, host2, host3 ]
The template
global:
scrape_interval: 15s
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'prometheus'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
{% if prometheus_hosts is defined %}
{% for host in prometheus_hosts %}
- job_name: '{{ host }}'
scrape_interval: 5s
static_configs:
- targets: ['{{ host }}:9100']
{% endfor %}
- job_name: 'mysql'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9104']
- job_name: 'redis'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9121']
{% for host in prometheus_hosts if host.name.startswith('edge') %}
- job_name: '{{ host }}_varnish'
scrape_interval: 5s
static_configs:
- targets: ['{{ host }}:9131']
{% endfor %}
{% endif %}
The Error
fatal: [test-mw]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'ansible.parsing.yaml.objects.AnsibleUnicode object' has no attribute 'name'"} to retry, use: --limit @/home/gitlab-runner/builds/xw_vGpUQ/0/ansible/middleware/middleware.retry
Do you have any idea how to fix it? Thank you!