routers_ansible_wgX_conf_j2.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package models
  15. const wgX_conf_j2 = `
  16. {% set interface = lookup('vars', 'wireguard_' + item + '_interface') -%}
  17. {% set peers = lookup('vars', 'wireguard_' + item + '_peers') -%}
  18. {% set interface_required_keys = { 'private_key': 'PrivateKey' } -%}
  19. {% set interface_optional_keys = {
  20. 'address': 'Address',
  21. 'listen_port': 'ListenPort',
  22. 'fw_mark': 'FwMark',
  23. 'dns': 'DNS',
  24. 'mtu': 'MTU',
  25. 'table': 'Table',
  26. 'pre_up': 'PreUp',
  27. 'post_up': 'PostUp',
  28. 'pre_down': 'PreDown',
  29. 'post_down': 'PostDown',
  30. 'save_config': 'SaveConfig'
  31. } -%}
  32. {% set peer_required_keys = {
  33. 'public_key': 'PublicKey',
  34. 'allowed_ips': 'AllowedIPs'
  35. } -%}
  36. {% set peer_optional_keys = {
  37. 'endpoint': 'EndPoint',
  38. 'preshared_key': 'PresharedKey',
  39. 'persistent_keepalive': 'PersistentKeepalive'
  40. } -%}
  41. {{ ansible_managed | comment }}
  42. [Interface]
  43. {% for key, option in interface_required_keys.items() %}
  44. {{ option }} = {{ interface[key] }}
  45. {% endfor %}
  46. {% for key, option in interface_optional_keys.items() %}
  47. {% if interface[key] is defined %}
  48. {{ option }} = {{ interface[key] }}
  49. {% endif %}
  50. {% endfor %}
  51. {% for peer_name, peer in peers.items() %}
  52. [Peer] # {{ peer_name }}
  53. {% for key, option in peer_required_keys.items() %}
  54. {{ option }} = {{ peer[key] }}
  55. {% endfor %}
  56. {% for key, option in peer_optional_keys.items() %}
  57. {% if peer[key] is defined %}
  58. {{ option }} = {{ peer[key] }}
  59. {% endif %}
  60. {% endfor %}
  61. {% endfor %}
  62. `