Saltstack Official Linux 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.

284 lines
7.6KB

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