ansible-netcup/plugins/inventory/scp.py

105 Zeilen
4.2 KiB
Python

from xmlrpc.client import ServerProxy
from ansible.inventory.data import InventoryData
from ansible.parsing.dataloader import DataLoader
from ansible.plugins.inventory import BaseInventoryPlugin
from ansible.utils.display import Display
from zeep import Client, Settings
display = Display()
class InventoryModule(BaseInventoryPlugin):
NAME = "ansible.netcup.scp"
loader: DataLoader
inventory: InventoryData
plugin: str
args: dict[str, str]
def parse(self, inventory: InventoryData, loader: DataLoader, path: str, cache=True):
super(InventoryModule, self).parse(inventory, loader, path, cache)
try:
self.config = self._read_config_data(path)
except Exception as e:
raise e
return
self.plugin = self.config["plugin"]
self.args = dict(loginName=str(self.config["userid"]), password=str(self.config["password"]))
client = Client("https://www.servercontrolpanel.de/SCP/WSEndUser?wsdl", settings=Settings(force_https=True))
self.service = client.service
inventory.add_group("netcup")
inventory.add_group("netcup_online")
inventory.add_group("netcup_offline")
inventory.add_child("netcup", "netcup_online")
inventory.add_child("netcup", "netcup_offline")
additional = self.config.get("additional_info", dict())
for hostname in self.service.getVServers(**self.args):
serverinfo = self.service.getVServerInformation(vservername=hostname, **self.args)
nickname = serverinfo["vServerNickname"] or serverinfo["vServerName"]
ai = additional.get(nickname, dict())
group = "netcup_online"
if serverinfo["status"] != "online":
group = "netcup_offline"
if inventory.get_host(nickname) is None:
inventory.add_host(nickname, group)
else:
inventory.add_child(group, nickname)
interfaces = dict()
ai_interfaces = ai.pop("interfaces", dict())
for interface in serverinfo["serverInterfaces"]:
interfaces[interface["mac"]] = dict()
interfaces[interface["mac"]].update(
driver=interface["driver"],
id=interface["id"],
ipv4=interface["ipv4IP"],
ipv6=interface["ipv6IP"],
mac=interface["mac"],
)
interfaces[interface["mac"]].update(ai_interfaces.pop(interface["mac"], dict()))
if len(interface["ipv4IP"]) > 0:
ansible_host = interface["ipv4IP"][0]
inventory.set_variable(nickname, "interfaces", interfaces)
inventory.set_variable(nickname, "memory", serverinfo["memory"])
inventory.set_variable(nickname, "name", serverinfo["vServerName"])
inventory.set_variable(nickname, "cores", serverinfo["cpuCores"])
inventory.set_variable(nickname, "up", serverinfo["status"] == "online")
if ai.get("ansible_host", False):
ansible_host = ai.pop("ansible_host")
elif self.config.get("nickname_is_hostname", False):
ansible_host = nickname
inventory.set_variable(nickname, "ansible_host", ansible_host)
DOCUMENTATION = """
name: ansible.netcup.scp
plugin_type: inventory
short_description: Returns Ansible inventory from netcup SCP
description: Returns Ansible inventory from netcup SCP
version_added: 0.1.0
options:
plugin:
description: Name of the plugin
required: true
choices: ['scp', "ansible.netcup.scp"]
userid:
description: userid for logging into SCP
required: true
password:
description:
- API password to authenticate againt the api.
- This is different from you SCP password and must be set indipendently from the SCP password
required: true
nickname_is_hostname:
version_added: 0.1.1
description: use the nickname as hostname
type: bool
required: false
default: false
additional_info:
version_added: 0.1.2
description:
required: false
default: {}
type: dict
"""