ansible-netcup/plugins/inventory/scp.py

83 Zeilen
3.3 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")
for hostname in self.service.getVServers(**self.args):
serverinfo = self.service.getVServerInformation(vservername=hostname, **self.args)
nickname = serverinfo["vServerNickname"] or serverinfo["vServerName"]
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 = []
for interface in serverinfo["serverInterfaces"]:
interfaces.append(
dict(
driver=interface["driver"],
id=interface["id"],
ipv4=interface["ipv4IP"],
ipv6=interface["ipv6IP"],
mac=interface["mac"],
)
)
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")
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
"""