Saltstack Official Chrony Formula
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

108 lines
4.2KB

  1. {%- macro files_switch(source_files,
  2. lookup=None,
  3. default_files_switch=['id', 'os_family'],
  4. indent_width=6,
  5. v1_path_prefix='') %}
  6. {#-
  7. Returns a valid value for the "source" parameter of a "file.managed"
  8. state function. This makes easier the usage of the Template Override and
  9. Files Switch (TOFS) pattern.
  10. Params:
  11. * source_files: ordered list of files to look for
  12. * lookup: key under '<tplroot>:tofs:source_files' to prepend to the
  13. list of source files
  14. * default_files_switch: if there's no config (e.g. pillar)
  15. '<tplroot>:tofs:files_switch' this is the ordered list of grains to
  16. use as selector switch of the directories under
  17. "<path_prefix>/files"
  18. * indent_witdh: indentation of the result value to conform to YAML
  19. * v1_path_prefix: (deprecated) only used for injecting a path prefix into
  20. the source, to support older TOFS configs
  21. Example (based on a `tplroot` of `xxx`):
  22. If we have a state:
  23. Deploy configuration:
  24. file.managed:
  25. - name: /etc/yyy/zzz.conf
  26. - source: {{ files_switch(['/etc/yyy/zzz.conf', '/etc/yyy/zzz.conf.jinja'],
  27. lookup='Deploy configuration'
  28. ) }}
  29. - template: jinja
  30. In a minion with id=theminion and os_family=RedHat, it's going to be
  31. rendered as:
  32. Deploy configuration:
  33. file.managed:
  34. - name: /etc/yyy/zzz.conf
  35. - source:
  36. - salt://xxx/files/theminion/etc/yyy/zzz.conf
  37. - salt://xxx/files/theminion/etc/yyy/zzz.conf.jinja
  38. - salt://xxx/files/RedHat/etc/yyy/zzz.conf
  39. - salt://xxx/files/RedHat/etc/yyy/zzz.conf.jinja
  40. - salt://xxx/files/default/etc/yyy/zzz.conf
  41. - salt://xxx/files/default/etc/yyy/zzz.conf.jinja
  42. - template: jinja
  43. #}
  44. {#- Get the `tplroot` from `tpldir` #}
  45. {%- set tplroot = tpldir.split('/')[0] %}
  46. {%- set path_prefix = salt['config.get'](tplroot ~ ':tofs:path_prefix', tplroot) %}
  47. {%- set files_dir = salt['config.get'](tplroot ~ ':tofs:dirs:files', 'files') %}
  48. {%- set files_switch_list = salt['config.get'](
  49. tplroot ~ ':tofs:files_switch',
  50. default_files_switch
  51. ) %}
  52. {#- Lookup source_files (v2), files (v1), or fallback to an empty list #}
  53. {%- set src_files = salt['config.get'](
  54. tplroot ~ ':tofs:source_files:' ~ lookup,
  55. salt['config.get'](tplroot ~ ':tofs:files:' ~ lookup, [])
  56. ) %}
  57. {#- Append the default source_files #}
  58. {%- set src_files = src_files + source_files %}
  59. {#- Only add to [''] when supporting older TOFS implementations #}
  60. {%- set path_prefix_exts = [''] %}
  61. {%- if v1_path_prefix != '' %}
  62. {%- do path_prefix_exts.append(v1_path_prefix) %}
  63. {%- endif %}
  64. {%- for path_prefix_ext in path_prefix_exts %}
  65. {%- set path_prefix_inc_ext = path_prefix ~ path_prefix_ext %}
  66. {#- For older TOFS implementation, use `files_switch` from the config #}
  67. {#- Use the default, new method otherwise #}
  68. {%- set fsl = salt['config.get'](
  69. tplroot ~ path_prefix_ext|replace('/', ':') ~ ':files_switch',
  70. files_switch_list
  71. ) %}
  72. {#- Append an empty value to evaluate as `default` in the loop below #}
  73. {%- if '' not in fsl %}
  74. {%- do fsl.append('') %}
  75. {%- endif %}
  76. {%- for fs in fsl %}
  77. {%- for src_file in src_files %}
  78. {%- if fs %}
  79. {%- set fs_dirs = salt['config.get'](fs, fs) %}
  80. {%- else %}
  81. {%- set fs_dirs = salt['config.get'](tplroot ~ ':tofs:dirs:default', 'default') %}
  82. {%- endif %}
  83. {#- Force the `config.get` lookup result as a list where necessary #}
  84. {#- since we need to also handle grains that are lists #}
  85. {%- if fs_dirs is string %}
  86. {%- set fs_dirs = [fs_dirs] %}
  87. {%- endif %}
  88. {%- for fs_dir in fs_dirs %}
  89. {%- set url = [
  90. '- salt:/',
  91. path_prefix_inc_ext.strip('/'),
  92. files_dir.strip('/'),
  93. fs_dir.strip('/'),
  94. src_file.strip('/'),
  95. ] | select | join('/') %}
  96. {{ url | indent(indent_width, true) }}
  97. {%- endfor %}
  98. {%- endfor %}
  99. {%- endfor %}
  100. {%- endfor %}
  101. {%- endmacro %}