Browse Source

Add host only network option

The host only network function was adding a global network address and a
host only network address which would flip flop should networkd or the
nspawn-macvlan service be restarted. This change makes resolves the flip
flopping and adds an option to enable this functionality when required.

Change-Id: Ia856723580d345d01e0df06ac41adb9cb7fcdb24
Signed-off-by: Kevin Carter <kevin.carter@rackspace.com>
Kevin Carter 6 months ago
parent
commit
e873bda7d6
No account linked to committer's email address
2 changed files with 21 additions and 17 deletions
  1. 2
    0
      defaults/main.yml
  2. 19
    17
      tasks/nspawn_networking.yml

+ 2
- 0
defaults/main.yml View File

@@ -35,6 +35,8 @@ nspawn_networks:
35 35
     macvlan_mode: bridge
36 36
     # Optional | set the routed interface
37 37
     routed_interface: mv-nspawn0
38
+    # Optional | Force address space to be scoped to only the host
39
+    host_only: true
38 40
 
39 41
 # Primary interface used for host to container communications. In the event that
40 42
 # the underlying system is running a condensed network stack a route will be

+ 19
- 17
tasks/nspawn_networking.yml View File

@@ -103,7 +103,7 @@
103 103
           {%        if hostvars[inventory_hostname][key.split('_')[0] + '_cidr'] is defined and (value.address is undefined) %}
104 104
           {%          set net_cidr = hostvars[inventory_hostname][key.split('_')[0] + '_cidr'] %}
105 105
           {%          set _ = start_commands.append('-/sbin/ip route add ' + net_cidr + ' dev ' + (value.routed_interface | default(nspawn_primary_interface)) + ' metric 100 proto kernel scope link table local') %}
106
-          {%        elif (value.address is defined) and ((interface_data['ipv4'] | default({'address': none}))['address'] != value.address) %}
106
+          {%        elif (value.address is defined) and ((interface_data['ipv4'] | default({'address': none}))['address'] != value.address) and ((value.host_only | default(false)) | bool) %}
107 107
           {%          set _ = start_commands.append('-/sbin/ip address add ' + value.address + '/' + (value.netmask | default('32')) + ' dev ' + mv_interface + ' scope host') %}
108 108
           {%-       endif %}
109 109
           {%-     endif %}
@@ -144,24 +144,26 @@
144 144
       {% for _, value in (nspawn_networks | combine(container_extra_networks)).items() %}
145 145
       {%   set netname = value.interface | default('mv-' + value.bridge.split('br-')[-1]) %}
146 146
       {%   set _network = {'interface': netname} %}
147
-      {%   if netname not in seen_networks %}
148
-      {%     set _ = seen_networks.append(netname) %}
149
-      {%     if value.address is defined %}
150
-      {%       set _ = _network.__setitem__('address', value.address) %}
151
-      {%       if (value.netmask is defined) and (_network.address != 'dhcp') %}
152
-      {%         set _ = _network.__setitem__('netmask', value.netmask) %}
153
-      {%         set prefix = (value.address + '/' + value.netmask) | ipaddr('prefix') %}
154
-      {%         set _ = _network.__setitem__('address', [value.address + '/' + prefix | string]) %}
147
+      {%   if (not (value.host_only | default(false)) | bool) %}
148
+      {%     if netname not in seen_networks %}
149
+      {%       set _ = seen_networks.append(netname) %}
150
+      {%       if value.address is defined %}
151
+      {%           set _ = _network.__setitem__('address', value.address) %}
152
+      {%           if (value.netmask is defined) and (_network.address != 'dhcp') %}
153
+      {%             set _ = _network.__setitem__('netmask', value.netmask) %}
154
+      {%             set prefix = (value.address + '/' + value.netmask) | ipaddr('prefix') %}
155
+      {%             set _ = _network.__setitem__('address', [value.address + '/' + prefix | string]) %}
156
+      {%           endif %}
155 157
       {%       endif %}
158
+      {%       set _ = _network.__setitem__('usedns', (value.usedns | default(true) | bool) | ternary('yes', 'no')) %}
159
+      {%       set _ = _network.__setitem__('static_routes', value.static_routes | default([])) %}
160
+      {%       if value.gateway is defined %}
161
+      {%         set _ = _network.__setitem__('gateway', value.gateway) %}
162
+      {%       endif %}
163
+      {%       set _ = _network.__setitem__('mtu', value.mtu | default(1500 | string)) %}
164
+      {%       set _ = _network.__setitem__('config_overrides', {'Network': {'IPForward': 'yes', 'IPMasquerade': 'yes'}, 'Link': {'ARP': 'yes'}}) %}
165
+      {%       set _ = _networks.append(_network) %}
156 166
       {%     endif %}
157
-      {%     set _ = _network.__setitem__('usedns', (value.usedns | default(true) | bool) | ternary('yes', 'no')) %}
158
-      {%     set _ = _network.__setitem__('static_routes', value.static_routes | default([])) %}
159
-      {%     if value.gateway is defined %}
160
-      {%       set _ = _network.__setitem__('gateway', value.gateway) %}
161
-      {%     endif %}
162
-      {%     set _ = _network.__setitem__('mtu', value.mtu | default(1500 | string)) %}
163
-      {%     set _ = _network.__setitem__('config_overrides', {'Network': {'IPForward': 'yes', 'IPMasquerade': 'yes'}, 'Link': {'ARP': 'yes'}}) %}
164
-      {%     set _ = _networks.append(_network) %}
165 167
       {%   endif %}
166 168
       {% endfor %}
167 169
       {{ _networks | sort(attribute='interface') }}

Loading…
Cancel
Save