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.

137 lines
4.5KB

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