xci: bootstrap-host: Make active network interface consistent 81/54581/1
authorMarkos Chandras <mchandras@suse.de>
Wed, 28 Mar 2018 15:10:28 +0000 (16:10 +0100)
committerMarkos Chandras <mchandras@suse.de>
Wed, 28 Mar 2018 21:05:21 +0000 (22:05 +0100)
When we run XCI for the first time, Ansible picks the first active
interface as the default one. However, after we configure all the XCI
bridges etc, and we try to run this role again, Ansible may have changed
its mind about what interface is active and it could default to one of
the bridges. This forces the role to redo the network configuration but
this time the bridges are being attached to bridges so everything goes
terribly wrong after that. The way to solve this would be to add a local
fact about what interface should be considered as the 'real' default one
so subsequent calls to this role to not destroy the network.

This also drops the task which removed the network configuration files
on SUSE platforms since Ansible is smart enough to not touch them if
they are configured properly.

Change-Id: Ic0525e934b1934a40d69e6cf977615ab9b3dac6d
Signed-off-by: Markos Chandras <mchandras@suse.de>
xci/playbooks/roles/bootstrap-host/handlers/main.yml [new file with mode: 0644]
xci/playbooks/roles/bootstrap-host/tasks/network.yml

diff --git a/xci/playbooks/roles/bootstrap-host/handlers/main.yml b/xci/playbooks/roles/bootstrap-host/handlers/main.yml
new file mode 100644 (file)
index 0000000..b910323
--- /dev/null
@@ -0,0 +1,12 @@
+---
+# SPDX-license-identifier: Apache-2.0
+##############################################################################
+# Copyright (c) 2018 SUSE Linux GmbH and others.
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+- name: Reload facts
+  setup:
+    filter: ansible_local
index ea9060e..92e9195 100644 (file)
     name: "{{ network_packages }}"
     state: present
 
+- name: Ensure local facts directory exists
+  file:
+    path: "/etc/ansible/facts.d"
+    state: directory
+
+# NOTE(hwoarang) We have to check all levels of the local fact before we add it
+# otherwise Ansible will fail.
+- name: Record initial active interface
+  ini_file:
+    create: yes
+    section: network
+    state: present
+    option: xci_interface
+    value:  "{{ ansible_default_ipv4.interface }}"
+    path: "/etc/ansible/facts.d/xci.fact"
+  when: ansible_local is not defined
+    or (ansible_local is defined and ansible_local.xci is not defined)
+    or (ansible_local is defined and ansible_local.xci is defined and ansible_local.xci.network is not defined)
+    or (ansible_local is defined and ansible_local.xci is defined and ansible_local.xci.network is defined and ansible_local.xci.network.xci_interface is not defined)
+  notify:
+    - Reload facts
+
+- name: Run handlers
+  meta: flush_handlers
+
 - block:
     - name: configure modules
       lineinfile:
         src: "{{ ansible_os_family | lower }}/{{ ansible_hostname }}.interface.j2"
         dest: "/etc/network/interfaces"
     - name: restart network service
-      shell: "/sbin/ifconfig {{ ansible_default_ipv4.interface }} 0 && /sbin/ifdown -a && /sbin/ifup -a"
+      shell: "/sbin/ifconfig {{ ansible_local.xci.network.xci_interface }} 0 && /sbin/ifdown -a && /sbin/ifup -a"
       async: 15
       poll: 0
   when: ansible_os_family | lower == "debian"
 
 - block:
-    - name: Remove existing network configuration
-      file:
-        path: "/etc/sysconfig/network/{{ item }}"
-        state: absent
-      with_items:
-        - "ifcfg-eth0"
-        - "ifroute-eth0"
-
     - name: Configure networking on SUSE
       template:
         src: "{{ ansible_os_family | lower }}/suse.interface.j2"
         dest: "/etc/sysconfig/network/ifcfg-{{ item.name }}"
       with_items:
-        - { name: "{{ ansible_default_ipv4.interface }}" }
-        - { name: "{{ ansible_default_ipv4.interface }}.10", vlan_id: 10 }
-        - { name: "{{ ansible_default_ipv4.interface }}.30", vlan_id: 30 }
-        - { name: "{{ ansible_default_ipv4.interface }}.20", vlan_id: 20 }
-        - { name: "br-mgmt", bridge_ports: "{{ ansible_default_ipv4.interface }}.10", ip: "{{ host_info[inventory_hostname].MGMT_IP }}/22" }
-        - { name: "br-vxlan", bridge_ports: "{{ ansible_default_ipv4.interface }}.30", ip: "{{ host_info[inventory_hostname].VXLAN_IP }}/22" }
-        - { name: "br-vlan", bridge_ports: "{{ ansible_default_ipv4.interface }}", ip: "{{ host_info[inventory_hostname].VLAN_IP }}/24" }
-        - { name: "br-storage", bridge_ports: "{{ ansible_default_ipv4.interface }}.20", ip: "{{ host_info[inventory_hostname].STORAGE_IP }}/22" }
+        - { name: "{{ ansible_local.xci.network.xci_interface }}" }
+        - { name: "{{ ansible_local.xci.network.xci_interface }}.10", vlan_id: 10 }
+        - { name: "{{ ansible_local.xci.network.xci_interface }}.30", vlan_id: 30 }
+        - { name: "{{ ansible_local.xci.network.xci_interface }}.20", vlan_id: 20 }
+        - { name: "br-mgmt", bridge_ports: "{{ ansible_local.xci.network.xci_interface }}.10", ip: "{{ host_info[inventory_hostname].MGMT_IP }}/22" }
+        - { name: "br-vxlan", bridge_ports: "{{ ansible_local.xci.network.xci_interface }}.30", ip: "{{ host_info[inventory_hostname].VXLAN_IP }}/22" }
+        - { name: "br-vlan", bridge_ports: "{{ ansible_local.xci.network.xci_interface }}", ip: "{{ host_info[inventory_hostname].VLAN_IP }}/24" }
+        - { name: "br-storage", bridge_ports: "{{ ansible_local.xci.network.xci_interface }}.20", ip: "{{ host_info[inventory_hostname].STORAGE_IP }}/22" }
 
     - name: Add postup/postdown scripts on SUSE
       copy:
         src: "{{ ansible_os_family | lower }}/interface.ifcfg.j2"
         dest: "/etc/sysconfig/network-scripts/ifcfg-{{ item.name }}"
       with_items:
-        - { name: "{{ ansible_default_ipv4.interface }}"   , bridge: "br-vlan"                 }
-        - { name: "{{ ansible_default_ipv4.interface }}.10", bridge: "br-mgmt"   , vlan_id: 10 }
-        - { name: "{{ ansible_default_ipv4.interface }}.20", bridge: "br-storage", vlan_id: 20 }
-        - { name: "{{ ansible_default_ipv4.interface }}.30", bridge: "br-vxlan"  , vlan_id: 30 }
+        - { name: "{{ ansible_local.xci.network.xci_interface }}"   , bridge: "br-vlan"                 }
+        - { name: "{{ ansible_local.xci.network.xci_interface }}.10", bridge: "br-mgmt"   , vlan_id: 10 }
+        - { name: "{{ ansible_local.xci.network.xci_interface }}.20", bridge: "br-storage", vlan_id: 20 }
+        - { name: "{{ ansible_local.xci.network.xci_interface }}.30", bridge: "br-vxlan"  , vlan_id: 30 }
     - name: Configure networking on CentOS for bridges
       template:
         src: "{{ ansible_os_family | lower }}/bridge.ifcfg.j2"