
    Vht                        d dl mZmZmZ eZdZdZdZd dl	Z	d dl
Z
d dlmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZ d Zd Z G d de      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functionu+Y  
module: proxmox_kvm
short_description: Management of Qemu(KVM) Virtual Machines in Proxmox VE cluster
description:
  - Allows you to create/delete/stop Qemu(KVM) Virtual Machines in Proxmox VE cluster.
author: "Abdoul Bah (@helldorado) <bahabdoul at gmail.com>"
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  archive:
    description:
      - Specify a path to an archive to restore (instead of creating or cloning a VM).
    type: str
  acpi:
    description:
      - Specify if ACPI should be enabled/disabled.
    type: bool
  agent:
    description:
      - Specify if the QEMU Guest Agent should be enabled/disabled.
    type: str
  args:
    description:
      - Pass arbitrary arguments to kvm.
      - This option is for experts only!
    type: str
  audio:
    description:
      - A hash/dictionary of audio devices for the VM. O(audio={"key":"value", "key":"value"}).
      - Keys allowed are - C(audio[n]) where 0 ≤ n ≤ N.
      - Values allowed are - C(device="ich9-intel-hda|intel-hda|AC97",driver="none|spice").
      - C(device) is either V(ich9-intel-hda) or V(intel-hda) or V(AC97).
      - Option C(driver) is V(none) or V(spice).
    type: dict
  autostart:
    description:
      - Specify if the VM should be automatically restarted after crash (currently ignored in PVE API).
    type: bool
  balloon:
    description:
      - Specify the amount of RAM for the VM in MB.
      - Using zero disables the balloon driver.
    type: int
  bios:
    description:
      - Specify the BIOS implementation.
    type: str
    choices: ['seabios', 'ovmf']
  boot:
    description:
      - Specify the boot order -> boot on floppy V(a), hard disk V(c), CD-ROM V(d), or network V(n).
      - For newer versions of Proxmox VE, use a boot order like V(order=scsi0;net0;hostpci0).
      - You can combine to set order.
      - Network boot requires setting V(rng0) since PVE 8.3.5.
    type: str
  bootdisk:
    description:
      - Enable booting from specified disk. Format V((ide|sata|scsi|virtio\)\\d+).
    type: str
  cicustom:
    description:
      - 'Cloud-init: Specify custom files to replace the automatically generated ones at start.'
    type: str
  cipassword:
    description:
      - 'Cloud-init: password of default user to create.'
    type: str
  citype:
    description:
      - 'Cloud-init: Specifies the cloud-init configuration format.'
      - The default depends on the configured operating system type (V(ostype)).
      - We use the V(nocloud) format for Linux, and V(configdrive2) for Windows.
    type: str
    choices: ['nocloud', 'configdrive2']
  ciupgrade:
    description:
      - 'Cloud-init: do an automatic package upgrade after the first boot.'
    type: bool
  ciuser:
    description:
      - 'Cloud-init: username of default user to create.'
    type: str
  clone:
    description:
      - Name of VM to be cloned. If O(vmid) is set, O(clone) can take an arbitrary value but is required for initiating the
        clone.
    type: str
  cores:
    description:
      - Specify number of cores per socket.
    type: int
  cpu:
    description:
      - Specify emulated CPU type.
    type: str
  cpulimit:
    description:
      - Specify if CPU usage will be limited. Value V(0) indicates no CPU limit.
      - If the computer has 2 CPUs, it has total of '2' CPU time.
    type: int
  cpuunits:
    description:
      - Specify CPU weight for a VM.
      - You can disable fair-scheduler configuration by setting this to V(0).
    type: int
  delete:
    description:
      - Specify a list of settings you want to delete.
    type: str
  description:
    description:
      - Specify the description for the VM. Only used on the configuration web interface.
      - This is saved as comment inside the configuration file.
    type: str
  digest:
    description:
      - Specify if to prevent changes if current configuration file has different SHA1 digest.
      - This can be used to prevent concurrent modifications.
    type: str
  efidisk0:
    description:
      - Specify a hash/dictionary of EFI disk options.
      - Requires O(bios=ovmf) to be set to be able to use it.
    type: dict
    suboptions:
      storage:
        description:
          - V(storage) is the storage identifier where to create the disk.
        type: str
      format:
        description:
          - V(format) is the drive's backing file's data format. Please refer to the Proxmox VE Administrator Guide, section
            Proxmox VE Storage (see U(https://pve.proxmox.com/pve-docs/chapter-pvesm.html) for the latest version, tables
            3 to 14) to find out format supported by the provided storage backend.
        type: str
      efitype:
        description:
          - V(efitype) indicates the size of the EFI disk.
          - V(2m) will allow for a 2MB EFI disk, which will be enough to persist boot order and new boot entries.
          - V(4m) will allow for a 4MB EFI disk, which will additionally allow to store EFI keys in order to enable Secure
            Boot.
        type: str
        choices:
          - 2m
          - 4m
      pre_enrolled_keys:
        description:
          - V(pre_enrolled_keys) indicates whether EFI keys for Secure Boot should be enrolled V(1) in the VM firmware upon
            creation or not (0).
          - If set to V(1), Secure Boot will also be enabled by default when the VM is created.
        type: bool
  force:
    description:
      - Allow to force stop VM.
      - Can be used with states V(stopped), V(restarted), and V(absent).
      - Requires parameter O(archive).
    type: bool
  format:
    description:
      - Target drive's backing file's data format.
      - Used only with clone.
      - Use O(format=unspecified) and O(full=false) for a linked clone.
      - Please refer to the Proxmox VE Administrator Guide, section Proxmox VE Storage (see U(https://pve.proxmox.com/pve-docs/chapter-pvesm.html)
        for the latest version, tables 3 to 14) to find out format supported by the provided storage backend.
      - Not specifying this option is equivalent to setting it to V(unspecified).
    type: str
    choices: ["cloop", "cow", "qcow", "qcow2", "qed", "raw", "vmdk", "unspecified"]
  freeze:
    description:
      - Specify if PVE should freeze CPU at startup (use 'c' monitor command to start execution).
    type: bool
  full:
    description:
      - Create a full copy of all disk. This is always done when you clone a normal VM.
      - For VM templates, we try to create a linked clone by default.
      - Used only with clone.
    type: bool
    default: true
  hookscript:
    description:
      - Script that will be executed during various steps in the containers lifetime.
    type: str
  hostpci:
    description:
      - Specify a hash/dictionary of map host pci devices into guest. O(hostpci='{"key":"value", "key":"value"}').
      - Keys allowed are - C(hostpci[n]) where 0 ≤ n ≤ N.
      - Values allowed are - V("host="HOSTPCIID[;HOSTPCIID2...]",pcie="1|0",rombar="1|0",x-vga="1|0"").
      - The C(host) parameter is Host PCI device pass through. HOSTPCIID syntax is V(bus:dev.func) (hexadecimal numbers).
      - V(pcie=boolean) V(default=0) Choose the PCI-express bus (needs the q35 machine model).
      - V(rombar=boolean) V(default=1) Specify whether or not the device's ROM will be visible in the guest's memory map.
      - V(x-vga=boolean) V(default=0) Enable vfio-vga device support.
      - /!\ This option allows direct access to host hardware. So it is no longer possible to migrate such machines - use
        with special care.
    type: dict
  hotplug:
    description:
      - Selectively enable hotplug features.
      - This is a comma separated list of hotplug features V(network), V(disk), V(cpu), V(memory), and V(usb).
      - Value 0 disables hotplug completely and value 1 is an alias for the default V(network,disk,usb).
    type: str
  hugepages:
    description:
      - Enable/disable hugepages memory.
    type: str
    choices: ['any', '2', '1024']
  ide:
    description:
      - A hash/dictionary of volume used as IDE hard disk or CD-ROM. O(ide='{"key":"value", "key":"value"}').
      - Keys allowed are - V(ide[n]) where 0 ≤ n ≤ 3.
      - Values allowed are - V("storage:size,format=value").
      - V(storage) is the storage identifier where to create the disk.
      - V(size) is the size of the disk in GB.
      - V(format) is the drive's backing file's data format. V(qcow2|raw|subvol). Please refer to the Proxmox VE Administrator
        Guide, section Proxmox VE Storage (see U(https://pve.proxmox.com/pve-docs/chapter-pvesm.html) for the latest version,
        tables 3 to 14) to find out format supported by the provided storage backend.
    type: dict
  ipconfig:
    description:
      - 'Cloud-init: Set the IP configuration.'
      - A hash/dictionary of network IP configurations. O(ipconfig='{"key":"value", "key":"value"}').
      - Keys allowed are - V(ipconfig[n]) where 0 ≤ n ≤ network interfaces.
      - Values allowed are - V("[gw=<GatewayIPv4>] [,gw6=<GatewayIPv6>] [,ip=<IPv4Format/CIDR>] [,ip6=<IPv6Format/CIDR>]").
      - 'Cloud-init: Specify IP addresses and gateways for the corresponding interface.'
      - IP addresses use CIDR notation, gateways are optional but they should be in the same subnet of specified IP address.
      - The special string V(dhcp) can be used for IP addresses to use DHCP, in which case no explicit gateway should be provided.
      - For IPv6 the special string V(auto) can be used to use stateless autoconfiguration.
      - If cloud-init is enabled and neither an IPv4 nor an IPv6 address is specified, it defaults to using dhcp on IPv4.
    type: dict
  keyboard:
    description:
      - Sets the keyboard layout for VNC server.
    type: str
  kvm:
    description:
      - Enable/disable KVM hardware virtualization.
    type: bool
  localtime:
    description:
      - Sets the real time clock to local time.
      - This is enabled by default if ostype indicates a Microsoft OS.
    type: bool
  lock:
    description:
      - Lock/unlock the VM.
    type: str
    choices: ['migrate', 'backup', 'snapshot', 'rollback']
  machine:
    description:
      - Specifies the Qemu machine type.
      - Type => V((pc|pc(-i440fx\)?-\\d+\\.\\d+(\\.pxe\)?|q35|pc-q35-\\d+\\.\\d+(\\.pxe\)?\)).
    type: str
  memory:
    description:
      - Memory size in MB for instance.
    type: int
  migrate:
    description:
      - Migrate the VM to O(node) if it is on another node.
    type: bool
    default: false
  migrate_downtime:
    description:
      - Sets maximum tolerated downtime (in seconds) for migrations.
    type: int
  migrate_speed:
    description:
      - Sets maximum speed (in MB/s) for migrations.
      - A value of 0 is no limit.
    type: int
  name:
    description:
      - Specifies the VM name. Name could be non-unique across the cluster.
      - Required only for O(state=present).
      - With O(state=present) if O(vmid) not provided and VM with name exists in the cluster then no changes will be made.
    type: str
  nameservers:
    description:
      - 'Cloud-init: DNS server IP address(es).'
      - If unset, PVE host settings are used.
    type: list
    elements: str
  net:
    description:
      - A hash/dictionary of network interfaces for the VM. O(net='{"key":"value", "key":"value"}').
      - Keys allowed are - C(net[n]) where 0 ≤ n ≤ N.
      - Values allowed are - C("model="XX:XX:XX:XX:XX:XX",bridge="value",rate="value",tag="value",firewall="1|0",trunks="vlanid"").
      - Model is one of C(e1000 e1000-82540em e1000-82544gc e1000-82545em i82551 i82557b i82559er ne2k_isa ne2k_pci pcnet
        rtl8139 virtio vmxnet3).
      - C(XX:XX:XX:XX:XX:XX) should be an unique MAC address. This is automatically generated if not specified.
      - The C(bridge) parameter can be used to automatically add the interface to a bridge device. The Proxmox VE standard
        bridge is called 'vmbr0'.
      - Option C(rate) is used to limit traffic bandwidth from and to this interface. It is specified as floating point number,
        unit is 'Megabytes per second'.
      - If you specify no bridge, we create a kvm 'user' (NATed) network device, which provides DHCP and DNS services.
    type: dict
  newid:
    description:
      - VMID for the clone. Used only with clone.
      - If newid is not set, the next available VM ID will be fetched from ProxmoxAPI.
    type: int
  numa:
    description:
      - A hash/dictionaries of NUMA topology. O(numa='{"key":"value", "key":"value"}').
      - Keys allowed are - V(numa[n]) where 0 ≤ n ≤ N.
      - Values allowed are - V("cpu="<id[-id];...>",hostnodes="<id[-id];...>",memory="number",policy="(bind|interleave|preferred)"").
      - V(cpus) CPUs accessing this NUMA node.
      - V(hostnodes) Host NUMA nodes to use.
      - V(memory) Amount of memory this NUMA node provides.
      - V(policy) NUMA allocation policy.
    type: dict
  numa_enabled:
    description:
      - Enables NUMA.
    type: bool
  onboot:
    description:
      - Specifies whether a VM will be started during system bootup.
    type: bool
  ostype:
    description:
      - Specifies guest operating system. This is used to enable special optimization/features for specific operating systems.
      - The l26 is Linux 2.6/3.X Kernel.
    type: str
    choices: ['other', 'wxp', 'w2k', 'w2k3', 'w2k8', 'wvista', 'win7', 'win8', 'win10', 'win11', 'l24', 'l26', 'solaris']
  parallel:
    description:
      - A hash/dictionary of map host parallel devices. O(parallel='{"key":"value", "key":"value"}').
      - Keys allowed are - (parallel[n]) where 0 ≤ n ≤ 2.
      - Values allowed are - C("/dev/parport\d+|/dev/usb/lp\d+").
    type: dict
  protection:
    description:
      - Enable/disable the protection flag of the VM. This will enable/disable the remove VM and remove disk operations.
    type: bool
  reboot:
    description:
      - Allow reboot. If set to V(true), the VM exit on reboot.
    type: bool
  revert:
    description:
      - Revert a pending change.
    type: str
  rng0:
    description: Randomness source for RNG device, for example V(/dev/urandom).
    type: str
    version_added: 1.0.0
  sata:
    description:
      - A hash/dictionary of volume used as sata hard disk or CD-ROM. O(sata='{"key":"value", "key":"value"}').
      - Keys allowed are - C(sata[n]) where 0 ≤ n ≤ 5.
      - Values allowed are - C("storage:size,format=value").
      - C(storage) is the storage identifier where to create the disk.
      - C(size) is the size of the disk in GB.
      - C(format) is the drive's backing file's data format. C(qcow2|raw|subvol). Please refer to the Proxmox VE Administrator
        Guide, section Proxmox VE Storage (see U(https://pve.proxmox.com/pve-docs/chapter-pvesm.html) for the latest version,
        tables 3 to 14) to find out format supported by the provided storage backend.
    type: dict
  scsi:
    description:
      - A hash/dictionary of volume used as SCSI hard disk or CD-ROM. O(scsi='{"key":"value", "key":"value"}').
      - Keys allowed are - C(scsi[n]) where 0 ≤ n ≤ 13.
      - Values allowed are - C("storage:size,format=value").
      - C(storage) is the storage identifier where to create the disk.
      - C(size) is the size of the disk in GB.
      - C(format) is the drive's backing file's data format. C(qcow2|raw|subvol). Please refer to the Proxmox VE Administrator
        Guide, section Proxmox VE Storage (see U(https://pve.proxmox.com/pve-docs/chapter-pvesm.html) for the latest version,
        tables 3 to 14) to find out format supported by the provided storage backend.
    type: dict
  scsihw:
    description:
      - Specifies the SCSI controller model.
    type: str
    choices: ['lsi', 'lsi53c810', 'virtio-scsi-pci', 'virtio-scsi-single', 'megasas', 'pvscsi']
  searchdomains:
    description:
      - 'Cloud-init: Sets DNS search domain(s).'
      - If unset, PVE host settings are used.
    type: list
    elements: str
  serial:
    description:
      - A hash/dictionary of serial device to create inside the VM. V('{"key":"value", "key":"value"}').
      - Keys allowed are - serial[n](str; required) where 0 ≤ n ≤ 3.
      - Values allowed are - V((/dev/.+|socket\)).
      - /!\ If you pass through a host serial device, it is no longer possible to migrate such machines - use with special
        care.
    type: dict
  shares:
    description:
      - Rets amount of memory shares for auto-ballooning. (0 - 50000).
      - The larger the number is, the more memory this VM gets.
      - The number is relative to weights of all other running VMs.
      - Using 0 disables auto-ballooning, this means no limit.
    type: int
  skiplock:
    description:
      - Ignore locks.
      - Only root is allowed to use this option.
    type: bool
  smbios:
    description:
      - Specifies SMBIOS type 1 fields.
      - Comma separated, Base64 encoded (optional) SMBIOS properties:.
      - V([base64=<1|0>] [,family=<Base64 encoded string>]).
      - V([,manufacturer=<Base64 encoded string>]).
      - V([,product=<Base64 encoded string>]).
      - V([,serial=<Base64 encoded string>]).
      - V([,sku=<Base64 encoded string>]).
      - V([,uuid=<UUID>]).
      - V([,version=<Base64 encoded string>]).
    type: str
  snapname:
    description:
      - The name of the snapshot. Used only with clone.
    type: str
  sockets:
    description:
      - Sets the number of CPU sockets. (1 - N).
    type: int
  sshkeys:
    description:
      - 'Cloud-init: SSH key to assign to the default user. NOT TESTED with multiple keys but a multi-line value should work.'
    type: str
  startdate:
    description:
      - Sets the initial date of the real time clock.
      - Valid format for date are V('now') or V('2016-09-25T16:01:21') or V('2016-09-25').
    type: str
  startup:
    description:
      - Startup and shutdown behavior. V([[order=]\\d+] [,up=\\d+] [,down=\\d+]).
      - Order is a non-negative number defining the general startup order.
      - Shutdown in done with reverse ordering.
    type: str
  state:
    description:
      - Indicates desired state of the instance.
      - If V(current), the current state of the VM will be fetched. You can access it with C(results.status).
    type: str
    choices: ['present', 'started', 'absent', 'stopped', 'restarted', 'current', 'template', 'paused', 'hibernated']
    default: present
  storage:
    description:
      - Target storage for full clone.
    type: str
  tablet:
    description:
      - Enables/disables the USB tablet device.
    type: bool
  tags:
    description:
      - List of tags to apply to the VM instance.
      - Tags must start with V([a-z0-9_]) followed by zero or more of the following characters V([a-z0-9_-+.]).
      - Tags are only available in Proxmox 6+.
    type: list
    elements: str
  target:
    description:
      - Target node. Only allowed if the original VM is on shared storage.
      - Used only with clone.
    type: str
  tdf:
    description:
      - Enables/disables time drift fix.
    type: bool
  template:
    description:
      - Enables/disables the template.
    type: bool
  timeout:
    description:
      - Timeout for operations.
      - When used with O(state=stopped) the option sets a graceful timeout for VM stop after which a VM will be forcefully
        stopped.
    type: int
    default: 30
  tpmstate0:
    description:
      - A hash/dictionary of options for the Trusted Platform Module disk.
      - A TPM state disk is required for Windows 11 installations.
    suboptions:
      storage:
        description:
          - O(tpmstate0.storage) is the storage identifier where to create the disk.
        type: str
        required: true
      version:
        description:
          - The TPM version to use.
        type: str
        choices: ['1.2', '2.0']
        default: '2.0'
    type: dict
  usb:
    description:
      - A hash/dictionary of USB devices for the VM. O(usb='{"key":"value", "key":"value"}').
      - Keys allowed are - C(usb[n]) where 0 ≤ n ≤ N.
      - Values allowed are - C(host="value|spice",mapping="value",usb3="1|0").
      - Host is either C(spice) or the USB id/port.
      - Option C(mapping) is the mapped USB device name.
      - Option C(usb3) enables USB 3 support.
    type: dict
  update:
    description:
      - If V(true), the VM will be updated with new value.
      - Because of the operations of the API and security reasons, I have disabled the update of the following parameters
        O(net), O(virtio), O(ide), O(sata), O(scsi). Per example updating O(net) update the MAC address and O(virtio) create
        always new disk... This security feature can be disabled by setting the O(update_unsafe) to V(true).
      - Update of O(pool) is disabled. It needs an additional API endpoint not covered by this module.
    type: bool
    default: false
  update_unsafe:
    description:
      - If V(true), do not enforce limitations on parameters O(net), O(virtio), O(ide), O(sata), O(scsi), O(efidisk0), and
        O(tpmstate0). Use this option with caution because an improper configuration might result in a permanent loss of data
        (for example disk recreated).
    type: bool
    default: false
  vcpus:
    description:
      - Sets number of hotplugged vcpus.
    type: int
  vga:
    description:
      - Select VGA type. If you want to use high resolution modes (>= 1280x1024x16) then you should use option V(std) or V(vmware).
    type: str
    choices: ['std', 'cirrus', 'vmware', 'qxl', 'serial0', 'serial1', 'serial2', 'serial3', 'qxl2', 'qxl3', 'qxl4']
  virtio:
    description:
      - A hash/dictionary of volume used as VIRTIO hard disk. O(virtio='{"key":"value", "key":"value"}').
      - Keys allowed are - V(virtio[n]) where 0 ≤ n ≤ 15.
      - Values allowed are - V(storage:size,format=value).
      - V(storage) is the storage identifier where to create the disk.
      - V(size) is the size of the disk in GB.
      - V(format) is the drive's backing file's data format. V(qcow2|raw|subvol). Please refer to the Proxmox VE Administrator
        Guide, section Proxmox VE Storage (see U(https://pve.proxmox.com/pve-docs/chapter-pvesm.html) for the latest version,
        tables 3 to 14) to find out format supported by the provided storage backend.
    type: dict
  watchdog:
    description:
      - Creates a virtual hardware watchdog device.
    type: str
seealso:
  - module: community.proxmox.proxmox_vm_info
extends_documentation_fragment:
  - community.proxmox.proxmox.actiongroup_proxmox
  - community.proxmox.proxmox.documentation
  - community.proxmox.proxmox.selection
  - community.proxmox.attributes
a  
- name: Create new VM with minimal options
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    name: spynal
    node: sabrewulf

- name: Create a VM from archive (backup)
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    archive: backup-storage:backup/vm/140/2023-03-08T06:41:23Z
    name: spynal

- name: Create new VM with minimal options and given vmid
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    name: spynal
    node: sabrewulf
    vmid: 100

- name: Create new VM with two network interface options
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    name: spynal
    node: sabrewulf
    net:
      net0: 'virtio,bridge=vmbr1,rate=200'
      net1: 'e1000,bridge=vmbr2'

- name: Create new VM with one network interface, three virto hard disk, 4 cores, and 2 vcpus
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    name: spynal
    node: sabrewulf
    net:
      net0: 'virtio,bridge=vmbr1,rate=200'
    virtio:
      virtio0: 'VMs_LVM:10'
      virtio1: 'VMs:2,format=qcow2'
      virtio2: 'VMs:5,format=raw'
    cores: 4
    vcpus: 2

- name: Create VM with 1 10GB SATA disk and an EFI disk, with Secure Boot disabled by default
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    name: spynal
    node: sabrewulf
    sata:
      sata0: 'VMs_LVM:10,format=raw'
    bios: ovmf
    efidisk0:
      storage: VMs_LVM_thin
      format: raw
      efitype: 4m
      pre_enrolled_keys: false

- name: Create VM with 1 10GB SATA disk and an EFI disk, with Secure Boot enabled by default
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    name: spynal
    node: sabrewulf
    sata:
      sata0: 'VMs_LVM:10,format=raw'
    bios: ovmf
    efidisk0:
      storage: VMs_LVM
      format: raw
      efitype: 4m
      pre_enrolled_keys: 1

- name: >
    Clone VM with only source VM name.
    The VM source is spynal.
    The target VM name is zavala
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    clone: spynal
    name: zavala
    node: sabrewulf
    storage: VMs
    format: qcow2
    timeout: 500

- name: >
    Create linked clone VM with only source VM name.
    The VM source is spynal.
    The target VM name is zavala
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    clone: spynal
    name: zavala
    node: sabrewulf
    storage: VMs
    full: false
    format: unspecified
    timeout: 500

- name: Clone VM with source vmid and target newid and raw format
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    clone: arbitrary_name
    vmid: 108
    newid: 152
    name: zavala
    node: sabrewulf
    storage: LVM_STO
    format: raw
    timeout: 300

- name: Create new VM and lock it for snapshot
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    name: spynal
    node: sabrewulf
    lock: snapshot

- name: Create new VM and set protection to disable the remove VM and remove disk operations
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    name: spynal
    node: sabrewulf
    protection: true

- name: Create new VM using cloud-init with a username and password
  community.proxmox.proxmox_kvm:
    node: sabrewulf
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    name: spynal
    ide:
      ide2: 'local:cloudinit,format=qcow2'
    ciuser: mylinuxuser
    cipassword: supersecret
    searchdomains: 'mydomain.internal'
    nameservers: 1.1.1.1
    net:
      net0: 'virtio,bridge=vmbr1,tag=77'
    ipconfig:
      ipconfig0: 'ip=192.168.1.1/24,gw=192.168.1.1'

- name: Create new VM using Cloud-Init with an ssh key
  community.proxmox.proxmox_kvm:
    node: sabrewulf
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    name: spynal
    ide:
      ide2: 'local:cloudinit,format=qcow2'
    sshkeys: 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILJkVm98B71lD5XHfihwcYHE9TVpsJmK1vR1JcaU82L+'
    searchdomains: 'mydomain.internal'
    nameservers:
      - '1.1.1.1'
      - '8.8.8.8'
    net:
      net0: 'virtio,bridge=vmbr1,tag=77'
    ipconfig:
      ipconfig0: 'ip=192.168.1.1/24'

- name: Start VM
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    name: spynal
    node: sabrewulf
    state: started

- name: Stop VM
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    name: spynal
    node: sabrewulf
    state: stopped

- name: Stop VM with force
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    name: spynal
    node: sabrewulf
    state: stopped
    force: true

- name: Restart VM
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    name: spynal
    node: sabrewulf
    state: restarted

- name: Convert VM to template
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    name: spynal
    node: sabrewulf
    state: template

- name: Convert VM to template (stop VM if running)
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    name: spynal
    node: sabrewulf
    state: template
    force: true

- name: Remove VM
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    name: spynal
    node: sabrewulf
    state: absent

- name: Get VM current state
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    name: spynal
    node: sabrewulf
    state: current

- name: Update VM configuration
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    name: spynal
    node: sabrewulf
    cores: 8
    memory: 16384
    update: true

- name: Update VM configuration (incl. unsafe options)
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    name: spynal
    node: sabrewulf
    cores: 8
    memory: 16384
    net:
      net0: virtio,bridge=vmbr1
    update: true
    update_unsafe: true

- name: Delete QEMU parameters
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    name: spynal
    node: sabrewulf
    delete: 'args,template,cpulimit'

- name: Revert a pending change
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    name: spynal
    node: sabrewulf
    revert: 'template,cpulimit'

- name: Migrate VM on second node
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    name: spynal
    node: sabrewulf-2
    migrate: true

- name: Add hookscript to existing VM
  community.proxmox.proxmox_kvm:
    api_user: root@pam
    api_password: secret
    api_host: helldorado
    vmid: 999
    node: sabrewulf
    hookscript: local:snippets/hookscript.pl
    update: true
aV  
vmid:
  description: The VM vmid.
  returned: success
  type: int
  sample: 115
status:
  description: The current virtual machine status.
  returned: success, not clone, not absent, not update
  type: str
  sample: running
msg:
  description: A short message.
  returned: always
  type: str
  sample: "VM kropta with vmid = 110 is running"
N)quote)LooseVersion)proxmox_auth_argument_specProxmoxAnsible)AnsibleModule)booleanc                 L    t        j                  d|       j                  d      S )Nz=(.*?),   researchgroup)netstrs    q/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/proxmox/plugins/modules/proxmox_kvm.py	parse_macr     s    99Y'--a00    c                 L    t        j                  d|       j                  d      S )Nz
(.*?)(,|$)r   r   )devstrs    r   	parse_devr     s    99\6*0033r   c                   H    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)ProxmoxKvmAnsiblec                 D   i a i }i }	 | j                  j                  |      j                  |      j                  j                         }|j                         D 	ci c]  \  }}	|		||	 }}}	t        |j                               D ]-  }t        ||   t              s|j                  ||          ||= / t        j                   d      }
t        j                   d      }|j                         D ]H  }|
j#                  |      rt%        |         ||<   &|j#                  |      s8t'        |         ||<   J |t         d<   |t         d<   t)        |      t         d<   y # t        $ r-}| j                  j                  d|d|       Y d }~Jd }~ww xY wc c}	}w )	Nz'Getting information for VM with vmid =  failed with exception: msgznet[0-9]z#(virtio|ide|scsi|sata|efidisk)[0-9]macdevicesvmid)resultsproxmox_apinodesqemuconfigget	Exceptionmodule	fail_jsonitemslistkeys
isinstancedictupdater   compilematchr   r   int)selfnoder!   kwargsr   r    vmekvre_netre_devs               r   
get_vminfozProxmoxKvmAnsible.get_vminfo  sw   	y!!''-2248??CCEB
 $*<<>C41aQ]!Q$CC fkkm$ 	A&)T*fQi(1I	 K(BC 	.A||A"2a5)Aa&r!u-
		. $	d)1  	yKK!!osuv&w!xx	y Ds$   AE# 
F(F#	F,"FFc                     | j                   j                  |      }|j                         D ci c]  \  }}|	|| }}} |j                  |      j                  j
                  di |d u S c c}}w )N )r#   r$   r+   r%   r&   set)r4   r!   r5   r6   proxmox_noder9   r:   s          r   settingszProxmoxKvmAnsible.settings  sq    ''--d3 $*<<>C41aQ]!Q$CC1|  &--11;F;tCC Ds
   
