| 1 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 2 |
from __future__ import (absolute_import, division, print_function) |
| 3 |
__metaclass__ = type |
| 4 |
|
| 5 |
DOCUMENTATION = r''' |
| 6 |
name: libvirt |
| 7 |
extends_documentation_fragment: |
| 8 |
- constructed |
| 9 |
- community.libvirt.requirements |
| 10 |
short_description: Libvirt inventory source |
| 11 |
description: |
| 12 |
- Get libvirt guests in an inventory source. |
| 13 |
author: |
| 14 |
- Dave Olsthoorn (@daveol) <[email protected]> |
| 15 |
version_added: "2.10.0" |
| 16 |
options: |
| 17 |
plugin: |
| 18 |
description: Token that ensures this is a source file for the 'libvirt' plugin. |
| 19 |
required: True |
| 20 |
choices: ['libvirt', 'community.libvirt.libvirt'] |
| 21 |
uri: |
| 22 |
description: Libvirt Connection URI |
| 23 |
required: True |
| 24 |
type: str |
| 25 |
inventory_hostname: |
| 26 |
description: | |
| 27 |
What to register as the inventory hostname. |
| 28 |
If set to 'uuid' the uuid of the server will be used and a |
| 29 |
group will be created for the server name. |
| 30 |
If set to 'name' the name of the server will be used unless |
| 31 |
there are more than one server with the same name in which |
| 32 |
case the 'uuid' logic will be used. |
| 33 |
Default is to do 'name'. |
| 34 |
type: string |
| 35 |
choices: |
| 36 |
- name |
| 37 |
- uuid |
| 38 |
default: "name" |
| 39 |
''' |
| 40 |
|
| 41 |
EXAMPLES = r''' |
| 42 |
# Connect to lxc host |
| 43 |
plugin: community.libvirt.libvirt |
| 44 |
uri: 'lxc:///' |
| 45 |
|
| 46 |
# Connect to qemu |
| 47 |
plugin: community.libvirt.libvirt |
| 48 |
uri: 'qemu:///system' |
| 49 |
''' |
| 50 |
|
| 51 |
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable |
| 52 |
from ansible.errors import AnsibleError |
| 53 |
from ansible.module_utils.six import raise_from |
| 54 |
|
| 55 |
try: |
| 56 |
import libvirt |
| 57 |
except ImportError as imp_exc: |
| 58 |
LIBVIRT_IMPORT_ERROR = imp_exc |
| 59 |
else: |
| 60 |
LIBVIRT_IMPORT_ERROR = None |
| 61 |
|
| 62 |
VIRDOMAINSTATE = ["nostate", "running", "blocked", "paused", "shutdown", "shutoff", "crashed", "pmsuspended", "last"] |
| 63 |
|
| 64 |
|
| 65 |
class InventoryModule(BaseInventoryPlugin, Constructable): |
| 66 |
NAME = 'community.libvirt.libvirt' |
| 67 |
|
| 68 |
def parse(self, inventory, loader, path, cache=True): |
| 69 |
if LIBVIRT_IMPORT_ERROR: |
| 70 |
raise_from( |
| 71 |
AnsibleError('libvirt python bindings must be installed to use this plugin'), |
| 72 |
LIBVIRT_IMPORT_ERROR) |
| 73 |
|
| 74 |
super(InventoryModule, self).parse( |
| 75 |
inventory, |
| 76 |
loader, |
| 77 |
path, |
| 78 |
cache=cache |
| 79 |
) |
| 80 |
|
| 81 |
config_data = self._read_config_data(path) |
| 82 |
|
| 83 |
# set _options from config data |
| 84 |
self._consume_options(config_data) |
| 85 |
|
| 86 |
uri = self.get_option('uri') |
| 87 |
if not uri: |
| 88 |
raise AnsibleError("hypervisor uri not given") |
| 89 |
|
| 90 |
connection = libvirt.open(uri) |
| 91 |
if not connection: |
| 92 |
raise AnsibleError("hypervisor connection failure") |
| 93 |
|
| 94 |
# TODO(daveol) |
| 95 |
# make using connection plugins optional |
| 96 |
connection_plugin = dict({ |
| 97 |
'LXC': 'community.libvirt.libvirt_lxc', |
| 98 |
'QEMU': 'community.libvirt.libvirt_qemu' |
| 99 |
}).get(connection.getType()) |
| 100 |
|
| 101 |
for server in connection.listAllDomains(): |
| 102 |
inventory_hostname = dict({ |
| 103 |
'uuid': server.UUIDString(), |
| 104 |
'name': server.name() |
| 105 |
}).get( |
| 106 |
self.get_option('inventory_hostname') |
| 107 |
) |
| 108 |
|
| 109 |
inventory_hostname_alias = dict({ |
| 110 |
'name': server.UUIDString(), |
| 111 |
'uuid': server.name() |
| 112 |
}).get( |
| 113 |
self.get_option('inventory_hostname') |
| 114 |
) |
| 115 |
|
| 116 |
# TODO(daveol): Fix "Invalid characters were found in group names" |
| 117 |
# This warning is generated because of uuid's |
| 118 |
self.inventory.add_host(inventory_hostname) |
| 119 |
self.inventory.add_group(inventory_hostname_alias) |
| 120 |
self.inventory.add_child(inventory_hostname_alias, inventory_hostname) |
| 121 |
|
| 122 |
if connection_plugin is not None: |
| 123 |
self.inventory.set_variable( |
| 124 |
inventory_hostname, |
| 125 |
'ansible_libvirt_uri', |
| 126 |
uri |
| 127 |
) |
| 128 |
self.inventory.set_variable( |
| 129 |
inventory_hostname, |
| 130 |
'ansible_connection', |
| 131 |
connection_plugin |
| 132 |
) |
| 133 |
|
| 134 |
try: |
| 135 |
domain = connection.lookupByUUIDString(server.UUIDString()) |
| 136 |
except libvirt.libvirtError as e: |
| 137 |
self.inventory.set_variable( |
| 138 |
inventory_hostname, |
| 139 |
'ERROR', |
| 140 |
str(e) |
| 141 |
) |
| 142 |
else: |
| 143 |
_domain_state, _domain_maxmem, _domain_mem, _domain_cpus, _domain_cput = domain.info() |
| 144 |
domain_info = {"state_number": _domain_state, |
| 145 |
"state": VIRDOMAINSTATE[_domain_state], |
| 146 |
"maxMem_kb": _domain_maxmem, |
| 147 |
"memory_kb": _domain_mem, |
| 148 |
"nrVirtCpu": _domain_cpus, |
| 149 |
"cpuTime_ns": _domain_cput} |
| 150 |
self.inventory.set_variable( |
| 151 |
inventory_hostname, |
| 152 |
'info', |
| 153 |
domain_info |
| 154 |
) |
| 155 |
|
| 156 |
self.inventory.set_variable( |
| 157 |
inventory_hostname, |
| 158 |
'xml_desc', |
| 159 |
domain.XMLDesc() |
| 160 |
) |
| 161 |
|
| 162 |
# This needs the guest powered on, 'qemu-guest-agent' installed and the org.qemu.guest_agent.0 channel configured. |
| 163 |
domain_guestInfo = '' |
| 164 |
try: |
| 165 |
# type==0 returns all types (users, os, timezone, hostname, filesystem, disks, interfaces) |
| 166 |
domain_guestInfo = domain.guestInfo(types=0) |
| 167 |
except libvirt.libvirtError as e: |
| 168 |
domain_guestInfo = {"error": str(e)} |
| 169 |
finally: |
| 170 |
self.inventory.set_variable( |
| 171 |
inventory_hostname, |
| 172 |
'guest_info', |
| 173 |
domain_guestInfo |
| 174 |
) |
| 175 |
|
| 176 |
# This needs the guest powered on, 'qemu-guest-agent' installed and the org.qemu.guest_agent.0 channel configured. |
| 177 |
domain_interfaceAddresses = '' |
| 178 |
try: |
| 179 |
domain_interfaceAddresses = domain.interfaceAddresses(source=libvirt.VIR_DOMAIN_INTERFACE_ADDRESSES_SRC_AGENT) |
| 180 |
except libvirt.libvirtError as e: |
| 181 |
domain_interfaceAddresses = {"error": str(e)} |
| 182 |
finally: |
| 183 |
self.inventory.set_variable( |
| 184 |
inventory_hostname, |
| 185 |
'interface_addresses', |
| 186 |
domain_interfaceAddresses |
| 187 |
) |
| 188 |
|
| 189 |
# Get variables for compose |
| 190 |
variables = self.inventory.hosts[inventory_hostname].get_vars() |
| 191 |
|
| 192 |
# Set composed variables |
| 193 |
self._set_composite_vars( |
| 194 |
self.get_option('compose'), |
| 195 |
variables, |
| 196 |
inventory_hostname, |
| 197 |
self.get_option('strict'), |
| 198 |
) |
| 199 |
|
| 200 |
# Add host to composed groups |
| 201 |
self._add_host_to_composed_groups( |
| 202 |
self.get_option('groups'), |
| 203 |
variables, |
| 204 |
inventory_hostname, |
| 205 |
self.get_option('strict'), |
| 206 |
) |
| 207 |
|
| 208 |
# Add host to keyed groups |
| 209 |
self._add_host_to_keyed_groups( |
| 210 |
self.get_option('keyed_groups'), |
| 211 |
variables, |
| 212 |
inventory_hostname, |
| 213 |
self.get_option('strict'), |
| 214 |
) |