'Construct the inventory'

def get_inventory(self):

    inventory = dict(_meta=dict(hostvars=dict()))
    conn = libvirt.openReadOnly(self.libvirt_uri)
    if (conn is None):
        print ('Failed    to    open    connection    to    %s' % self.libvirt_uri)
        sys.exit(1)
    domains = conn.listAllDomains()
    if (domains is None):
        print ('Failed    to    list    domains    for    connection    %s' % self.libvirt_uri)
        sys.exit(1)
    for domain in domains:
        hostvars = dict(libvirt_name=domain.name(), libvirt_id=domain.ID(), libvirt_uuid=domain.UUIDString())
        domain_name = domain.name()
        (state, _) = domain.state()
        if (state != 1):
            continue
        hostvars['libvirt_status'] = 'running'
        root = ET.fromstring(domain.XMLDesc())
        ansible_ns = {'ansible': 'https://github.com/ansible/ansible'}
        for tag_elem in root.findall('./metadata/ansible:tags/ansible:tag', ansible_ns):
            tag = tag_elem.text
            _push(inventory, ('tag_%s' % tag), domain_name)
            _push(hostvars, 'libvirt_tags', tag)
        interface = root.find("./devices/interface[@type='network']")
        if (interface is not None):
            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
        inventory['_meta']['hostvars'][domain_name] = hostvars
    return inventory
