From a9a712cada0a5773cdd8c26ada098a1134f2f0af Mon Sep 17 00:00:00 2001 From: Emiliano Vavassori Date: Thu, 18 Aug 2022 00:19:30 +0200 Subject: [PATCH] Latest changes, works in production. --- proxmoxve-ansible-python/README.md | 8 ++- proxmoxve-ansible-python/__main__.py | 82 ++++++++++++---------------- proxmoxve-ansible-python/input.yaml | 25 +++++++++ 3 files changed, 66 insertions(+), 49 deletions(-) create mode 100644 proxmoxve-ansible-python/input.yaml diff --git a/proxmoxve-ansible-python/README.md b/proxmoxve-ansible-python/README.md index 1b6c5e1..64d82d0 100644 --- a/proxmoxve-ansible-python/README.md +++ b/proxmoxve-ansible-python/README.md @@ -1,3 +1,7 @@ -# Required configuration parameters # +# Quick configuration # -- `password`: password of the `root@pam` user (use `--secret` please) +Check out the file `input.yml` for a quick setup for a new machine. +Also, don't forget to setup the password for your account on your ProxmoxVE +server configuring `pulumi`: + + $ pulumi config set password --secret diff --git a/proxmoxve-ansible-python/__main__.py b/proxmoxve-ansible-python/__main__.py index 5b00317..79b41ba 100644 --- a/proxmoxve-ansible-python/__main__.py +++ b/proxmoxve-ansible-python/__main__.py @@ -3,46 +3,32 @@ import pulumi import pulumi_proxmoxve as proxmox import pulumi_command as command +import yaml -# Basic PVE configuration -pve_url = "https://pve.vavassori.lcl:8006" -pve_username = "root@pam" -pve_nodename = "pve" -pve_storage = "local-lvm" - -# Basic VM configuration -vm_name = "virtual-machine" -vm_clone = 802 -vm_sockets = 1 -vm_cores = 1 -vm_ram = 2048 -vm_disksize = 20 -vm_username = "syntaxerrormmm" -vm_sshkeys = [ - "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFioHkaV1NhX6NCqsJakJw8EVBOcDHm1MEbpY499CPtG syntaxerrormmm@fisso", - "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILu91hBh8pNRt4eE1pug0Y4jCHZDCcMJ+vj3CiF5EQHV syntaxerrormmm@syntaxxps", - "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIP/hn/0xn6DRS2B0paFzDQRakupgTQQ5hitQhqOfWcqz syntaxerrormmm@microbo" -] - -# Ansible configuration -ansible_playbook = "deploy.yml" - -# Start of the program +# 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", virtual_environment = { - "endpoint": pve_url, - "insecure": True, - "username": pve_username, + "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 = pve_nodename, + node_name = input_['pve']['nodename'], agent = proxmox.vm.VirtualMachineAgentArgs( enabled = True, trim = True, @@ -50,22 +36,22 @@ vm = proxmox.vm.VirtualMachine("vm", ), bios = "seabios", cpu = proxmox.vm.VirtualMachineCpuArgs( - cores = vm_cores, - sockets = vm_sockets + cores = input_['vm']['cores'], + sockets = input_['vm']['sockets'] ), memory = proxmox.vm.VirtualMachineMemoryArgs( - dedicated = vm_ram + dedicated = input_['vm']['ram'] ), clone = proxmox.vm.VirtualMachineCloneArgs( - node_name = pve_nodename, - vm_id = vm_clone, + node_name = input_['pve']['nodename'], + vm_id = input_['vm']['clone'], full = True ), disks = [ proxmox.vm.VirtualMachineDiskArgs( interface = "virtio0", - datastore_id = pve_storage, - size = vm_disksize, + datastore_id = input_['pve']['storage'], + size = input_['vm']['disksize'], file_format = "raw" ) ], @@ -81,11 +67,11 @@ vm = proxmox.vm.VirtualMachine("vm", ), initialization = proxmox.vm.VirtualMachineInitializationArgs( type = "nocloud", - datastore_id = pve_storage, + datastore_id = input_['pve']['storage'], user_account = proxmox.vm.VirtualMachineInitializationUserAccountArgs( username = vm_username, - password = "cicciopasticcio", - keys = vm_sshkeys + password = input_['vm']['password'], + keys = input_['vm']['sshkeys'] ) ), opts = pulumi.ResourceOptions( @@ -95,22 +81,24 @@ vm = proxmox.vm.VirtualMachine("vm", # 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 (which will not be used by pulumi for ansible, -# btw). -inventory = open('inventory', 'w') -vm.ipv4_addresses[1][0].apply( - lambda lanip: inventory.write(f"{vm_name} ansible_host={lanip} ansible_user={vm_username}") +# 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={lanip} ansible_user={vm_username}' >./inventory" + ), + delete = "rm -f ./inventory", + opts = pulumi.ResourceOptions(depends_on = [ vm ]) ) -inventory.close() # Try the deployment with ansible # Applying the command -execute_ansible = command.local.Command("ansible", +execute_ansible = command.local.Command("a-deploy", create = vm.ipv4_addresses[1][0].apply( - lambda ipaddr: f"ansible-playbook -i '{ipaddr},' {ansible_playbook}" + lambda ipaddr: f"ansible-playbook {ansible_playbook}" ), - opts = pulumi.ResourceOptions(depends_on = [vm]) + delete = "rm -f ./ansible.log", + opts = pulumi.ResourceOptions(depends_on = [ inventory ]) ) # Outputs diff --git a/proxmoxve-ansible-python/input.yaml b/proxmoxve-ansible-python/input.yaml new file mode 100644 index 0000000..5d11f77 --- /dev/null +++ b/proxmoxve-ansible-python/input.yaml @@ -0,0 +1,25 @@ +--- +pve: + url: "https://pve.vavassori.lcl:8006" + insecure: true + username: root@pam + nodename: pve + storage: local-lvm + +# Basic VM configuration +vm: + name: virtual-machine + clone: 802 + sockets: 1 + cores: 1 + ram: 2048 + disksize: 20 + username: syntaxerrormmm + password: cicciopasticcio + sshkeys: + - "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFioHkaV1NhX6NCqsJakJw8EVBOcDHm1MEbpY499CPtG syntaxerrormmm@fisso" + - "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILu91hBh8pNRt4eE1pug0Y4jCHZDCcMJ+vj3CiF5EQHV syntaxerrormmm@syntaxxps" + - "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIP/hn/0xn6DRS2B0paFzDQRakupgTQQ5hitQhqOfWcqz syntaxerrormmm@microbo" + +# Ansible configuration +ansible_playbook: deploy.yml