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
3.6KB

  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. {%- if 'Hosts' in ssh_config %}
  69. {%- do processed_options.append('Hosts') %}
  70. {% for host, conf in ssh_config['Hosts'].items() %}
  71. Host {{ host }}
  72. {%- for key, val in conf.items() %}
  73. {{ render_raw_option(key, val) }}
  74. {%- endfor %}
  75. {%- endfor %}
  76. {%- endif %}
  77. {# Handling unknown in salt template options #}
  78. {%- for keyword in ssh_config.keys() %}
  79. {#- Matches have to be at the bottom and should be handled differently -#}
  80. {%- if not keyword in processed_options and keyword != 'matches' -%}
  81. {%- if not keyword in string_or_list_options -%}
  82. {#- send a blank default as it doesn't matter #}
  83. {{ render_option(keyword, '') }}
  84. {%- else -%}
  85. {#- same as above #}
  86. {{ option_string_or_list(keyword, '', True) }}
  87. {%- endif -%}
  88. {%- endif -%}
  89. {%- endfor %}
  90. {# Handle matches last as they need to go at the bottom #}
  91. {%- if 'matches' in ssh_config %}
  92. {%- for match in ssh_config['matches'].values() %}
  93. Match {{ match['type'].keys()[0] }} {{ match['type'].values()[0] }}
  94. {%- for keyword in match['options'].keys() %}
  95. {{ render_option(keyword, '', config_dict=match['options']) }}
  96. {%- endfor %}
  97. {%- endfor %}
  98. {%- endif %}
  99. {#- vim: set ft=jinja : #}