From 6b6795c2181c8143e139dbbf8433aecd97ba19a3 Mon Sep 17 00:00:00 2001 From: Emiliano Vavassori Date: Wed, 17 Aug 2022 21:34:26 +0200 Subject: [PATCH] Adding a new template. --- proxmoxve-ansible-python/.gitignore | 4 + proxmoxve-ansible-python/Pulumi.yaml | 6 ++ proxmoxve-ansible-python/README.md | 3 + proxmoxve-ansible-python/__main__.py | 112 ++++++++++++++++++++++ proxmoxve-ansible-python/ansible.cfg | 5 + proxmoxve-ansible-python/deploy.yml | 6 ++ proxmoxve-ansible-python/requirements.txt | 3 + 7 files changed, 139 insertions(+) create mode 100644 proxmoxve-ansible-python/.gitignore create mode 100644 proxmoxve-ansible-python/Pulumi.yaml create mode 100644 proxmoxve-ansible-python/README.md create mode 100644 proxmoxve-ansible-python/__main__.py create mode 100644 proxmoxve-ansible-python/ansible.cfg create mode 100644 proxmoxve-ansible-python/deploy.yml create mode 100644 proxmoxve-ansible-python/requirements.txt diff --git a/proxmoxve-ansible-python/.gitignore b/proxmoxve-ansible-python/.gitignore new file mode 100644 index 0000000..de6bc4a --- /dev/null +++ b/proxmoxve-ansible-python/.gitignore @@ -0,0 +1,4 @@ +*.pyc +venv/ +Pulumi.*.yaml +inventory diff --git a/proxmoxve-ansible-python/Pulumi.yaml b/proxmoxve-ansible-python/Pulumi.yaml new file mode 100644 index 0000000..63e4d47 --- /dev/null +++ b/proxmoxve-ansible-python/Pulumi.yaml @@ -0,0 +1,6 @@ +--- +name: ${PROJECT} +description: ${DESCRIPTION} +runtime: python +template: + description: ProxmoxVE provider, Ansible powered with Python diff --git a/proxmoxve-ansible-python/README.md b/proxmoxve-ansible-python/README.md new file mode 100644 index 0000000..1b6c5e1 --- /dev/null +++ b/proxmoxve-ansible-python/README.md @@ -0,0 +1,3 @@ +# Required configuration parameters # + +- `password`: password of the `root@pam` user (use `--secret` please) diff --git a/proxmoxve-ansible-python/__main__.py b/proxmoxve-ansible-python/__main__.py new file mode 100644 index 0000000..b628539 --- /dev/null +++ b/proxmoxve-ansible-python/__main__.py @@ -0,0 +1,112 @@ +"""ProxmoxVE provider, Ansible powered with Python""" + +import pulumi +import pulumi_proxmoxve as proxmox +import pulumi_command as command + +# 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 + +config = pulumi.Config() + +provider = proxmox.Provider("proxmoxve", + virtual_environment = { + "endpoint": pve_url, + "insecure": True, + "username": pve_username, + "password": config.require_secret("password") + } +) + +vm = proxmox.vm.VirtualMachine("vm", + name = vm_name, + node_name = pve_nodename, + agent = proxmox.vm.VirtualMachineAgentArgs( + enabled = True, + trim = True, + type = "virtio" + ), + bios = "seabios", + cpu = proxmox.vm.VirtualMachineCpuArgs( + cores = vm_cores, + sockets = vm_sockets + ), + memory = proxmox.vm.VirtualMachineMemoryArgs( + dedicated = mv_ram + ), + clone = proxmox.vm.VirtualMachineCloneArgs( + node_name = pve_nodename, + vm_id = vm_clone, + full = True + ), + disks = [ + proxmox.vm.VirtualMachineDiskArgs( + interface = "virtio0", + datastore_id = pve_storage, + size = 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 = pve_storage, + user_account = proxmox.vm.VirtualMachineInitializationUserAccountArgs( + username = vm_username, + password = "cicciopasticcio", + keys = vm_sshkeys + ) + ), + opts = pulumi.ResourceOptions( + provider = provider + ) +) + +# Getting local IP +# 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. +lanip = vm.ipv4_addresses[1][0] + +# Creating the inventory file. +with open('inventory', 'w') as f: + f.write(f"{vm_name} ansible_host={lanip} ansible_user={vm_username}") + +# Try the deployment with ansible + +# Applying the command +execute_ansible = command.local.Command("ansible", + create = f"ansible-playbook {ansible_playbook}" +) + +# Outputs +pulumi.export("ip", lanip) diff --git a/proxmoxve-ansible-python/ansible.cfg b/proxmoxve-ansible-python/ansible.cfg new file mode 100644 index 0000000..1752d61 --- /dev/null +++ b/proxmoxve-ansible-python/ansible.cfg @@ -0,0 +1,5 @@ +[defaults] +collections_on_ansible_version_mismatch = False +action_warnings = False +host_key_checking = False +inventory = inventory diff --git a/proxmoxve-ansible-python/deploy.yml b/proxmoxve-ansible-python/deploy.yml new file mode 100644 index 0000000..293b54a --- /dev/null +++ b/proxmoxve-ansible-python/deploy.yml @@ -0,0 +1,6 @@ +--- +- hosts: all + roles: + - base_personal + become: yes + become_user: root diff --git a/proxmoxve-ansible-python/requirements.txt b/proxmoxve-ansible-python/requirements.txt new file mode 100644 index 0000000..729741d --- /dev/null +++ b/proxmoxve-ansible-python/requirements.txt @@ -0,0 +1,3 @@ +pulumi>=3.0.0,<4.0.0 +pulumi-proxmoxve>=2.0.0 +pulumi-command>=0.4.1