A/A/c                    | j                   j                  d   }| j                   j                  d   dk(  r|dz  }|rL| j                  ||      rt        j                  d       y|dz
  }|dk(  r	 yt        j                  d       |rLy)	Ntimeoutstatestopped
   r   Tr   F)r)   paramsapi_task_oktimesleep)r4   r5   taskidrD   s       r   wait_for_taskzProxmoxKvmAnsible.wait_for_task  s    ++$$Y/;;g&)3rMGf-

1kG!| JJqM  r   c                    g d}g d}dg}g d}i }dj                  |      }| j                  j                  |      }|j                         D ci c]  \  }}|	|| }}}|j	                  |j                         D ci c]"  \  }}t        |t              s|t        |      $ c}}       | j                         }|t        d      k  rdn|j                  d   }|d	k  r|D ]
  }||v s||=  |d
k  r|D ]
  }||v s||=  |dk  r|D ]
  }||v s||=  d|v rt        |d   d      }t        |      |d<   |	rC|
du r1d|v r|d= d|v r|d= d|v r|d= d|v r|d= d|v r|d= d|v r|d= d|v r|d= d|v r|d= d|v r|d= d|v r(d|vsd|d   k7  r| j                  j                  d       d|v rd}t        j                  d      }d|d   v r.||d   j!                  d      dz   z  }|d   j#                  d       |d j%                  |d   j                         D cg c]+  \  }}d|k7  r!|j'                  d!|      d"z   t        |      z   - c}}      z  }||d<   d|v r:d#j                  |d   j!                  d      |d   j!                  d$      %      |d<   t)        |j+                               D ]-  }t        ||   t,              s|j	                  ||          ||= / d&|v r	 t        t/        |d&   d'(            |d&<   d)|v r|d)   |d*<   |d)= d+| j                  j2                  v r;| j                  j2                  j#                  d+      }|rd,j%                  |      |d-<   d.| j                  j2                  v r;| j                  j2                  j#                  d.      }|rd,j%                  |      |d/<   d0|v rgt        j                  d1      }|d0   D ]3  }|j5                  |      r| j                  j                  d2|z         5 d j%                  |d0         |d0<   | j                  j2                  d3   d4k(  r6| j                  j2                  d5   | j                  j2                  d5   |d5<   nQ| j                  j2                  d3   d4k7  r5| j                  j2                  d5   | j                  j                  d6       | j                  j2                  d3   d4k7  r5| j                  j2                  d7   | j                  j                  d8       |	r3 |j7                  |      j8                  j:                  d?|||||d9|y'y| j                  j2                  d:   |D ]8  }| j                  j2                  |   | j                  j2                  |   ||<   : |j	                  |j                         D ci c]"  \  }}t        |t              s|t        |      $ c}}        |j7                  |      j<                  j>                  d?||d;|} n# |j6                  j@                  d?||||||d<|} | jC                  ||       sJ| j                  j                  d=|jE                  |       jF                  j!                         d d> z         yy'c c}}w c c}}w c c}}w # t0        $ r Y w xY wc c}}w )@N)force
protectionskiplock)ciuser
cipasswordsshkeysipconfigtags	ciupgrade)formatfullpoolsnapnamestoragetargetz:-serial unix:/var/run/qemu-server/{0}.serial,server,nowaitz4.0   r            rT    )safeFvirtiosatascsiideefidisk0	tpmstate0netrO   rZ   biosovmfz4efidisk0 cannot be used if bios is not set to ovmf. r   _r\   z:1,,-=z{storage}:1,version=v{version}versionr\   rq   agentT)strictnuma_enablednumanameservers 
nameserversearchdomainssearchdomainrV   z!^[a-zA-Z0-9_][a-zA-Z0-9_\-\+\.]*$z%s is not a valid tagapi_userzroot@pamargsz&args parameter require root@pam user. rQ   z*skiplock parameter require root@pam user. )namememorycpucoressocketsclone)newidr~   )r!   r~   r   r   r   r   zSReached timeout while waiting for creating VM. Last line in task before timeout: %sr   r?   )$rX   r#   r$   r+   r0   r.   boolr3   rq   r   r   strr)   r*   r   r1   r'   popjoinsubr,   r-   r/   r   	TypeErrorrH   r2   r%   r&   r@   r   postcreaterM   taskslog)!r4   r!   r   r5   r~   r   r   r   r   r0   update_unsafer6   only_v4only_v6only_v8valid_clone_paramsclone_paramsvm_argsrA   r9   r:   rq   pve_major_versionpurlencoded_ssh_keysefidisk0_str	hyphen_rerw   rz   re_tagtagparamrL   s!                                    r   	create_vmzProxmoxKvmAnsible.create_vm  sG   5I- YNUUVZ[''--d3 $*<<>C41aQ]!Q$CCV\\^STQz!T?Rq#a&yST,,.!(<+>!>AGOOTUDV q  ";q	"
 q  ";q	"
 q  ";q	"
 "'y(9"C #$7 8F9 %v%x(V#vV#vF?u'z*&({+F?u& 7O6N f$&F6N*B%%*`%a L

