diff options
author | Lénaïc Huard <lhuard@amadeus.com> | 2015-05-20 13:26:04 +0200 |
---|---|---|
committer | Lénaïc Huard <lhuard@amadeus.com> | 2015-05-22 08:42:01 +0200 |
commit | 4e8f6d1923782e7aef863441f7c1434ca1029164 (patch) | |
tree | 9edd8d1637b5e95dea5c7d39e38b092fe7a6cc08 /inventory/libvirt | |
parent | 09717b94a0b76d03db38baee793b141e1a651cdd (diff) | |
download | openshift-4e8f6d1923782e7aef863441f7c1434ca1029164.tar.gz openshift-4e8f6d1923782e7aef863441f7c1434ca1029164.tar.bz2 openshift-4e8f6d1923782e7aef863441f7c1434ca1029164.tar.xz openshift-4e8f6d1923782e7aef863441f7c1434ca1029164.zip |
[libvirt cluster] Use net-dhcp-leases to find VMs’ IPs
Query libvirt’s DHCP leases rather than inspecting the host’s ARP cache
to find the VMs’ IPs.
Diffstat (limited to 'inventory/libvirt')
-rwxr-xr-x | inventory/libvirt/hosts/libvirt_generic.py | 26 |
1 files changed, 7 insertions, 19 deletions
diff --git a/inventory/libvirt/hosts/libvirt_generic.py b/inventory/libvirt/hosts/libvirt_generic.py index 4652f112e..d1e1069f9 100755 --- a/inventory/libvirt/hosts/libvirt_generic.py +++ b/inventory/libvirt/hosts/libvirt_generic.py @@ -113,8 +113,6 @@ class LibvirtInventory(object): print "Failed to list domains for connection %s" % libvirt_uri sys.exit(1) - arp_entries = self.parse_arp_entries() - for domain in domains: hostvars = dict(libvirt_name=domain.name(), libvirt_id=domain.ID(), @@ -140,11 +138,13 @@ class LibvirtInventory(object): # interface types other than 'network' interface = root.find("./devices/interface[@type='network']") if interface is not None: - mac_elem = interface.find('mac') - if mac_elem is not None: - mac = mac_elem.get('address') - if mac in arp_entries: - ip_address = arp_entries[mac]['ip_address'] + source_elem = interface.find('source') + mac_elem = interface.find('mac') + if source_elem is not None and \ + mac_elem is not None: + dhcp_leases = conn.networkLookupByName(source_elem.get('network')).DHCPLeases(mac_elem.get('address')) + if len(dhcp_leases) > 0: + ip_address = dhcp_leases[0]['ipaddr'] hostvars['ansible_ssh_host'] = ip_address hostvars['libvirt_ip_address'] = ip_address @@ -152,18 +152,6 @@ class LibvirtInventory(object): return inventory - def parse_arp_entries(self): - arp_entries = dict() - with open('/proc/net/arp', 'r') as f: - # throw away the header - f.readline() - - for line in f: - ip_address, _, _, mac, _, device = line.strip().split() - arp_entries[mac] = dict(ip_address=ip_address, device=device) - - return arp_entries - def push(self, my_dict, key, element): if key in my_dict: my_dict[key].append(element) |