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.

285 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 %}
  30. add_int_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. - unless: grep -q "iface {{ interface_name }} " /etc/network/interfaces
  93. - proto: {{ interface.get('proto', 'static') }}
  94. {% endif %}
  95. {%- if grains.os_family == 'RedHat' %}
  96. {%- if interface.get('proto', 'none') == 'manual' %}
  97. - proto: 'none'
  98. {%- else %}
  99. - proto: {{ interface.get('proto', 'none') }}
  100. {%- endif %}
  101. {% endif %}
  102. - ipaddr: {{ interface.address }}
  103. - netmask: {{ interface.netmask }}
  104. {%- else %}
  105. - proto: {{ interface.get('proto', 'dhcp') }}
  106. {%- endif %}
  107. {%- if interface.name_servers is defined %}
  108. - dns: {{ interface.name_servers }}
  109. {%- endif %}
  110. {%- if interface.wireless is defined and grains.os_family == 'Debian' %}
  111. {%- if interface.wireless.security == "wpa" %}
  112. - wpa-ssid: {{ interface.wireless.essid }}
  113. - wpa-psk: {{ interface.wireless.key }}
  114. {%- else %}
  115. - wireless-ssid: {{ interface.wireless.essid }}
  116. - wireless-psk: {{ interface.wireless.key }}
  117. {%- endif %}
  118. {%- endif %}
  119. {%- for param in network.interface_params %}
  120. {{ set_param(param, interface) }}
  121. {%- endfor %}
  122. {%- if interface.type == 'bridge' %}
  123. - bridge: {{ interface_name }}
  124. - delay: 0
  125. - bypassfirewall: True
  126. - use:
  127. {%- for network in interface.use_interfaces %}
  128. - network: linux_interface_{{ network }}
  129. {%- endfor %}
  130. - ports: {% for network in interface.get('use_interfaces', []) %}{{ network }} {% endfor %}{% for network in interface.get('use_ovs_ports', []) %}{{ network }} {% endfor %}
  131. - require:
  132. {%- for network in interface.get('use_interfaces', []) %}
  133. - network: linux_interface_{{ network }}
  134. {%- endfor %}
  135. {%- for network in interface.get('use_ovs_ports', []) %}
  136. - cmd: ovs_port_up_{{ network }}
  137. {%- endfor %}
  138. {%- endif %}
  139. {%- if interface.type == 'bond' %}
  140. - slaves: {{ interface.slaves }}
  141. - mode: {{ interface.mode }}
  142. {%- endif %}
  143. {%- for network in interface.get('use_ovs_ports', []) %}
  144. remove_interface_{{ network }}_line1:
  145. file.replace:
  146. - name: /etc/network/interfaces
  147. - pattern: auto {{ network }}
  148. - repl: ""
  149. remove_interface_{{ network }}_line2:
  150. file.replace:
  151. - name: /etc/network/interfaces
  152. - pattern: iface {{ network }} inet manual
  153. - repl: ""
  154. {%- endfor %}
  155. {%- if interface.gateway is defined %}
  156. linux_system_network:
  157. network.system:
  158. - enabled: {{ interface.enabled }}
  159. - hostname: {{ network.fqdn }}
  160. {%- if interface.gateway is defined %}
  161. - gateway: {{ interface.gateway }}
  162. - gatewaydev: {{ interface_name }}
  163. {%- endif %}
  164. - nozeroconf: True
  165. - nisdomain: {{ system.domain }}
  166. - require_reboot: False
  167. {%- endif %}
  168. {%- endif %}
  169. {%- endif %}
  170. {%- if interface.wireless is defined %}
  171. {%- if grains.os_family == 'Arch' %}
  172. linux_network_packages:
  173. pkg.installed:
  174. - names: {{ network.pkgs }}
  175. /etc/netctl/network_{{ interface.wireless.essid }}:
  176. file.managed:
  177. - source: salt://linux/files/wireless
  178. - mode: 755
  179. - template: jinja
  180. - require:
  181. - pkg: linux_network_packages
  182. - defaults:
  183. interface_name: {{ interface_name }}
  184. switch_profile_{{ interface.wireless.essid }}:
  185. cmd.run:
  186. - name: netctl switch-to network_{{ interface.wireless.essid }}
  187. - cwd: /root
  188. - unless: "iwconfig {{ interface_name }} | grep -e 'ESSID:\"{{ interface.wireless.essid }}\"'"
  189. - require:
  190. - file: /etc/netctl/network_{{ interface.wireless.essid }}
  191. enable_profile_{{ interface.wireless.essid }}:
  192. cmd.run:
  193. - name: netctl enable network_{{ interface.wireless.essid }}
  194. - cwd: /root
  195. - unless: test -e /etc/systemd/system/multi-user.target.wants/netctl@network_{{ interface.wireless.essid }}.service
  196. - require:
  197. - file: /etc/netctl/network_{{ interface.wireless.essid }}
  198. {%- endif %}
  199. {%- endif %}
  200. {%- endif %}
  201. {%- if interface.route is defined %}
  202. linux_network_{{ interface_name }}_routes:
  203. network.routes:
  204. - name: {{ interface_name }}
  205. - routes:
  206. {%- for route_name, route in interface.route.iteritems() %}
  207. - name: {{ route_name }}
  208. ipaddr: {{ route.address }}
  209. netmask: {{ route.netmask }}
  210. gateway: {{ route.gateway }}
  211. {%- endfor %}
  212. {%- endif %}
  213. {%- endfor %}
  214. {%- if network.bridge != 'none' %}
  215. linux_interfaces_final_include:
  216. file.prepend:
  217. - name: /etc/network/interfaces
  218. - text: 'source /etc/network/interfaces.d/*'
  219. {%- endif %}
  220. {%- endif %}
  221. {%- if network.network_manager.disable is defined and network.network_manager.disable == True %}
  222. NetworkManager:
  223. service.dead:
  224. - enable: false
  225. {%- endif %}