You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.6KB

  1. {%- macro printassign(key, value) %}
  2. {#- Check values like: 3, 25, 3s, 45m, 8d #}
  3. {%- if value is number or "vars" in value or value in ["false","true"] or key in ["check_interval", "retry_interval"] %}
  4. {{ key }} = {{ value }}
  5. {#- Check string values, the more common #}
  6. {%- elif value is string %}
  7. {{ key }} = "{{ value }}"
  8. {%- endif %}
  9. {%- endmacro %}
  10. {%- macro printconfig(type, object, name, config, applyto="", applymethod="")%}
  11. {%- if applymethod == "to" %}
  12. {{ type }} {{ object }} "{{ name }}" to {{ applyto }} {
  13. {%- elif applymethod == "for" %}
  14. {{ type }} {{ object }} for {{ applyto }} {
  15. {%- elif object == "Host" and type != "template" %}
  16. {{ type }} {{ object }} {{ name }} {
  17. {%- else %}
  18. {{ type }} {{ object }} "{{ name }}" {
  19. {%- endif %}
  20. {%- if config is defined %}
  21. {#- Check import first to be the first line on config blocks #}
  22. {%- for key, value in config.items() %}
  23. {%- if key == "import" %}
  24. {{key}} "{{ value }}"
  25. {%- endif %}
  26. {%- endfor %}
  27. {%- for key, value in config.items() if key != "import" -%}
  28. {{ printassign(key, value) }}
  29. {#- Handle vars values that can be a dict, list or value #}
  30. {%- if key == "vars" %}
  31. {%- for varkey, varvalue in config.vars.items() %}
  32. {%- if varvalue is mapping %}
  33. {%- for k, v in varvalue.items() %}
  34. vars.{{ varkey }}["{{ k }}"] = {
  35. {%- if v is not none %}
  36. {%- for k1, v1 in v.items() -%}
  37. {{ printassign(k1, v1)|indent(2)}}
  38. {%- endfor %}
  39. {%- endif %}
  40. }
  41. {%- endfor %}
  42. {%- else -%}
  43. {{ printassign("vars." + varkey, varvalue) }}
  44. {%- endif %}
  45. {%- endfor %}
  46. {#- Handle dict values mapping dict type on yaml with icinga2 syntax #}
  47. {%- elif value is mapping %}
  48. {{ key }} = {
  49. {%- for k, v in value.items() -%}
  50. {{ printassign(k, v)|indent(2) }}
  51. {%- endfor %}
  52. }
  53. {#- Special case for assign and ignore #}
  54. {%- elif key in ["assign", "ignore"] %}
  55. {%- for item in value %}
  56. {{ key }} where {{ item }}
  57. {%- endfor %}
  58. {#- Handle lists values mapping list type on yaml with icinga2 syntax #}
  59. {%- elif value is iterable and not value is string %}
  60. {{ key }} = [ {{ value|join(",") }} ]
  61. {%- endif %}
  62. {%- endfor %}
  63. {#- Add a line to aggregate variables read from dictionary #}
  64. {%- if applymethod == "for" %}
  65. vars += config
  66. {%- endif %}
  67. {%- endif %}
  68. }
  69. {%- endmacro %}