4(IF:..z 2 6 6y AE IIz"&&y1 CHHW]^hWiWoWoWq &9tqRS)2a '0mmC&;c&ACF&J &9 : :L!-F: & "B"I"I{+//	:{+//	: #J #F; fkkm$ 	A&)T*fQi(1I	
 f"%gfWod&K"Lw V##N3F6N~& DKK...++,,00?K'*xx'<|$dkk000 KK..22?CM),-)@~& VZZ DEFf~ M||C(KK)).E.K)LM !XXfVn5F6N ;;j)Z7DKK<N<Nv<V<b![[//7F6N[[
+z9dkk>P>PQW>X>dKK!!&N!O;;j)Z7DKK<N<Nz<Z<fKK!!&R!S1|  &--11|tFPS[`jq|u{|  E[[(4+ D;;%%e,8*.++*<*<U*CL'D |7I7I7K ctq!zZ[]aObCF cd7\&&t,2277_e$_R^_F-\&&--  D4d6WZbgqx  D  }C  DF!!$/KK!!&{"."4"4V"<"@"@"D"D"Fr"J'K! La DSF&9,  Z !ds<   
Z#Z#Z)
Z)
!0Z/,Z5 [
[
5	[[c                 n   |d   }| j                   j                  |d         }|j                  |      j                  j                  j                         }| j                  |d   |      sJ| j                  j                  d|j                  |      j                  j                         d d z         yy)Nr!   r5   zSReached timeout while waiting for starting VM. Last line in task before timeout: %sr   r   FT)r#   r$   r%   statusstartr   rM   r)   r*   r   r   r'   )r4   r7   r!   rA   rL   s        r   start_vmzProxmoxKvmAnsible.start_vmx  s    &z''--bj9""4(//55::<!!"V*f5KK!!&{"."4"4V"<"@"@"D"D"Fr"J'K! Lr   c                 |   |d   }| j                   j                  |d         }|j                  |      j                  j                  j                  |rdnd|      }| j                  |d   |      sJ| j                  j                  d|j                  |      j                  j                         d d z         yy	)
Nr!   r5   r   r   )	forceStoprD   zSReached timeout while waiting for stopping VM. Last line in task before timeout: %sr   FT)r#   r$   r%   r   shutdownr   rM   r)   r*   r   r   r'   )r4   r7   rO   rD   r!   rA   rL   s          r   stop_vmzProxmoxKvmAnsible.stop_vm  s    &z''--bj9""4(//88==eYZel=m!!"V*f5KK!!&{"."4"4V"<"@"@"D"D"Fr"J'K! Lr   c                 L   |d   }	 | j                   j                  |d         }|r3|j                  |      j                  j                  j                         n2|j                  |      j                  j                  j                         }| j                  |d   |      sJ| j                  j                  d|j                  |      j                  j                         d d z         yy# t        $ r-}| j                  j                  |d|d	|
       Y d }~yd }~ww xY w)Nr!   r5   zTReached timeout while waiting for rebooting VM. Last line in task before timeout: %sr   r   FTzrestarting of VM r   r!   r   )r#   r$   r%   r   resetr   rebootrM   r)   r*   r   r   r'   r(   )r4   r7   rO   r   r!   rA   rL   r8   s           r   
restart_vmzProxmoxKvmAnsible.restart_vm  s   &z
	++11"V*=LDI\&&t,3399>>@|O`O`aeOfOmOmOtOtOyOyO{F%%bj&9%%  +A*6*<*<V*D*H*H*L*L*NrPQ*R+S% T 	KK!!tdhjk1l!m	s   C$C- -	D#6#DD#c                    |d   }	 | j                   j                  |d         }|j                  |      j                  j                  j                         d   dk(  r|r| j                  ||||       |j                  |      j                  j                          y# t        $ r-}| j                  j                  |d|d|       Y d }~y	d }~ww xY w)
Nr!   r5   r   runningTconversion of VM $ to template failed with exception: r   F)r#   r$   r%   r   currentr'   stop_instancetemplater   r(   r)   r*   )r4   r7   rD   rO   r!   rA   r8   s          r   convert_to_templatez%ProxmoxKvmAnsible.convert_to_template  s    &z		++11"V*=L  &--5599;HERW\""2tWe<d#,,113 	KK!!tptvw1x!y	s   BB 	C'#CCc                 j   |d   }| j                   j                  |d         }|j                  |      j                  j	                  ||d   |d      }| j                  |d   |      sJ| j                  j                  d|j                  |      j                  j                         d d z         yy)	Nr!   r5   r   )r!   r5   r]   onlinezTReached timeout while waiting for migrating VM. Last line in task before timeout: %sr   FT)r#   r$   r%   migrater   rM   r)   r*   r   r   r'   )r4   r7   target_noder!   rA   rL   s         r   
migrate_vmzProxmoxKvmAnsible.migrate_vm  s    &z''--bj9""4(00554bjYdmn5o!!"V*f5KK!!&|"."4"4V"<"@"@"D"D"Fr"J'K! Lr   c                 |   |d   }| j                   j                  |d         }|j                  |      j                  j                  j                  |rdnd|      }| j                  |d   |      sJ| j                  j                  d|j                  |      j                  j                         d d z         yy	)
Nr!   r5   r   r   )todiskrD   zUReached timeout while waiting for suspending VM. Last line in task before timeout: %sr   FT)r#   r$   r%   r   suspendr   rM   r)   r*   r   r   r'   )r4   r7   rD   r   r!   rA   rL   s          r   
suspend_vmzProxmoxKvmAnsible.suspend_vm  s    &z''--bj9""4(//77<<&QVWbi<j!!"V*f5KK!!&}"."4"4V"<"@"@"D"D"Fr"J'K! Lr   N)__name__
__module____qualname__r=   rB   rM   r   r   r   r   r   r   r   r?   r   r   r   r     s8    $BD"_Br   r   c                  -   t               } t        di dt        d      dt        d      dt        d      dt        d      dt        d	      d
t        d      dt        d      dt        ddg      dt        d      dt        d      dt        d      dt        dd      dt        dddg      dt        d      dt        d      dt        d      dt        d      dt        d      d t        d      d!t        d      d"t        d      d#t        d      d$t        d      d%t        d	t        t        d      t        d      t        dd&d'g      t        d      (      )      d*t        d      d+t        dg d,      d-t        d      d.t        dd/      d0t        d      d1t        d	      d2t        d      d3t        g d4      d5t        d	      d6t        d	      d7t        d      d8t        d      d9t        d      d:t        g d;      d<t        d      d=t        d      d>t        dd?/      d@t        d      dAt        d      dBt        d      dCt        dDdE      dFt        d	      dGt        d      dHt               dIt        d	      dJt        d      dKt        d      dLt        g dM      dNt        d	      dOt        d      dPt        d      dQt        d      dRt        d      dSt        d      dTt        d	      dUt        d	      dVt        g dW      dXt        d	      dYt        dDdE      dZt        d      d[t        d      d\t        d      d]t        d      d^t        d      d_t        dd?      d`t        d      dat               dbt        dcg dde      dft        d      dgt        d      dht        dDdE      dit        d      djt        d      dkt        d      dlt        ddm/      dnt        d	t        t        ddo      t        ddpdqgdpr      s      )      dtt        d	      dut        dd?/      dvt        dd?/      dwt        d      dxt        g dy      dzt        d	      d{t        d      d|t               }| j                  |       t        | g d}d~gddgdbdcdHgfg      }|j                  d   }|j                  d   }|j                  d   }|j                  d"   }|j                  d>   }|j                  d=   }|j                  dB   }	|j                  dG   }
|j                  dH   }|j                  dR   }|j                  d^   }|j                  db   }t        |j                  du         }t        |j                  dv         }|j                  d{   }|j                  d   }|j                  d+   dk(  rd |j                  d+<   t        |      }|sR|dck(  r4|s2|s0|s.|s,|s*|j                  |	d      }|r|}n+	 |j                         }n|xs |	}|j                  |d      }|i|
s	 |j                         }
|s|j                  d|z         |j                  |       |j                  |
d      r|j                  d?|d|
d|	d       |9	 |j                  |||       |j                  d|dj                  |	|             |9	 |j                  |||       |j                  d|dj                  |	|             |rx	 |j                  |      }|dH   }||k7  r8|j!                  ||       |j                  d|dj                  |||             n$|j                  d?|dj                  ||             |dck(  r|s-|s+|j                  |d      r|j                  d?|d|z         n|s>|s<|s:|j                  |	d      r'|j                  d?|j                  |	      d|	z         n]|s|j                  d       nH|r t#        ||	g      s|j                  d       n&|j%                  |      s|j                  d|z         	  |j&                  ||
||	||||||f
i d|j                  d   d|j                  d   d|j                  d   d|j                  d   d
|j                  d
   d|j                  d   d|j                  d   d|j                  d   d|j                  d   d|j                  d   d|j                  d   d|j                  d   d|j                  d   d|j                  d   d |j                  d    d!|j                  d!   d#|j                  d#   d$|j                  d$   d%|j                  d%   d*|j                  d*   d-|j                  d-   d0|j                  d0   d1|j                  d1   d2|j                  d2   d3|j                  d3   d5|j                  d5   d6|j                  d6   d7|j                  d7   d8|j                  d8   d9|j                  d9   d:|j                  d:   d<|j                  d<   d@|j                  d@   dA|j                  dA   dF|j                  dF   dI|j                  dI   dJ|j                  dJ   dK|j                  dK   dL|j                  dL   dN|j                  dN   dO|j                  dO   dP|j                  dP   dQ|j                  dQ   dS|j                  dS   dT|j                  dT   dU|j                  dU   dV|j                  dV   dX|j                  dX   dZ|j                  dZ   d[|j                  d[   d|j                  d\   d]|j                  d]   d_|j                  d_   d`|j                  d`   da|j                  da   dg|j                  dg   dh|j                  dh   di|j                  di   dj|j                  dj   dk|j                  dk   dn|j                  dn   dt|j                  dt   dw|j                  dw   dx|j                  dx   dz|j                  dz   d||j                  d|    |sY|j)                  |||j                  d5   |j                  dF   |j                  dT   |j                  dU   |j                  dz          |r|j                  d|d|	d|d       y ||j                  d|
d|	d|
d|       y  |j                  ddd|	d|ddt*         y |dk(  r|s|j                  d|	z         i }	 |j                  |      }|j,                  j/                  |dH         j1                  |      j2                  j4                  j7                         d   }||d<   |dk(  r |j                  dd?|d|z  d| |j9                  |      r |j                  dd|d|z  d| y y |dk(  r|s|j                  d|	z         i }	 |j                  |      }|j,                  j/                  |dH         j1                  |      j2                  j4                  j7                         d   }||d<   |dk(  r |j                  dd?|d|z  d| |j;                  ||j                  d*   |j                  dl           |j                  dd|d|z  d| y |dkk(  r|s|j                  d|	z         i }	 |j                  |      }|dk   dk(  r |j                  dd?|d|z  d| |j=                  ||j                  d*   |j                  dl         r |j                  dd|d|z  d| y y |dk(  r|s|j                  d|	z         i }|j                  |      }|j,                  j/                  |dH         j1                  |      j2                  j4                  j7                         d   }||d<   |dk(  r |j                  dd?|d|z  d| |j?                  ||j                  d*         r |j                  dd|d|z  d| y y |dk(  rri }|s|j                  d?d|	z         	 |j                  |d      }|s|j                  d?|       |j,                  j/                  |dH         }|j1                  |      j2                  j4                  j7                         d   }||d<   |dk(  rH|j                  d*   r"|j;                  |d|j                  dl          n|j                  d?|d|z         |j0                  jA                  |      }|jC                  |dH   |      s@|j                  d|jE                  |      jF                  j7                         d d z         n|j                  d|d|z         y y |dk(  ri }|s|j                  d|	z         |j                  |      }|	s|j7                  dBdƫ      }	|j,                  j/                  |dH         j1                  |      j2                  j4                  j7                         d   }||d<   |r  |j                  dd?|d|	d|d|d| y y |dv r|s|j                  d|	z         i }	 |j                  |      }|j,                  j/                  |dH         j1                  |      j2                  j4                  j7                         d   }||d<   |dk7  r |j                  dd?|d|z  d| |jI                  ||j                  d*   |j                  dl   |dk(  ˫        |j                  dd|d|z  d| y y # t        $ r% |j                  dj                  |	             Y w xY w# t        $ r% |j                  dj                  |	             Y w xY w# t        $ r:}|j                  |dj                  |	|      t        |      z          Y d }~yd }~ww xY w# t        $ r:}|j                  |dj                  |	|      t        |      z          Y d }~d }~ww xY w# t        $ r0}|j                  |dj                  |||             Y d }~Fd }~ww xY w# t        $ r}|r0|j                  |dj                  |	|      t        |      z          nN|0|j                  |dj                  |	|      t        |      z          n|j                  |d|	d|d|       Y d }~ud }~ww xY w# t        $ r%} |j                  d|d|d|d| Y d }~y d }~ww xY w# t        $ r%} |j                  d|d|d|d| Y d }~y d }~ww xY w# t        $ r%} |j                  d|d|d|d| Y d }~y d }~ww xY w# t        $ r"}|j                  d|d|       Y d }~y d }~ww xY w# t        $ r%} |j                  d|d|d|d| Y d }~y d }~ww xY w)Narchiver   )typeacpir   rs   r}   audior/   	autostartballoonr3   rk   seabiosrl   )choicesbootbootdiskcicustomrS   T)r   no_logcitypenocloudconfigdrive2)r   r   rW   rR   r   r   r   cpulimitcpuunitsdeletedescriptiondigestrh   2m4m)r\   rX   efitypepre_enrolled_keys)r   optionsrO   rX   )cloopcowqcowqcow2qedrawvmdkunspecifiedfreezerY   )r   default
hookscripthostpcihotplug	hugepages)any21024rg   rU   keyboardkvm	localtimelock)r   backupsnapshotrollbackmachiner   r   Fmigrate_downtimemigrate_speedr~   rw   r,   )r   elementsrj   r   r5   rv   ru   onbootostype)otherwxpw2kw2k3w2k8wvistawin7win8win10win11l24l26solarisparallelrZ   rP   r   revertrng0re   rf   scsihw)lsi	lsi53c810zvirtio-scsi-pcizvirtio-scsi-singlemegasaspvscsiserialrz   sharesrQ   smbiosr[   r   rT   	startdatestartuprE   present)	r  absentrF   started	restartedr   r   paused
hibernated)r   r   r\   tabletrV   r]   tdfr   rD      ri   )r   requiredz2.0z1.2)r   r   r   rr   usbr0   r   vcpusvga)stdcirrusvmwareqxlserial0serial1serial2serial3qxl2qxl3qxl4rd   r!   watchdog))r   r  )r   r0   )r  r0   )r   r0   )r   r   )r   r  )api_token_idapi_token_secret)r~   r!   )api_passwordr+  )argument_specmutually_exclusiverequired_togetherrequired_one_ofrequired_ifvalidate_certsr   )ignore_missingzSCan't get the next vmid for VM {0} automatically. Ensure your cluster state is goodr   z+VM with name = %s does not exist in clusterzvmid z with VM name z already exists)changedr!   r   )r   z,Settings has deleted on VM {0} with vmid {1}z3Unable to delete settings on VM {0} with vmid {1}: r   )r  z-Settings has reverted on VM {0} with vmid {1}zTUnable to revert settings on VM {0} with vmid {1}: Maybe is not a pending task...   z(VM {0} has been migrated from {1} to {2}zVM {0} is already on {1}z-Unable to migrate VM {0} from {1} to {2}: {3}z VM with vmid <%s> already existsz VM with name <%s> already existsz*node is mandatory for creating/updating VMz)vmid or name is mandatory for updating VMz#node '%s' does not exist in clustersmbios1)rg   rj   re   rf   rd   z&Unable to update vm {0} with vmid {1}=z%Unable to clone vm {0} from vmid {1}=zcreation of qemu VM z with vmid z failed with exception=zVM z updatedz with newid z cloned from vm with vmid z	 deployed)r5  r   r  r   r   zVM %s is already runningzVM %s startedzstarting of VM r   rF   zVM %s is already stopped)rO   rD   zVM %s is shutting downzstopping of VM r   zVM %s is already a templatezVM %s is converting to templater   r   r  zVM %s is not running)rO   zVM %s is restartedr  z#VM with name = %s is already absent)r5  r!   )rD   z<VM %s is running. Stop it before deletion or use force=true.zSReached timeout while waiting for removing VM. Last line in task before timeout: %szVM %s removedzdeletion of VM r   z	(unnamed)z with vmid = z is )r  r  r  )rO   rD   r   zVM %s is suspendingzsuspending of VM r?   )%r   r/   r0   r
   rH   r   r   get_vmidget_nextvmidr(   r*   rX   get_vm	exit_jsonrB   r   r   r   get_noder   r=   r"   r#   r$   r%   r   r   r'   r   r   r   r   r   rM   r   r   r   )module_argskvm_argsr)   r   r   r   r   r   r   r~   r   r5   r  r   rE   r0   r   r!   r3  proxmoxexisting_vmidclone_targetr8   r7   vm_noder   r   rA   rL   s                                r   mainrB    s   ,.K c% cvc c u	c
 c F#c % c 9f-.c uc 5!c 5!c U40c N(CDc F#c c  !c" #c$ e%c& 5!'c( 5!)c* +c, e$-c. /c0 6""&E"2!%5!1"&ED$<"H,0f,=	1c> ?c@ (noAcB  CcD vt,EcF U#GcH &!IcJ % KcL 34McN fOcP 6"QcR 5!ScT fUcV F#WcX GHYcZ % [c\ ]c^ &%0_c` 5)acb &ccd uecf fu5gch ficj kcl Vmcn vocp v&qcr  sct   I  Jucv 6"wcx uycz V${c|  }c~ c@ uAcB vCcD vEcF noGcH  IcJ 7KcL McN 6"OcP QcR 5!ScT % UcV %.WcX E"YcZ [c\ 9  /V  W]c^ % _c`  acb v.ccd ecf fgch 6"icj %,kcl F##'UT#B#'UUENTY#Z mcv fwcx /ycz 6{c| }c~   Ac@  AcB uCcD EcHH x ! ]?@)+KLy6(34F MM'"E
--
CMM'"E]]8$FmmI&G]]8$F== DMM'"E== D]]8$FmmI&GMM'"E&--)*F78M== D]]#34N}}X-/"&h'G IfU6RXah#,,T$,GM$M"//1D !=DL##L#FDI,,.
 !NQV!VW 	t >>%>5Uglnr;st	T47T:h:o:optvz:{| 	`T47T:i:p:pquw{:|} 		|%BjGw""2t,  D>h>o>optv}  @D  ?E   F  T?Y?`?`aegk?l m 	%W^^D^%NU;]`d;deETw/?/?UY/?/ZU1A1A$1GMorvMvw!MNCt-!LM!!$'!F!MNR	BGdE4vsE7TZ\i BB&,mmI&>BB#)==#8BB %+MM'$:BB %+MM'$:	BB
 )/k(BBB '-mmI&>BB $*==#8BB $*==#8BB (.}}Z'@BB (.}}Z'@BB *0|)DBB &,]]8%<BB )/k(BBB &,]]8%<BB (.}}Z'@BB  (.}}Z'@!BB" +1--*F#BB$ &,]]8%<%BB& (.}}Z'@'BB( %+MM'$:)BB* &,]]8%<+BB, *0|)D-BB. '-mmI&>/BB0 '-mmI&>1BB2 )/k(B3BB4 #)--"65BB6 (.}}Z'@7BB8 (.}}Z'@9BB: #)--"6;BB< )/k(B=BB> $*==#8?BB@ '-mmI&>ABBB 06}}=O/PCBBD -3MM/,JEBBF #)--"6GBBH $*==#8IBBJ ,2==+HKBBL &,]]8%<MBBN &,]]8%<OBBP (.}}Z'@QBBR $*==#8SBBT *0|)DUBBV &,]]8%<WBBX $*==#8YBBZ $*==#8[BB\ $*==#8]BB^ &,]]8%<_BB` &,]]8%<aBBb &,]]8%<cBBd (.}}Z'@eBBf '-mmH&=gBBh (.}}Z'@iBBj '-mmI&>kBBl )/k(BmBBn '-mmI&>oBBp &,]]8%<qBBr $*==#8sBBt &,]]8%<uBBv #)--"6wBBx (.}}Z'@yBBz )/k(B{BB| #)--"6}BB~ %+MM'$:BB@ #)--"6ABBB &,]]8%<CBBD (.}}Z'@EBBH ""4'-}}U';'-}}U';(.f(=(.f(=*0--*A # C TZ^`d:efTptv{  ~B  <C  DFgTPTVZ/[g_fg	)	!NQU!UV
	q%B))//6
