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.

135 lines
4.3KB

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