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.

81 lines
2.7KB

  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. {%- 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. {#- generic renderer used for ssh matches, known options, -#}
  19. {#- and unknown options -#}
  20. {%- macro render_option(keyword, default, config_dict=ssh_config) -%}
  21. {%- set value = config_dict.get(keyword, default) -%}
  22. {{ render_raw_option(keyword, value) }}
  23. {%- endmacro -%}
  24. {#- macros for render option according to present -#}
  25. {%- macro option_impl(keyword, default, present) -%}
  26. {%- if present -%}
  27. {%- do processed_options.append(keyword) -%}
  28. {%- set prefix='' -%}
  29. {%- else -%}
  30. {%- set prefix='#' -%}
  31. {%- endif -%}
  32. {#- add prefix to keyword -#}
  33. {%- set keyword = prefix ~ keyword -%}
  34. {{ render_option(keyword, default) }}
  35. {%- endmacro -%}
  36. {#- macros for render option commented by default -#}
  37. {%- macro option(keyword, default, present) -%}
  38. {{ option_impl(keyword, default, keyword in ssh_config) }}
  39. {%- endmacro -%}
  40. {#- macros for render option uncommented by default -#}
  41. {%- macro option_default_uncommented(keyword, default, present) -%}
  42. {{ option_impl(keyword, default, True) }}
  43. {%- endmacro -%}
  44. # Do not edit this file manually!
  45. # It will be overwritten by salt!
  46. {%- if 'Hosts' in ssh_config %}
  47. {%- do processed_options.append('Hosts') %}
  48. {% for host, conf in ssh_config['Hosts'].items() %}
  49. Host {{ host }}
  50. {%- for key, val in conf.items() %}
  51. {{ render_raw_option(key, val) }}
  52. {%- endfor %}
  53. {%- endfor %}
  54. {%- endif %}
  55. {# Handling unknown in salt template options #}
  56. {%- for keyword in ssh_config.keys() %}
  57. {#- Matches have to be at the bottom and should be handled differently -#}
  58. {%- if not keyword in processed_options and keyword != 'matches' -%}
  59. {#- send a blank default as it doesn't matter #}
  60. {{ render_option(keyword, '') }}
  61. {%- endif -%}
  62. {%- endfor %}
  63. {# Handle matches last as they need to go at the bottom #}
  64. {%- if 'matches' in ssh_config %}
  65. {%- for match in ssh_config['matches'].values() %}
  66. Match {{ match['type'].keys()[0] }} {{ match['type'].values()[0] }}
  67. {%- for keyword in match['options'].keys() %}
  68. {{ render_option(keyword, '', config_dict=match['options']) }}
  69. {%- endfor %}
  70. {%- endfor %}
  71. {%- endif %}