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

102 lines
3.9KB

  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 override
  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 source_files parameter #}
  53. {%- set src_files = salt['config.get'](
  54. tplroot ~ ':tofs:source_files:' ~ lookup,
  55. salt['config.get'](
  56. tplroot ~ ':tofs:files:' ~ lookup,
  57. source_files
  58. )
  59. ) %}
  60. {#- Only add to [''] when supporting older TOFS implementations #}
  61. {%- set path_prefix_exts = [''] %}
  62. {%- if v1_path_prefix != '' %}
  63. {%- do path_prefix_exts.append(v1_path_prefix) %}
  64. {%- endif %}
  65. {%- for path_prefix_ext in path_prefix_exts %}
  66. {%- set path_prefix_inc_ext = path_prefix ~ path_prefix_ext %}
  67. {#- For older TOFS implementation, use `files_switch` from the config #}
  68. {#- Use the default, new method otherwise #}
  69. {%- set fsl = salt['config.get'](
  70. tplroot ~ path_prefix_ext|replace('/', ':') ~ ':files_switch',
  71. files_switch_list
  72. ) %}
  73. {#- Append an empty value to evaluate as `default` in the loop below #}
  74. {%- if '' not in fsl %}
  75. {%- do fsl.append('') %}
  76. {%- endif %}
  77. {%- for fs in fsl %}
  78. {%- for src_file in src_files %}
  79. {%- if fs %}
  80. {%- set fs_dir = salt['config.get'](fs, fs) %}
  81. {%- else %}
  82. {%- set fs_dir = salt['config.get'](tplroot ~ ':tofs:dirs:default', 'default') %}
  83. {%- endif %}
  84. {%- set url = [
  85. '- salt:/',
  86. path_prefix_inc_ext.strip('/'),
  87. files_dir.strip('/'),
  88. fs_dir.strip('/'),
  89. src_file.strip('/'),
  90. ] | select | join('/') %}
  91. {{ url | indent(indent_width, true) }}
  92. {%- endfor %}
  93. {%- endfor %}
  94. {%- endfor %}
  95. {%- endmacro %}