xoxys.general/plugins/inventory/proxmox.py

299 lines
10 KiB
Python
Raw Normal View History

2020-08-18 13:10:09 +00:00
# Copyright (c) 2014, Mathieu GAUTHIER-LAFAYE <gauthierl@lapth.cnrs.fr>
# Copyright (c) 2016, Matt Harris <matthaeus.harris@gmail.com>
# Copyright (c) 2020, Robert Kaussow <mail@thegeeklab.de>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
2020-08-18 21:44:49 +00:00
"""Dynamic inventory plugin for Proxmox VE."""
2020-08-18 13:10:09 +00:00
2023-01-30 13:20:45 +00:00
from __future__ import (absolute_import, division, print_function)
2020-08-18 13:10:09 +00:00
__metaclass__ = type
DOCUMENTATION = """
name: proxmox
plugin_type: inventory
short_description: Proxmox VE inventory source
2023-01-30 13:20:45 +00:00
version_added: 1.1.0
2020-08-18 13:10:09 +00:00
description:
- Get inventory hosts from the proxmox service.
2020-09-04 20:57:25 +00:00
- "Uses a configuration file as an inventory source, it must end in C(.proxmox.yml) or C(.proxmox.yaml) and has a C(plugin: xoxys.general.proxmox) entry."
2020-08-18 13:10:09 +00:00
extends_documentation_fragment:
- inventory_cache
options:
plugin:
2020-09-04 20:57:25 +00:00
description: The name of this plugin, it should always be set to C(xoxys.general.proxmox) for this plugin to recognize it as it's own.
2020-08-18 13:10:09 +00:00
required: yes
choices: ["xoxys.general.proxmox"]
server:
description: Proxmox VE server url.
default: "pve.example.com"
2020-09-04 20:57:25 +00:00
type: string
2020-08-18 13:10:09 +00:00
required: yes
env:
- name: PROXMOX_SERVER
user:
description: Proxmox VE authentication user.
2020-09-04 20:57:25 +00:00
type: string
2020-08-18 13:10:09 +00:00
required: yes
env:
- name: PROXMOX_USER
password:
2020-09-04 20:57:25 +00:00
description: Proxmox VE authentication password.
type: string
2020-08-18 13:10:09 +00:00
required: yes
env:
- name: PROXMOX_PASSWORD
2020-09-04 20:57:25 +00:00
verify_ssl:
description: Skip SSL certificate verification.
type: boolean
2023-01-30 13:20:45 +00:00
default: True
2020-09-04 20:57:25 +00:00
auth_timeout:
description: Proxmox VE authentication timeout.
type: int
default: 5
2020-08-18 13:10:09 +00:00
exclude_vmid:
2020-09-04 20:57:25 +00:00
description: VMID's to exclude from inventory.
2020-08-18 13:10:09 +00:00
type: list
default: []
elements: str
exclude_state:
2020-09-04 20:57:25 +00:00
description: VM states to exclude from inventory.
2020-08-18 13:10:09 +00:00
type: list
default: []
elements: str
group:
2020-09-04 20:57:25 +00:00
description: Group to place all hosts into.
type: string
2020-08-18 13:10:09 +00:00
default: proxmox
want_facts:
description: Toggle, if C(true) the plugin will retrieve host facts from the server
type: boolean
2023-01-30 13:20:45 +00:00
default: True
""" # noqa
2020-08-18 13:10:09 +00:00
EXAMPLES = """
# proxmox.yml
2020-08-18 15:52:43 +00:00
plugin: xoxys.general.proxmox
2020-08-18 13:10:09 +00:00
server: pve.example.com
user: admin@pve
password: secure
"""
import json
import re
import socket
2023-01-30 13:20:45 +00:00
from collections import defaultdict
from distutils.version import LooseVersion
2020-08-18 13:10:09 +00:00
from ansible.errors import AnsibleError
from ansible.module_utils._text import to_native
2020-09-04 20:57:25 +00:00
from ansible.module_utils.parsing.convert_bool import boolean
2020-08-18 13:10:09 +00:00
from ansible.module_utils.six import iteritems
from ansible.plugins.inventory import BaseInventoryPlugin
try:
from proxmoxer import ProxmoxAPI
HAS_PROXMOXER = True
except ImportError:
HAS_PROXMOXER = False
2023-01-30 13:20:45 +00:00
try:
from requests.packages import urllib3
HAS_URLLIB3 = True
except ImportError:
try:
import urllib3
HAS_URLLIB3 = True
except ImportError:
HAS_URLLIB3 = False
2020-08-18 13:10:09 +00:00
class InventoryModule(BaseInventoryPlugin):
2023-01-30 13:20:45 +00:00
"""Provide Proxmox VE inventory."""
2020-08-18 15:52:43 +00:00
NAME = "xoxys.general.proxmox"
2020-08-18 13:10:09 +00:00
def _auth(self):
2023-01-30 13:20:45 +00:00
verify_ssl = boolean(self.get_option("verify_ssl"), strict=False)
if not verify_ssl and HAS_URLLIB3:
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
2020-08-18 13:10:09 +00:00
return ProxmoxAPI(
self.get_option("server"),
user=self.get_option("user"),
password=self.get_option("password"),
2023-01-30 13:20:45 +00:00
verify_ssl=verify_ssl,
2020-09-04 20:57:25 +00:00
timeout=self.get_option("auth_timeout")
2020-08-18 13:10:09 +00:00
)
def _get_version(self):
return LooseVersion(self.client.version.get()["version"])
def _get_major(self):
return LooseVersion(self.client.version.get()["release"])
def _get_names(self, pve_list, pve_type):
if pve_type == "node":
2023-01-30 13:20:45 +00:00
return [node["node"] for node in pve_list]
if pve_type == "pool":
return [pool["poolid"] for pool in pve_list]
2020-08-18 13:10:09 +00:00
2023-01-30 13:20:45 +00:00
return []
2020-08-18 13:10:09 +00:00
def _get_variables(self, pve_list, pve_type):
variables = {}
if pve_type in ["qemu", "container"]:
for vm in pve_list:
nested = {}
for key, value in iteritems(vm):
nested["proxmox_" + key] = value
variables[vm["name"]] = nested
return variables
def _get_ip_address(self, pve_type, pve_node, vmid):
def validate(address):
try:
# IP address validation
2023-01-30 13:20:45 +00:00
if socket.inet_aton(address) and address != "127.0.0.1":
return address
except OSError:
2020-08-18 13:10:09 +00:00
return False
address = False
networks = False
if pve_type == "qemu":
# If qemu agent is enabled, try to gather the IP address
try:
if self.client.nodes(pve_node).get(pve_type, vmid, "agent", "info") is not None:
networks = self.client.nodes(pve_node).get(
"qemu", vmid, "agent", "network-get-interfaces"
)["result"]
2023-01-30 13:20:45 +00:00
except Exception: # noqa
2020-08-18 13:10:09 +00:00
pass
2023-01-30 13:20:45 +00:00
if networks and type(networks) is list:
for network in networks:
for ip_address in network["ip-addresses"]:
address = validate(ip_address["ip-address"])
2020-08-18 13:10:09 +00:00
else:
try:
config = self.client.nodes(pve_node).get(pve_type, vmid, "config")
address = re.search(r"ip=(\d*\.\d*\.\d*\.\d*)", config["net0"]).group(1)
2023-01-30 13:20:45 +00:00
except Exception: # noqa
2020-08-18 13:10:09 +00:00
pass
return address
def _exclude(self, pve_list):
filtered = []
for item in pve_list:
obj = defaultdict(dict, item)
if obj["template"] == 1:
continue
if obj["status"] in self.get_option("exclude_state"):
continue
if obj["vmid"] in self.get_option("exclude_vmid"):
continue
filtered.append(item.copy())
return filtered
def _propagate(self):
for node in self._get_names(self.client.nodes.get(), "node"):
try:
qemu_list = self._exclude(self.client.nodes(node).qemu.get())
container_list = self._exclude(self.client.nodes(node).lxc.get())
2023-01-30 13:20:45 +00:00
except Exception as e: # noqa
raise AnsibleError(f"Proxmoxer API error: {to_native(e)}") from e
2020-08-18 13:10:09 +00:00
# Merge QEMU and Containers lists from this node
2020-08-18 15:52:43 +00:00
instances = self._get_variables(qemu_list, "qemu").copy()
2020-08-18 13:10:09 +00:00
instances.update(self._get_variables(container_list, "container"))
for host in instances:
vmid = instances[host]["proxmox_vmid"]
try:
pve_type = instances[host]["proxmox_type"]
except KeyError:
pve_type = "qemu"
try:
description = self.client.nodes(node).get(pve_type, vmid,
"config")["description"]
except KeyError:
description = None
2023-01-30 13:20:45 +00:00
except Exception as e: # noqa
raise AnsibleError(f"Proxmoxer API error: {to_native(e)}") from e
2020-08-18 13:10:09 +00:00
try:
metadata = json.loads(description)
except TypeError:
metadata = {}
except ValueError:
metadata = {"notes": description}
# Add hosts to default group
self.inventory.add_group(group=self.get_option("group"))
self.inventory.add_host(group=self.get_option("group"), host=host)
# Group hosts by status
self.inventory.add_group(group=instances[host]["proxmox_status"])
self.inventory.add_host(group=instances[host]["proxmox_status"], host=host)
if "groups" in metadata:
for group in metadata["groups"]:
self.inventory.add_group(group=group)
self.inventory.add_host(group=group, host=host)
if self.get_option("want_facts"):
for attr in instances[host]:
if attr not in ["proxmox_template"]:
self.inventory.set_variable(host, attr, instances[host][attr])
address = self._get_ip_address(pve_type, node, vmid)
if address:
self.inventory.set_variable(host, "ansible_host", address)
for pool in self._get_names(self.client.pools.get(), "pool"):
try:
pool_list = self._exclude(self.client.pool(pool).get()["members"])
2023-01-30 13:20:45 +00:00
except Exception as e: # noqa
raise AnsibleError(f"Proxmoxer API error: {to_native(e)}") from e
2020-08-18 13:10:09 +00:00
members = [
member["name"]
for member in pool_list
if (member["type"] == "qemu" or member["type"] == "lxc")
]
for member in members:
self.inventory.add_host(group=pool, host=member)
def verify_file(self, path):
"""Verify the Proxmox VE configuration file."""
2023-01-30 13:20:45 +00:00
if super().verify_file(path):
2020-08-18 13:10:09 +00:00
endings = ("proxmox.yaml", "proxmox.yml")
2023-01-30 13:20:45 +00:00
if any(path.endswith(ending) for ending in endings):
2020-08-18 13:10:09 +00:00
return True
return False
2023-01-30 13:20:45 +00:00
def parse(self, inventory, loader, path, cache=True): # noqa
2020-08-18 13:10:09 +00:00
"""Dynamically parse the Proxmox VE cloud inventory."""
if not HAS_PROXMOXER:
raise AnsibleError(
"The Proxmox VE dynamic inventory plugin requires proxmoxer: "
"https://pypi.org/project/proxmoxer/"
)
2023-01-30 13:20:45 +00:00
super().parse(inventory, loader, path)
2020-08-18 13:10:09 +00:00
self._read_config_data(path)
self.client = self._auth()
self._propagate()