首页 > 解决方案 > Ansible模板混合元素的顺序

问题描述

我有一个问题,可能想出了一个新的 Ansible 版本,因为它以前可以工作:

我将此块传递给 ansible 模板

 - monitoring-test-blackbox_exporter:
   source: "{{ consul_template_template_dir }}/blackbox_exporter.ctmpl"
   destination: "/etc/prometheus/file_sd/blackbox_exporter.json"
   create_dest_dirs: true
   command_timeout: "60s"
   error_on_missing_key: false
   grafana_link: "xtkCtBkiz" 

这是模板:

# Template configuration
{% for ctmpl in consul_template_templates_config_node %}
# {{ ctmpl | first }}
template {
{% for option, value in ctmpl.items()  %}
{%     if value is sameas true %}
  {{ option }} = true
{%     elif value is sameas false %}
  {{ option }} = false
{%     elif value is string %}
  {{ option }} = "{{ value|string }}"
{%     elif value is number %}
  {{ option }} = {{ value|int }}
{%     endif %}
{%   endfor %} 

ctmpl | first之前总是过滤掉第一个元素 monitoring-test-blackbox_exporter 这很重要,因为我们稍后会在模板配置中使用它。

我用排序和选择属性尝试了几件事,但它们都不起作用。有没有人有想法让它再次工作?

标签: ansiblejinja2

解决方案


推荐阅读