105 lines
3.1 KiB
Python
105 lines
3.1 KiB
Python
"""ProxmoxVE provider, Ansible powered with Python"""
|
|
|
|
import pulumi
|
|
import pulumi_proxmoxve as proxmox
|
|
import pulumi_command as command
|
|
import yaml
|
|
|
|
# Import configuration parameters from input.yaml
|
|
i = open("./input.yaml")
|
|
input_ = yaml.safe_load(i.read())
|
|
i.close()
|
|
|
|
# Check pulumi configuration
|
|
config = pulumi.Config()
|
|
|
|
provider = proxmox.Provider("proxmoxve",
|
|
endpoint=input_['pve']['url'],
|
|
insecure=input_['pve']['insecure'],
|
|
username=input_['pve']['username'],
|
|
password=config.require_secret("password")
|
|
)
|
|
|
|
vm_name = input_['vm']['name']
|
|
vm_username = input_['vm']['username']
|
|
ansible_playbook = input_['ansible_playbook']
|
|
|
|
vm = proxmox.vm.VirtualMachine("vm",
|
|
name = vm_name,
|
|
node_name = input_['pve']['nodename'],
|
|
agent = proxmox.vm.VirtualMachineAgentArgs(
|
|
enabled = True,
|
|
trim = True,
|
|
type = "virtio"
|
|
),
|
|
bios = "seabios",
|
|
cpu = proxmox.vm.VirtualMachineCpuArgs(
|
|
cores = input_['vm']['cores'],
|
|
sockets = input_['vm']['sockets']
|
|
),
|
|
memory = proxmox.vm.VirtualMachineMemoryArgs(
|
|
dedicated = input_['vm']['ram']
|
|
),
|
|
clone = proxmox.vm.VirtualMachineCloneArgs(
|
|
node_name = input_['pve']['nodename'],
|
|
vm_id = input_['vm']['clone'],
|
|
full = True
|
|
),
|
|
disks = [
|
|
proxmox.vm.VirtualMachineDiskArgs(
|
|
interface = "virtio0",
|
|
datastore_id = input_['pve']['storage'],
|
|
size = input_['vm']['disksize'],
|
|
file_format = "raw"
|
|
)
|
|
],
|
|
network_devices = [
|
|
proxmox.vm.VirtualMachineNetworkDeviceArgs(
|
|
bridge = "vmbr0",
|
|
model = "virtio"
|
|
)
|
|
],
|
|
on_boot = True,
|
|
operating_system = proxmox.vm.VirtualMachineOperatingSystemArgs(
|
|
type = "l26"
|
|
),
|
|
initialization = proxmox.vm.VirtualMachineInitializationArgs(
|
|
type = "nocloud",
|
|
datastore_id = input_['pve']['storage'],
|
|
user_account = proxmox.vm.VirtualMachineInitializationUserAccountArgs(
|
|
username = vm_username,
|
|
password = input_['vm']['password'],
|
|
keys = input_['vm']['sshkeys']
|
|
)
|
|
),
|
|
opts = pulumi.ResourceOptions(
|
|
provider = provider,
|
|
custom_timeouts=pulumi.CustomTimeouts(create="3m")
|
|
)
|
|
)
|
|
|
|
# First item of the ipv4_addresses is of the loopback interface (so the usual 127.0.0.1). Let's get the second and grab only the text.
|
|
|
|
# Creating the inventory file
|
|
inventory = command.local.Command("a-inventory",
|
|
create = vm.ipv4_addresses[1][0].apply(
|
|
lambda ipaddr: f"echo '{vm_name} ansible_host={ipaddr} ansible_user={vm_username}' >./inventory"
|
|
),
|
|
delete = "rm -f ./inventory",
|
|
opts = pulumi.ResourceOptions(depends_on = [ vm ])
|
|
)
|
|
|
|
# Try the deployment with ansible
|
|
|
|
# Applying the command
|
|
execute_ansible = command.local.Command("a-deploy",
|
|
create = vm.ipv4_addresses[1][0].apply(
|
|
lambda ipaddr: f"ansible-playbook {ansible_playbook}"
|
|
),
|
|
delete = "rm -f ./ansible.log",
|
|
opts = pulumi.ResourceOptions(depends_on = [ inventory ])
|
|
)
|
|
|
|
# Outputs
|
|
pulumi.export("ip", vm.ipv4_addresses[1][0])
|