0

My template file is node_rules.yml.j2:

groups:
- name: Node Alerts
  rules:
  {{ prometheus_alert_rules | to_nice_yaml(indent=3,sort_keys=False) | indent(3,False) }}

And default file is main.yml:

prometheus_alert_rules:
  - alert: Load_RAM_>_80%_for_5_min
    expr: '100 * (1 - ((avg_over_time(node_memory_MemFree_bytes[5m]) + avg_over_time(node_memory_Cached_bytes[5m]) + avg_over_time(node_memory_Buffers_bytes[5m])) / avg_over_time(node_memory_MemTotal_bytes[5m]))) > 80'
    for: 0m
    labels:
      severity: critical
    annotations:
      summary: '{% raw %}Host used many RAM (instance {{ $labels.instance }}){% endraw %}'
      description: '{% raw %}Using RAM avg. 5 min. > 80%\n  VALUE = {{ $value }}\n  LABELS = {{ $labels }}{% endraw %}'

After applying the playbook, the output is a node_rules.yml file with shifted lines:

   -  alert: Load_RAM_>_80%_for_5_min
      expr: 100 * (1 - ((avg_over_time(node_memory_MemFree_bytes[5m]) + avg_over_time(node_memory_Cached_bytes[5m])
         + avg_over_time(node_memory_Buffers_bytes[5m])) / avg_over_time(node_memory_MemTotal_bytes[5m])))
         > 80
      for: 0m
      labels:
         severity: critical
      annotations:
         summary: Host used many RAM (instance {{ $labels.instance }})
         description: Using RAM avg. 5 min. > 80%\n  VALUE = {{ $value
            }}\n  LABELS = {{ $labels }}

Why?

Maksim
  • 197
  • 2
  • 12
  • I'd recommend using 2 for indentation instead of 3 when it comes to formatting YAML. It comes from your indentation in your template file, though it seems like you're not including all of your output of _node_rules.yml_. – user1098490 Feb 02 '22 at 07:54
  • 2
    For a start, you can just remove the spaces you explicitly added in front of your statements, or consider using `lstrip_blocks`. I think this is already answered: https://stackoverflow.com/questions/42914324/extra-spaces-appearing-in-ansible-templates – user1098490 Feb 02 '22 at 08:01
  • Can you be more specific about what you mean by "shifted lines"? If you mean "why are all the lines indented at the beginning", the answer is that you're explicitly passing your YAML output through `indent(3, False)`. If you're asking "why are the lines wrapped differently", that's not an issue; they're semantically identical. – larsks Feb 04 '22 at 03:28

0 Answers0