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
|
2022-08-18 00:19:30 +02:00
|
|
|
import yaml
|
2022-08-17 21:34:26 +02:00
|
|
|
|
2022-08-18 00:19:30 +02:00
|
|
|
# Import configuration parameters from input.yaml
|
|
|
|
i = open("./input.yaml")
|
|
|
|
input_ = yaml.safe_load(i.read())
|
|
|
|
i.close()
|
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()
|
|
|
|
|
|
|
|
provider = proxmox.Provider("proxmoxve",
|
2024-04-19 01:05:26 +02:00
|
|
|
endpoint=input_['pve']['url'],
|
|
|
|
insecure=input_['pve']['insecure'],
|
|
|
|
username=input_['pve']['username'],
|
|
|
|
password=config.require_secret("password")
|
2022-08-17 21:34:26 +02:00
|
|
|
)
|
|
|
|
|
2022-08-18 00:19:30 +02:00
|
|
|
vm_name = input_['vm']['name']
|
|
|
|
vm_username = input_['vm']['username']
|
|
|
|
ansible_playbook = input_['ansible_playbook']
|
|
|
|
|
2022-08-17 21:34:26 +02:00
|
|
|
vm = proxmox.vm.VirtualMachine("vm",
|
|
|
|
name = vm_name,
|
2022-08-18 00:19:30 +02:00
|
|
|
node_name = input_['pve']['nodename'],
|
2022-08-17 21:34:26 +02:00
|
|
|
agent = proxmox.vm.VirtualMachineAgentArgs(
|
|
|
|
enabled = True,
|
|
|
|
trim = True,
|
|
|
|
type = "virtio"
|
|
|
|
),
|
|
|
|
bios = "seabios",
|
|
|
|
cpu = proxmox.vm.VirtualMachineCpuArgs(
|
2022-08-18 00:19:30 +02:00
|
|
|
cores = input_['vm']['cores'],
|
|
|
|
sockets = input_['vm']['sockets']
|
2022-08-17 21:34:26 +02:00
|
|
|
),
|
|
|
|
memory = proxmox.vm.VirtualMachineMemoryArgs(
|
2022-08-18 00:19:30 +02:00
|
|
|
dedicated = input_['vm']['ram']
|
2022-08-17 21:34:26 +02:00
|
|
|
),
|
|
|
|
clone = proxmox.vm.VirtualMachineCloneArgs(
|
2022-08-18 00:19:30 +02:00
|
|
|
node_name = input_['pve']['nodename'],
|
|
|
|
vm_id = input_['vm']['clone'],
|
2022-08-17 21:34:26 +02:00
|
|
|
full = True
|
|
|
|
),
|
|
|
|
disks = [
|
|
|
|
proxmox.vm.VirtualMachineDiskArgs(
|
|
|
|
interface = "virtio0",
|
2022-08-18 00:19:30 +02:00
|
|
|
datastore_id = input_['pve']['storage'],
|
|
|
|
size = input_['vm']['disksize'],
|
2022-08-17 21:34:26 +02:00
|
|
|
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",
|
2022-08-18 00:19:30 +02:00
|
|
|
datastore_id = input_['pve']['storage'],
|
2022-08-17 21:34:26 +02:00
|
|
|
user_account = proxmox.vm.VirtualMachineInitializationUserAccountArgs(
|
|
|
|
username = vm_username,
|
2022-08-18 00:19:30 +02:00
|
|
|
password = input_['vm']['password'],
|
|
|
|
keys = input_['vm']['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
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
|
2022-08-18 00:19:30 +02:00
|
|
|
# Creating the inventory file
|
|
|
|
inventory = command.local.Command("a-inventory",
|
|
|
|
create = vm.ipv4_addresses[1][0].apply(
|
2022-08-18 00:22:54 +02:00
|
|
|
lambda ipaddr: f"echo '{vm_name} ansible_host={ipaddr} ansible_user={vm_username}' >./inventory"
|
2022-08-18 00:19:30 +02:00
|
|
|
),
|
|
|
|
delete = "rm -f ./inventory",
|
|
|
|
opts = pulumi.ResourceOptions(depends_on = [ vm ])
|
2022-08-17 23:05:54 +02:00
|
|
|
)
|
2022-08-17 21:34:26 +02:00
|
|
|
|
|
|
|
# Try the deployment with ansible
|
|
|
|
|
|
|
|
# Applying the command
|
2022-08-18 00:19:30 +02:00
|
|
|
execute_ansible = command.local.Command("a-deploy",
|
2022-08-17 23:05:54 +02:00
|
|
|
create = vm.ipv4_addresses[1][0].apply(
|
2022-08-18 00:19:30 +02:00
|
|
|
lambda ipaddr: f"ansible-playbook {ansible_playbook}"
|
2022-08-17 23:05:54 +02:00
|
|
|
),
|
2022-08-18 00:19:30 +02:00
|
|
|
delete = "rm -f ./ansible.log",
|
|
|
|
opts = pulumi.ResourceOptions(depends_on = [ inventory ])
|
2022-08-17 21:34:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# Outputs
|
2022-08-17 21:55:14 +02:00
|
|
|
pulumi.export("ip", vm.ipv4_addresses[1][0])
|