pulumi-templates/pvm-ansible/__main__.py

110 lines
3.5 KiB
Python

"""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
# Check pulumi configuration
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")
provider = proxmox.Provider(pvehostname,
endpoint=pveURL,
insecure=not verify,
api_token=config.require_secret("pveTokenValue").apply(lambda x: f"{username}!{tokenname}={x}")
)
pve = PveWrapper(pvehostname,
username=username,
token_name=tokenname,
token_value=config.require_secret("pveTokenValue").apply(lambda v: f"{v}"),
nodename=pvenodename,
verify_ssl=verify
)
vm = proxmox.vm.VirtualMachine(vm_name,
name = vm_name,
node_name = pvenodename,
agent = proxmox.vm.VirtualMachineAgentArgs(
enabled = True,
trim = True,
type = "virtio"
),
cpu = proxmox.vm.VirtualMachineCpuArgs(
cores = config.get_int("vmSockets"),
sockets = config.get_int("vmCores")
),
memory = proxmox.vm.VirtualMachineMemoryArgs(dedicated = config.get_int("vmRAM")),
clone = proxmox.vm.VirtualMachineCloneArgs(
node_name = pvenodename,
vm_id = additional_config.os_to_template[config.require("vmTemplate")],
full = False
),
disks = [
proxmox.vm.VirtualMachineDiskArgs(
interface = "virtio0",
datastore_id = pvedefaultstorage,
size = config.get_int("vmDiskSize"),
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,
user_account = proxmox.vm.VirtualMachineInitializationUserAccountArgs(
username = vm_username,
password = config.require_secret("cloudPassword"),
keys = additional_config.sshkeys
)
),
opts = pulumi.ResourceOptions(
provider = provider,
custom_timeouts=pulumi.CustomTimeouts(create="3m")
)
)
ipv4_addresses = vm.vm_id.apply(lambda vm_id: pve.ipv4_addresses(f"{vm_id}"))
## 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 ])
# )
# Outputs
pulumi.export("ipv4_addresses", vm.ipv4_addresses)