From 654aebea15fd458594789dec5787bb1feb7cb35c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20Erzno=C5=BEnik?= Date: Fri, 31 Jan 2020 18:10:34 +0100 Subject: [PATCH 1/3] Check for subnet changes and redeclare network if True --- filter_plugins/xml2dict.py | 23 ++++++++++++++++++ tasks/networks.yml | 48 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 filter_plugins/xml2dict.py diff --git a/filter_plugins/xml2dict.py b/filter_plugins/xml2dict.py new file mode 100644 index 0000000..6c30d8c --- /dev/null +++ b/filter_plugins/xml2dict.py @@ -0,0 +1,23 @@ +class FilterModule(object): + + def filters(self): + return { + 'xml2dict': self.xml2dict, + } + + def xml2dict(self, value): + from xml.etree import ElementTree + # return json.dumps(xmltodict.parse(value)) + res = {} + e = ElementTree.fromstring(value) + + def rp(root, result): + for element in root: + result[element.tag] = rp(element, {}) + result[element.tag].update(element.attrib) + if not result[element.tag]: + result[element.tag] = element.text + return result + + rp(e, res) + return res diff --git a/tasks/networks.yml b/tasks/networks.yml index 3c74095..55c1a5c 100644 --- a/tasks/networks.yml +++ b/tasks/networks.yml @@ -1,4 +1,52 @@ --- +- name: Get current state of libvirt networks + virt_net: + name: "{{ item.name }}" + command: get_xml + with_items: "{{ libvirt_host_networks }}" + become: True + register: qemu_stackhpc_net_xml + +- name: Convert state of libvirt networks to dict + set_fact: + qemu_stackhpc_net: "{{ qemu_stackhpc_net_xml.results | xml2dict }}" + +- name: Stop all misconfigured networks + virt_net: + name: "{{ item.name }}" + state: inactive + uri: "{{ libvirt_host_uri | default(omit, true) }}" + when: > + 'forward' in qemu_stackhpc_net[item.name] and + 'mode' in qemu_stackhpc_net[item.name].forward and + qemu_stackhpc_net[item.name].forward.mode == 'nat' and + ( + qemu_stackhpc_net[item.name].ip.address != item.ip or + qemu_stackhpc_net[item.name].ip.netmask != item.netmask or + (qemu_stackhpc_net[item.name].ip.dhcp.range.start if 'dhcp' in qemu_stackhpc_net[item.name].ip else '') != (item.dhcp_start if 'dhcp_start' in item else '') or + (qemu_stackhpc_net[item.name].ip.dhcp.range.end if 'dhcp' in qemu_stackhpc_net[item.name].ip else '') != (item.dhcp_end if 'dhcp_end' in item else '') + ) + with_items: "{{ libvirt_host_networks }}" + become: True + +- name: Remove all misconfigured networks + virt_net: + name: "{{ item.name }}" + command: undefine + when: > + 'forward' in qemu_stackhpc_net[item.name] and + 'mode' in qemu_stackhpc_net[item.name].forward and + qemu_stackhpc_net[item.name].forward.mode == 'nat' and + ( + qemu_stackhpc_net[item.name].ip.address != item.ip or + qemu_stackhpc_net[item.name].ip.netmask != item.netmask or + (qemu_stackhpc_net[item.name].ip.dhcp.range.start if 'dhcp' in qemu_stackhpc_net[item.name].ip else '') != (item.dhcp_start if 'dhcp_start' in item else '') or + (qemu_stackhpc_net[item.name].ip.dhcp.range.end if 'dhcp' in qemu_stackhpc_net[item.name].ip else '') != (item.dhcp_end if 'dhcp_end' in item else '') + ) + with_items: "{{ libvirt_host_networks }}" + become: True + + - name: Ensure libvirt networks are defined virt_net: name: "{{ item.name }}" From a5c14cadc64ca9264aa4117771e58fd7a5a64e4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20Erzno=C5=BEnik?= Date: Sat, 1 Feb 2020 06:37:32 +0100 Subject: [PATCH 2/3] Fix whitespace and long lines --- tasks/networks.yml | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/tasks/networks.yml b/tasks/networks.yml index 55c1a5c..e33fb53 100644 --- a/tasks/networks.yml +++ b/tasks/networks.yml @@ -17,14 +17,16 @@ state: inactive uri: "{{ libvirt_host_uri | default(omit, true) }}" when: > - 'forward' in qemu_stackhpc_net[item.name] and - 'mode' in qemu_stackhpc_net[item.name].forward and + 'forward' in qemu_stackhpc_net[item.name] and + 'mode' in qemu_stackhpc_net[item.name].forward and qemu_stackhpc_net[item.name].forward.mode == 'nat' and ( qemu_stackhpc_net[item.name].ip.address != item.ip or qemu_stackhpc_net[item.name].ip.netmask != item.netmask or - (qemu_stackhpc_net[item.name].ip.dhcp.range.start if 'dhcp' in qemu_stackhpc_net[item.name].ip else '') != (item.dhcp_start if 'dhcp_start' in item else '') or - (qemu_stackhpc_net[item.name].ip.dhcp.range.end if 'dhcp' in qemu_stackhpc_net[item.name].ip else '') != (item.dhcp_end if 'dhcp_end' in item else '') + (qemu_stackhpc_net[item.name].ip.dhcp.range.start if 'dhcp' in qemu_stackhpc_net[item.name].ip else '') != + (item.dhcp_start if 'dhcp_start' in item else '') or + (qemu_stackhpc_net[item.name].ip.dhcp.range.end if 'dhcp' in qemu_stackhpc_net[item.name].ip else '') != + (item.dhcp_end if 'dhcp_end' in item else '') ) with_items: "{{ libvirt_host_networks }}" become: True @@ -34,14 +36,16 @@ name: "{{ item.name }}" command: undefine when: > - 'forward' in qemu_stackhpc_net[item.name] and - 'mode' in qemu_stackhpc_net[item.name].forward and + 'forward' in qemu_stackhpc_net[item.name] and + 'mode' in qemu_stackhpc_net[item.name].forward and qemu_stackhpc_net[item.name].forward.mode == 'nat' and ( qemu_stackhpc_net[item.name].ip.address != item.ip or qemu_stackhpc_net[item.name].ip.netmask != item.netmask or - (qemu_stackhpc_net[item.name].ip.dhcp.range.start if 'dhcp' in qemu_stackhpc_net[item.name].ip else '') != (item.dhcp_start if 'dhcp_start' in item else '') or - (qemu_stackhpc_net[item.name].ip.dhcp.range.end if 'dhcp' in qemu_stackhpc_net[item.name].ip else '') != (item.dhcp_end if 'dhcp_end' in item else '') + (qemu_stackhpc_net[item.name].ip.dhcp.range.start if 'dhcp' in qemu_stackhpc_net[item.name].ip else '') != + (item.dhcp_start if 'dhcp_start' in item else '') or + (qemu_stackhpc_net[item.name].ip.dhcp.range.end if 'dhcp' in qemu_stackhpc_net[item.name].ip else '') != + (item.dhcp_end if 'dhcp_end' in item else '') ) with_items: "{{ libvirt_host_networks }}" become: True From 15b67e09aa471c55a70bd96ef672a15da7afa6b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20Erzno=C5=BEnik?= Date: Sat, 1 Feb 2020 07:58:52 +0100 Subject: [PATCH 3/3] Update for filter to convert lists too --- filter_plugins/xml2dict.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/filter_plugins/xml2dict.py b/filter_plugins/xml2dict.py index 6c30d8c..2501365 100644 --- a/filter_plugins/xml2dict.py +++ b/filter_plugins/xml2dict.py @@ -9,6 +9,13 @@ def xml2dict(self, value): from xml.etree import ElementTree # return json.dumps(xmltodict.parse(value)) res = {} + + if isinstance(value, list): + for item in value: + e = self.xml2dict(item['get_xml']) + res[e['name']] = e + return res + e = ElementTree.fromstring(value) def rp(root, result):