;@@FMMUUYY[\deG&F8)#   kT?Y\`?`kdjk#   _DoPT>T_X^_ $
 
)	!NQU!UV
	q%B))//6
;@@FMMUUYY[\deG&F8)#   kT?Y\`?`kdjkOOBfmmG&<fmmT]F^O_FdT:RUY:Yd]cd 
*	!NQU!UV		%B*~"   nT?\_c?cngmn**2V]]75KU[UbUbclUm*n   qD>_bf>fqjpq o
 
+	!NQU!UV^^D!%%++BvJ7<<TBIIQQUUWX`a"xiFcU;QTX;Xc\bcbg(>?F`T:NQU:U`Y_` @ 
(	U0UX\0\]	\T:B  T :"..44RZ@L"''-44<<@@B8LG&F8)#==)OOBfmmI6NOO$$U  DB  EI  DI$  J!&&--d3F((FV<  %z!-!3!3F!;!?!?!C!C!Ebq!I&J  K   DoPT>T UK 
)	!NQU!UV^^D!66&+.D%%++BvJ7<<TBIIQQUUWX`a"xFzU[_aegn;ozsyz  
*	*!NQU!UV
	s%B))//6
;@@FMMUUYY[\deG&F8)#   gT?UX\?\g`fgrw)?W`Iakp  uA  lA  CFaT:ORV:VaZ`a 
+C ! M$$)~  *F  *F  GK  *L$  MM  I  %z  &B  &B  CG  &H   II$  	$,a,h,himos,twz{|w},}~~	  	`$  -C  -J  -J  KO  QU  -V  Y\  ]^  Y_  -_  `  `	`  	|$,[,b,bcgiprvxy,z{{	|v  	B  d0X0_0_`dfj0knqrsnt0t u"  d0W0^0^_cei0jmpqrms0s t  drvx|~  1A   B	B:  	qFp$]acd,epiop	q"  	qFp$]acd,epiop	q   	F~$koqr,s~w}~	R  	\RVXY!Z[[	\:  	sFr$_cef,grkqr	ssO  AP/ 5AQ  8AR 8AS A7AT RAU 1B0AW0 CAX! /A9AY D*EAZ MCAZ1 P/*AQQAQQ *ARRARR	ASR/ASSASS	ATS /ATTATT	AUT&%AUUAUU	AW-U"B AW(W(AW-W0	AXW9AXXAXX!	AYX*AY
Y
AYY	AZ YAY;Y;AZ Z	AZ.ZAZ)Z)AZ.Z1	A[Z:A[[A[__main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNr   rJ   +ansible.module_utils.six.moves.urllib.parser   Bansible_collections.community.proxmox.plugins.module_utils.versionr   Bansible_collections.community.proxmox.plugins.module_utils.proxmoxr   r	   ansible.module_utils.basicr
   )ansible.module_utils.parsing.convert_boolr   r   r   r   rB  r   r?   r   r   <module>rN     sx    A @hT@D

$ 
  = [ { 4 =14] ]@	`sF zF r   