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.3KB

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