Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for subnet changes and redeclare network if True #27

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions filter_plugins/xml2dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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 = {}

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):
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
52 changes: 52 additions & 0 deletions tasks/networks.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,56 @@
---
- 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 }}"
Expand Down