Saltstack Official Linux Formula
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

305 行
8.2KB

  1. {%- from "linux/map.jinja" import network with context %}
  2. {%- from "linux/map.jinja" import system with context %}
  3. {%- if network.enabled %}
  4. {%- macro set_param(param_name, param_dict) -%}
  5. {%- if param_dict.get(param_name, False) -%}
  6. - {{ param_name }}: {{ param_dict[param_name] }}
  7. {%- endif -%}
  8. {%- endmacro -%}
  9. {%- if network.bridge != 'none' %}
  10. linux_network_bridge_pkgs:
  11. pkg.installed:
  12. {%- if network.bridge == 'openvswitch' %}
  13. - pkgs: {{ network.ovs_pkgs }}
  14. {%- else %}
  15. - pkgs: {{ network.bridge_pkgs }}
  16. {%- endif %}
  17. {%- endif %}
  18. {%- for interface_name, interface in network.interface.iteritems() %}
  19. {%- set interface_name = interface.get('name', interface_name) %}
  20. {# it is not used for any interface with type preffix dpdk,eg. dpdk_ovs_port #}
  21. {%- if interface.get('managed', True) and not 'dpdk' in interface.type %}
  22. {%- if grains.os_family in ['RedHat', 'Debian'] %}
  23. {%- if interface.type == 'ovs_bridge' %}
  24. ovs_bridge_{{ interface_name }}:
  25. openvswitch_bridge.present:
  26. - name: {{ interface_name }}
  27. {# add linux network interface into OVS bridge #}
  28. {%- for int_name, int in network.interface.iteritems() %}
  29. {%- set int_name = int.get('name', int_name) %}
  30. {%- if int.ovs_bridge is defined and interface_name == int.ovs_bridge %}
  31. add_int_{{ int_name }}_to_ovs_bridge_{{ interface_name }}:
  32. cmd.run:
  33. - unless: ovs-vsctl show | grep {{ int_name }}
  34. - name: ovs-vsctl add-port {{ interface_name }} {{ int_name }}
  35. {%- endif %}
  36. {%- endfor %}
  37. {%- elif interface.type == 'ovs_port' %}
  38. {%- if interface.get('port_type','internal') == 'patch' %}
  39. ovs_port_{{ interface_name }}:
  40. openvswitch_port.present:
  41. - name: {{ interface_name }}
  42. - bridge: {{ interface.bridge }}
  43. - require:
  44. - openvswitch_bridge: ovs_bridge_{{ interface.bridge }}
  45. ovs_port_set_type_{{ interface_name }}:
  46. cmd.run:
  47. - name: ovs-vsctl set interface {{ interface_name }} type=patch
  48. - unless: ovs-vsctl show | grep -A 1 'Interface {{ interface_name }}' | grep patch
  49. ovs_port_set_peer_{{ interface_name }}:
  50. cmd.run:
  51. - name: ovs-vsctl set interface {{ interface_name }} options:peer={{ interface.peer }}
  52. - unless: ovs-vsctl show | grep -A 2 'Interface floating-to-prv' | grep {{ interface.peer }}
  53. {%- else %}
  54. linux_interfaces_include_{{ interface_name }}:
  55. file.prepend:
  56. - name: /etc/network/interfaces
  57. - text: |
  58. source /etc/network/interfaces.d/*
  59. # Workaround for Upstream-Bug: https://github.com/saltstack/salt/issues/40262
  60. source /etc/network/interfaces.u/*
  61. ovs_port_{{ interface_name }}:
  62. file.managed:
  63. - name: /etc/network/interfaces.u/ifcfg-{{ interface_name }}
  64. - makedirs: True
  65. - source: salt://linux/files/ovs_port
  66. - defaults:
  67. port: {{ interface|yaml }}
  68. port_name: {{ interface_name }}
  69. - template: jinja
  70. ovs_port_{{ interface_name }}_line1:
  71. file.replace:
  72. - name: /etc/network/interfaces
  73. - pattern: auto {{ interface_name }}
  74. - repl: ""
  75. ovs_port_{{ interface_name }}_line2:
  76. file.replace:
  77. - name: /etc/network/interfaces
  78. - pattern: 'iface {{ interface_name }} inet .*'
  79. - repl: ""
  80. ovs_port_up_{{ interface_name }}:
  81. cmd.run:
  82. - name: ifup {{ interface_name }}
  83. - require:
  84. - file: ovs_port_{{ interface_name }}
  85. - file: ovs_port_{{ interface_name }}_line1
  86. - file: ovs_port_{{ interface_name }}_line2
  87. - openvswitch_bridge: ovs_bridge_{{ interface.bridge }}
  88. {%- endif %}
  89. {%- else %}
  90. linux_interface_{{ interface_name }}:
  91. network.managed:
  92. - enabled: {{ interface.enabled }}
  93. - name: {{ interface_name }}
  94. - type: {{ interface.type }}
  95. {%- if interface.address is defined %}
  96. {%- if grains.os_family == 'Debian' %}
  97. - proto: {{ interface.get('proto', 'static') }}
  98. {% endif %}
  99. {%- if grains.os_family == 'RedHat' %}
  100. {%- if interface.get('proto', 'none') == 'manual' %}
  101. - proto: 'none'
  102. {%- else %}
  103. - proto: {{ interface.get('proto', 'none') }}
  104. {%- endif %}
  105. {% endif %}
  106. - ipaddr: {{ interface.address }}
  107. - netmask: {{ interface.netmask }}
  108. {%- else %}
  109. - proto: {{ interface.get('proto', 'dhcp') }}
  110. {%- endif %}
  111. {%- if interface.type == 'slave' %}
  112. - master: {{ interface.master }}
  113. {%- endif %}
  114. {%- if interface.name_servers is defined %}
  115. - dns: {{ interface.name_servers }}
  116. {%- endif %}
  117. {%- if interface.wireless is defined and grains.os_family == 'Debian' %}
  118. {%- if interface.wireless.security == "wpa" %}
  119. - wpa-ssid: {{ interface.wireless.essid }}
  120. - wpa-psk: {{ interface.wireless.key }}
  121. {%- else %}
  122. - wireless-ssid: {{ interface.wireless.essid }}
  123. - wireless-psk: {{ interface.wireless.key }}
  124. {%- endif %}
  125. {%- endif %}
  126. {%- for param in network.interface_params %}
  127. {{ set_param(param, interface) }}
  128. {%- endfor %}
  129. {%- if interface.type == 'bridge' %}
  130. - bridge: {{ interface_name }}
  131. - delay: 0
  132. - bypassfirewall: True
  133. - use:
  134. {%- for network in interface.use_interfaces %}
  135. - network: linux_interface_{{ network }}
  136. {%- endfor %}
  137. - ports: {% for network in interface.get('use_interfaces', []) %}{{ network }} {% endfor %}{% for network in interface.get('use_ovs_ports', []) %}{{ network }} {% endfor %}
  138. - require:
  139. {%- for network in interface.get('use_interfaces', []) %}
  140. - network: linux_interface_{{ network }}
  141. {%- endfor %}
  142. {%- for network in interface.get('use_ovs_ports', []) %}
  143. - cmd: ovs_port_up_{{ network }}
  144. {%- endfor %}
  145. {%- endif %}
  146. {%- if interface.type == 'bond' %}
  147. - slaves: {{ interface.slaves }}
  148. - mode: {{ interface.mode }}
  149. {%- endif %}
  150. {%- for network in interface.get('use_ovs_ports', []) %}
  151. remove_interface_{{ network }}_line1:
  152. file.replace:
  153. - name: /etc/network/interfaces
  154. - pattern: auto {{ network }}
  155. - repl: ""
  156. remove_interface_{{ network }}_line2:
  157. file.replace:
  158. - name: /etc/network/interfaces
  159. - pattern: iface {{ network }} inet manual
  160. - repl: ""
  161. {%- endfor %}
  162. {%- if interface.gateway is defined %}
  163. linux_system_network:
  164. network.system:
  165. - enabled: {{ interface.enabled }}
  166. - hostname: {{ network.fqdn }}
  167. {%- if interface.gateway is defined %}
  168. - gateway: {{ interface.gateway }}
  169. - gatewaydev: {{ interface_name }}
  170. {%- endif %}
  171. - nozeroconf: True
  172. - nisdomain: {{ system.domain }}
  173. - require_reboot: True
  174. {%- endif %}
  175. {%- endif %}
  176. {%- endif %}
  177. {%- if interface.wireless is defined %}
  178. {%- if grains.os_family == 'Arch' %}
  179. linux_network_packages:
  180. pkg.installed:
  181. - pkgs: {{ network.pkgs }}
  182. /etc/netctl/network_{{ interface.wireless.essid }}:
  183. file.managed:
  184. - source: salt://linux/files/wireless
  185. - mode: 755
  186. - template: jinja
  187. - require:
  188. - pkg: linux_network_packages
  189. - defaults:
  190. interface_name: {{ interface_name }}
  191. switch_profile_{{ interface.wireless.essid }}:
  192. cmd.run:
  193. - name: netctl switch-to network_{{ interface.wireless.essid }}
  194. - cwd: /root
  195. - unless: "iwconfig {{ interface_name }} | grep -e 'ESSID:\"{{ interface.wireless.essid }}\"'"
  196. - require:
  197. - file: /etc/netctl/network_{{ interface.wireless.essid }}
  198. enable_profile_{{ interface.wireless.essid }}:
  199. cmd.run:
  200. - name: netctl enable network_{{ interface.wireless.essid }}
  201. - cwd: /root
  202. - unless: test -e /etc/systemd/system/multi-user.target.wants/netctl@network_{{ interface.wireless.essid }}.service
  203. - require:
  204. - file: /etc/netctl/network_{{ interface.wireless.essid }}
  205. {%- endif %}
  206. {%- endif %}
  207. {%- endif %}
  208. {%- if interface.route is defined %}
  209. linux_network_{{ interface_name }}_routes:
  210. network.routes:
  211. - name: {{ interface_name }}
  212. - routes:
  213. {%- for route_name, route in interface.route.iteritems() %}
  214. - name: {{ route_name }}
  215. ipaddr: {{ route.address }}
  216. netmask: {{ route.netmask }}
  217. gateway: {{ route.gateway }}
  218. {%- endfor %}
  219. {%- endif %}
  220. {%- endfor %}
  221. {%- if network.bridge != 'none' %}
  222. linux_interfaces_final_include:
  223. file.prepend:
  224. - name: /etc/network/interfaces
  225. - text: |
  226. source /etc/network/interfaces.d/*
  227. # Workaround for Upstream-Bug: https://github.com/saltstack/salt/issues/40262
  228. source /etc/network/interfaces.u/*
  229. {%- endif %}
  230. {%- endif %}
  231. {%- if network.network_manager.disable is defined and network.network_manager.disable == True %}
  232. NetworkManager:
  233. service.dead:
  234. - enable: false
  235. {%- endif %}
  236. {%- if network.tap_custom_txqueuelen is defined %}
  237. /etc/udev/rules.d/60-net-txqueue.rules:
  238. file.managed:
  239. - source: salt://linux/files/60-net-txqueue.rules
  240. - mode: 755
  241. - template: jinja
  242. {%- endif %}