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.

120 lines
4.0KB

  1. {%- import_yaml "openssh/defaults.yaml" as default_settings -%}
  2. {%- set ssh_config = salt['pillar.get']('ssh_config', default=default_settings.ssh_config, merge=True) -%}
  3. {#- present in ssh_config and known in actual file options -#}
  4. {%- set processed_options = [] -%}
  5. {%- set string_or_list_options = ['KexAlgorithms', 'Ciphers', 'MACs'] -%}
  6. {%- macro render_raw_option(keyword, value) -%}
  7. {%- if value is sameas true -%}
  8. {{ keyword }} yes
  9. {%- elif value is sameas false -%}
  10. {{ keyword }} no
  11. {%- elif value is string or value is number -%}
  12. {{ keyword }} {{ value }}
  13. {%- else -%}
  14. {%- for single_value in value -%}
  15. {{ keyword }} {{ single_value }}
  16. {% endfor -%}
  17. {%- endif -%}
  18. {%- endmacro -%}
  19. {#- generic renderer used for ssh matches, known options, -#}
  20. {#- and unknown options -#}
  21. {%- macro render_option(keyword, default, config_dict=ssh_config) -%}
  22. {%- set value = config_dict.get(keyword, default) -%}
  23. {{ render_raw_option(keyword, value) }}
  24. {%- endmacro -%}
  25. {#- macros for render option according to present -#}
  26. {%- macro option_impl(keyword, default, present) -%}
  27. {%- if present -%}
  28. {%- do processed_options.append(keyword) -%}
  29. {%- set prefix='' -%}
  30. {%- else -%}
  31. {%- set prefix='#' -%}
  32. {%- endif -%}
  33. {#- add prefix to keyword -#}
  34. {%- set keyword = prefix ~ keyword -%}
  35. {{ render_option(keyword, default) }}
  36. {%- endmacro -%}
  37. {#- macros for render option commented by default -#}
  38. {%- macro option(keyword, default, present) -%}
  39. {{ option_impl(keyword, default, keyword in ssh_config) }}
  40. {%- endmacro -%}
  41. {#- macros for render option uncommented by default -#}
  42. {%- macro option_default_uncommented(keyword, default, present) -%}
  43. {{ option_impl(keyword, default, True) }}
  44. {%- endmacro -%}
  45. {#- macro for collapsing a list into a string -#}
  46. {%- macro option_collapselist(keyword, sep) -%}
  47. {%- do processed_options.append(keyword) -%}
  48. {{keyword}} {{ssh_config.get(keyword)|join(sep)}}
  49. {%- endmacro -%}
  50. {#- macro for handling an option that can be specified as a list or a string -#}
  51. {%- macro option_string_or_list(keyword, default, default_commented, sep=',') -%}
  52. {%- if ssh_config.get(keyword, '') is string -%}
  53. {%- if default_commented -%}
  54. {{ option(keyword, default) }}
  55. {%- else -%}
  56. {{ option_default_uncommented(keyword, default) }}
  57. {%- endif -%}
  58. {%- else -%}
  59. {{ option_collapselist(keyword, sep) }}
  60. {%- endif -%}
  61. {%- endmacro -%}
  62. {%- if ssh_config.get('ConfigBanner', False) -%}
  63. {{ ssh_config['ConfigBanner'] }}
  64. {%- else -%}
  65. # Do not edit this file manually!
  66. # It will be overwritten by salt!
  67. {%- endif %}
  68. {%- set global_src_url = salt ['pillar.get']('__formulas:print_template_url', None) %}
  69. {%- set local_src_url = salt ['pillar.get']('openssh-formula:print_template_url', None) %}
  70. {%- if (global_src_url and local_src_url is none) or local_src_url %}
  71. #
  72. # Template used to generate this file:
  73. # {{ source }}
  74. {%- endif %}
  75. {%- if 'Hosts' in ssh_config %}
  76. {%- do processed_options.append('Hosts') %}
  77. {% for host, conf in ssh_config['Hosts'].items() %}
  78. Host {{ host }}
  79. {%- for key, val in conf.items() %}
  80. {{ render_raw_option(key, val) }}
  81. {%- endfor %}
  82. {%- endfor %}
  83. {%- endif %}
  84. {# Handling unknown in salt template options #}
  85. {%- for keyword in ssh_config.keys() %}
  86. {#- Matches have to be at the bottom and should be handled differently -#}
  87. {%- if not keyword in processed_options and keyword != 'matches' -%}
  88. {%- if not keyword in string_or_list_options -%}
  89. {#- send a blank default as it doesn't matter #}
  90. {{ render_option(keyword, '') }}
  91. {%- else -%}
  92. {#- same as above #}
  93. {{ option_string_or_list(keyword, '', True) }}
  94. {%- endif -%}
  95. {%- endif -%}
  96. {%- endfor %}
  97. {# Handle matches last as they need to go at the bottom #}
  98. {%- if 'matches' in ssh_config %}
  99. {%- for match in ssh_config['matches'].values() %}
  100. Match {{ match['type'].keys()[0] }} {{ match['type'].values()[0] }}
  101. {%- for keyword in match['options'].keys() %}
  102. {{ render_option(keyword, '', config_dict=match['options']) }}
  103. {%- endfor %}
  104. {%- endfor %}
  105. {%- endif %}
  106. {#- vim: set ft=jinja : #}