2022-08-17 21:34:26 +02:00
|
|
|
"""ProxmoxVE provider, Ansible powered with Python"""
|
|
|
|
|
|
|
|
import pulumi
|
|
|
|
import pulumi_proxmoxve as proxmox
|
|
|
|
import pulumi_command as command
|
2024-09-09 02:09:20 +02:00
|
|
|
from pvewrapper import PveWrapper
|
|
|
|
import additional_config
|
2022-08-17 21:34:26 +02:00
|
|
|
|
2022-08-18 00:19:30 +02:00
|
|
|
# Check pulumi configuration
|
2022-08-17 21:34:26 +02:00
|
|
|
config = pulumi.Config()
|
|
|
|
|
2024-09-09 02:09:20 +02:00
|
|
|
username = config.require("pveTokenName").split("!")[0]
|
|
|
|
tokenname = config.require("pveTokenName").split("!")[1]
|
|
|
|
pvehostname = config.require("pveHostname")
|
|
|
|
pvenodename = config.require("pveNodeName")
|
|
|
|
pveURL=f"https://{pvehostname}:8006/"
|
|
|
|
verify = True if config.get_int("pveVerifySSL") == 1 else False
|
|
|
|
pvedefaultstorage = config.require("pveDefaultStorage")
|
|
|
|
vm_name: str = config.require("hostname")
|
|
|
|
vm_username: str = config.require("cloudUsername")
|
|
|
|
|
|
|
|
provider = proxmox.Provider(pvehostname,
|
|
|
|
endpoint=pveURL,
|
|
|
|
insecure=not verify,
|
|
|
|
api_token=config.require_secret("pveTokenValue").apply(lambda x: f"{username}!{tokenname}={x}")
|
2022-08-17 21:34:26 +02:00
|
|
|
)
|
|
|
|
|
2024-09-09 02:09:20 +02:00
|
|
|
pve = PveWrapper(pvehostname,
|
|
|
|
username=username,
|
|
|
|
token_name=tokenname,
|
|
|
|
token_value=config.require_secret("pveTokenValue").apply(lambda v: f"{v}"),
|
|
|
|
nodename=pvenodename,
|
|
|
|
verify_ssl=verify
|
|
|
|
)
|
2022-08-18 00:19:30 +02:00
|
|
|
|
2024-09-09 02:09:20 +02:00
|
|
|
vm = proxmox.vm.VirtualMachine(vm_name,
|
2022-08-17 21:34:26 +02:00
|
|
|
name = vm_name,
|
2024-09-09 02:09:20 +02:00
|
|
|
node_name = pvenodename,
|
2022-08-17 21:34:26 +02:00
|
|
|
agent = proxmox.vm.VirtualMachineAgentArgs(
|
|
|
|
enabled = True,
|
|
|
|
trim = True,
|
|
|
|
type = "virtio"
|
|
|
|
),
|
|
|
|
cpu = proxmox.vm.VirtualMachineCpuArgs(
|
2024-09-09 02:09:20 +02:00
|
|
|
cores = config.get_int("vmSockets"),
|
|
|
|
sockets = config.get_int("vmCores")
|
2022-08-17 21:34:26 +02:00
|
|
|
),
|
2024-09-09 02:09:20 +02:00
|
|
|
memory = proxmox.vm.VirtualMachineMemoryArgs(dedicated = config.get_int("vmRAM")),
|
2022-08-17 21:34:26 +02:00
|
|
|
clone = proxmox.vm.VirtualMachineCloneArgs(
|
2024-09-09 02:09:20 +02:00
|
|
|
node_name = pvenodename,
|
|
|
|
vm_id = additional_config.os_to_template[config.require("vmTemplate")],
|
|
|
|
full = False
|
2022-08-17 21:34:26 +02:00
|
|
|
),
|
|
|
|
disks = [
|
|
|
|
proxmox.vm.VirtualMachineDiskArgs(
|
|
|
|
interface = "virtio0",
|
2024-09-09 02:09:20 +02:00
|
|
|
datastore_id = pvedefaultstorage,
|
|
|
|
size = config.get_int("vmDiskSize"),
|
2022-08-17 21:34:26 +02:00
|
|
|
file_format = "raw"
|
|
|
|
)
|
|
|
|
],
|
|
|
|
network_devices = [
|
|
|
|
proxmox.vm.VirtualMachineNetworkDeviceArgs(
|
|
|
|
bridge = "vmbr0",
|
|
|
|
model = "virtio"
|
|
|
|
)
|
|
|
|
],
|
|
|
|
operating_system = proxmox.vm.VirtualMachineOperatingSystemArgs(
|
|
|
|
type = "l26"
|
|
|
|
),
|
|
|
|
initialization = proxmox.vm.VirtualMachineInitializationArgs(
|
|
|
|
type = "nocloud",
|
2024-09-09 02:09:20 +02:00
|
|
|
datastore_id = pvedefaultstorage,
|
2022-08-17 21:34:26 +02:00
|
|
|
user_account = proxmox.vm.VirtualMachineInitializationUserAccountArgs(
|
|
|
|
username = vm_username,
|
2024-09-09 02:09:20 +02:00
|
|
|
password = config.require_secret("cloudPassword"),
|
|
|
|
keys = additional_config.sshkeys
|
2022-08-17 21:34:26 +02:00
|
|
|
)
|
|
|
|
),
|
|
|
|
opts = pulumi.ResourceOptions(
|
2024-04-19 01:05:26 +02:00
|
|
|
provider = provider,
|
|
|
|
custom_timeouts=pulumi.CustomTimeouts(create="3m")
|
2022-08-17 21:34:26 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2024-09-09 02:09:20 +02:00
|
|
|
ipv4_addresses = vm.vm_id.apply(lambda vm_id: pve.ipv4_addresses(f"{vm_id}"))
|
2022-08-17 21:34:26 +02:00
|
|
|
|
2024-09-09 17:23:10 +02:00
|
|
|
## Creating the inventory file
|
|
|
|
#inventory = command.local.Command("a-inventory",
|
|
|
|
# create = vm.ipv4_addresses.apply(lambda ipaddr:
|
|
|
|
# f"echo '{vm_name} ansible_host={ipaddr[0]['ipv4_address']} ansible_user={vm_username}' >./inventory"
|
|
|
|
# ),
|
|
|
|
# delete = "rm -f ./inventory",
|
|
|
|
# opts = pulumi.ResourceOptions(depends_on = [ vm, ipv4_addresses ])
|
|
|
|
#)
|
|
|
|
#
|
|
|
|
## Applying the command
|
|
|
|
#for playbook in additional_config.playbooks:
|
|
|
|
# shortname: str = playbook.split(".")[0]
|
|
|
|
# command.local.Command(f"ap-{shortname}",
|
|
|
|
# create = vm.ipv4_addresses.apply(
|
|
|
|
# lambda run: f"ansible-playbook {run[0]['ipv4_address']}"
|
|
|
|
# ),
|
|
|
|
# delete = "rm -f ./ansible.log",
|
|
|
|
# opts = pulumi.ResourceOptions(depends_on = [ inventory ])
|
|
|
|
# )
|
2022-08-17 21:34:26 +02:00
|
|
|
|
|
|
|
# Outputs
|
2024-09-09 02:09:20 +02:00
|
|
|
pulumi.export("ipv4_addresses", vm.ipv4_addresses)
|