pulumi-templates/pvm-ansible/__main__.py

112 lines
3.6 KiB
Python
Raw Normal View History

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
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()
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")
2024-12-26 21:46:45 +01:00
tokenvalue = config.require_secret("pveTokenValue")
provider = proxmox.Provider(pvehostname,
endpoint=pveURL,
insecure=not verify,
2024-12-26 21:46:45 +01:00
api_token=tokenvalue.apply(lambda x: f"{username}!{tokenname}={x}")
2022-08-17 21:34:26 +02:00
)
pve = PveWrapper(pvehostname,
username=username,
token_name=tokenname,
2024-12-26 21:46:45 +01:00
token_value=tokenvalue.apply(lambda v: f"{v}"),
nodename=pvenodename,
verify_ssl=verify
)
2022-08-18 00:19:30 +02:00
vm = proxmox.vm.VirtualMachine(vm_name,
2022-08-17 21:34:26 +02:00
name = vm_name,
node_name = pvenodename,
2022-08-17 21:34:26 +02:00
agent = proxmox.vm.VirtualMachineAgentArgs(
2024-12-26 21:46:45 +01:00
enabled=True,
type="virtio",
timeout="2m",
),
2022-08-17 21:34:26 +02:00
cpu = proxmox.vm.VirtualMachineCpuArgs(
cores = config.get_int("vmSockets"),
sockets = config.get_int("vmCores")
2022-08-17 21:34:26 +02:00
),
memory = proxmox.vm.VirtualMachineMemoryArgs(dedicated = config.get_int("vmRAM")),
2022-08-17 21:34:26 +02:00
clone = proxmox.vm.VirtualMachineCloneArgs(
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",
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",
datastore_id = pvedefaultstorage,
2022-08-17 21:34:26 +02:00
user_account = proxmox.vm.VirtualMachineInitializationUserAccountArgs(
username = vm_username,
password = config.require_secret("cloudPassword"),
keys = additional_config.sshkeys
2024-12-26 21:46:45 +01:00
),
vendor_data_file_id = "cloud-init/qemu-guest-agent_deb.yml"
2022-08-17 21:34:26 +02:00
),
opts = pulumi.ResourceOptions(
provider = provider,
custom_timeouts=pulumi.CustomTimeouts(create="3m")
2022-08-17 21:34:26 +02:00
)
)
2024-12-26 21:46:45 +01:00
ipv4_addresses = vm.id.apply(lambda id: pve.ipv4_addresses(f"{id}"))
# Creating the inventory file
inventory = command.local.Command("a-inventory",
create = ipv4_addresses.apply(lambda ipaddr:
f"echo '{vm_name} ansible_host={ipaddr[0]['address']} ansible_user={vm_username}' >./inventory"
),
delete = "rm -f ./inventory",
opts = pulumi.ResourceOptions(depends_on = [ vm, ipv4_addresses ])
)
2022-08-17 21:34:26 +02:00
2024-12-26 21:46:45 +01:00
# 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
pulumi.export("ipv4_addresses", vm.ipv4_addresses)