Saltstack Official OpenSSH Formula
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.

113 lines
4.5KB

  1. {%- macro files_switch(source_files,
  2. lookup=None,
  3. default_files_switch=['id', 'os_family'],
  4. indent_width=6,
  5. use_subpath=False) %}
  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_width: indentation of the result value to conform to YAML
  19. * use_subpath: defaults to `False` but if set, lookup the source file
  20. recursively from the current state directory up to `tplroot`
  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 use_subpath and tplroot != tpldir %}
  62. {#- Walk directory tree to find {{ files_dir }} #}
  63. {%- set subpath_parts = tpldir.lstrip(tplroot).lstrip('/').split('/') %}
  64. {%- for path in subpath_parts %}
  65. {%- set subpath = subpath_parts[0:loop.index] | join('/') %}
  66. {%- do path_prefix_exts.append('/' ~ subpath) %}
  67. {%- endfor %}
  68. {%- endif %}
  69. {%- for path_prefix_ext in path_prefix_exts|reverse %}
  70. {%- set path_prefix_inc_ext = path_prefix ~ path_prefix_ext %}
  71. {#- For older TOFS implementation, use `files_switch` from the config #}
  72. {#- Use the default, new method otherwise #}
  73. {%- set fsl = salt['config.get'](
  74. tplroot ~ path_prefix_ext|replace('/', ':') ~ ':files_switch',
  75. files_switch_list
  76. ) %}
  77. {#- Append an empty value to evaluate as `default` in the loop below #}
  78. {%- if '' not in fsl %}
  79. {%- set fsl = fsl + [''] %}
  80. {%- endif %}
  81. {%- for fs in fsl %}
  82. {%- for src_file in src_files %}
  83. {%- if fs %}
  84. {%- set fs_dirs = salt['config.get'](fs, fs) %}
  85. {%- else %}
  86. {%- set fs_dirs = salt['config.get'](tplroot ~ ':tofs:dirs:default', 'default') %}
  87. {%- endif %}
  88. {#- Force the `config.get` lookup result as a list where necessary #}
  89. {#- since we need to also handle grains that are lists #}
  90. {%- if fs_dirs is string %}
  91. {%- set fs_dirs = [fs_dirs] %}
  92. {%- endif %}
  93. {%- for fs_dir in fs_dirs %}
  94. {%- set url = [
  95. '- salt:/',
  96. path_prefix_inc_ext.strip('/'),
  97. files_dir.strip('/'),
  98. fs_dir.strip('/'),
  99. src_file.strip('/'),
  100. ] | select | join('/') %}
  101. {{ url | indent(indent_width, true) }}
  102. {%- endfor %}
  103. {%- endfor %}
  104. {%- endfor %}
  105. {%- endfor %}
  106. {%- endmacro %}