ansible-netcup/plugins/inventory/scp.py

92 Zeilen
3.7 KiB
Python

2023-12-09 12:06:45 +00:00
from xmlrpc.client import ServerProxy
2023-11-26 15:03:21 +00:00
from ansible.inventory.data import InventoryData
from ansible.parsing.dataloader import DataLoader
2023-12-09 12:06:45 +00:00
from ansible.plugins.inventory import BaseInventoryPlugin
2023-11-26 15:03:21 +00:00
from ansible.utils.display import Display
2023-12-09 12:06:45 +00:00
from zeep import Client, Settings
2023-11-26 15:03:21 +00:00
display = Display()
2023-12-09 12:06:45 +00:00
2023-11-26 15:03:21 +00:00
class InventoryModule(BaseInventoryPlugin):
2023-12-10 19:47:58 +00:00
NAME = "ansible.netcup.scp"
2023-11-26 15:03:21 +00:00
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 = dict()
2023-11-26 15:03:21 +00:00
for interface in serverinfo["serverInterfaces"]:
interfaces[interface["mac"]] = dict(
driver=interface["driver"],
id=interface["id"],
ipv4=interface["ipv4IP"],
ipv6=interface["ipv6IP"],
mac=interface["mac"],
2023-11-26 15:03:21 +00:00
)
if len(interface["ipv4IP"]) > 0:
ansible_host = interface["ipv4IP"][0]
2023-11-26 15:03:21 +00:00
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 self.config.get("nickname_is_hostname", False):
ansible_host = nickname
inventory.set_variable(nickname, "ansible_host", ansible_host)
2023-11-26 15:03:21 +00:00
2023-12-10 19:47:58 +00:00
DOCUMENTATION = """
name: ansible.netcup.scp
2023-11-26 15:03:21 +00:00
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
2023-12-10 19:47:58 +00:00
choices: ['scp', "ansible.netcup.scp"]
2023-11-26 15:03:21 +00:00
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
2023-12-09 12:06:45 +00:00
"""