2023-01-31 19:09:29 +00:00
|
|
|
#!/usr/bin/python
|
2020-08-18 21:44:49 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2023-01-31 19:09:29 +00:00
|
|
|
|
2021-05-27 14:58:09 +00:00
|
|
|
# Copyright: (c) 2016, Abdoul Bah (@helldorado) <bahabdoul at gmail.com>
|
2020-08-18 21:44:49 +00:00
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
2023-01-31 19:09:29 +00:00
|
|
|
"""Control Proxmox KVM machines."""
|
2020-08-18 21:44:49 +00:00
|
|
|
|
2023-12-13 10:23:28 +00:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2020-08-18 21:44:49 +00:00
|
|
|
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2023-01-31 19:09:29 +00:00
|
|
|
DOCUMENTATION = r"""
|
2020-08-18 21:44:49 +00:00
|
|
|
---
|
|
|
|
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.
|
2021-05-27 14:58:09 +00:00
|
|
|
author: "Abdoul Bah (@helldorado) <bahabdoul at gmail.com>"
|
2023-01-31 19:09:29 +00:00
|
|
|
version_added: 1.1.0
|
2020-08-18 21:44:49 +00:00
|
|
|
options:
|
2023-01-31 19:09:29 +00:00
|
|
|
vmid:
|
|
|
|
description:
|
|
|
|
- Specifies the instance ID.
|
|
|
|
- If not set the next available ID will be fetched from ProxmoxAPI.
|
|
|
|
type: int
|
|
|
|
node:
|
|
|
|
description:
|
|
|
|
- Proxmox VE node on which to operate.
|
|
|
|
- Only required for I(state=present).
|
|
|
|
- For every other states it will be autodiscovered.
|
|
|
|
type: str
|
|
|
|
pool:
|
|
|
|
description:
|
|
|
|
- Add the new VM to the specified pool.
|
|
|
|
type: str
|
|
|
|
api_host:
|
|
|
|
description:
|
|
|
|
- Specify the target host of the Proxmox VE cluster.
|
2023-01-31 20:35:03 +00:00
|
|
|
- You can use C(PROXMOX_SERVER) environment variable.
|
2023-01-31 19:09:29 +00:00
|
|
|
type: str
|
|
|
|
required: true
|
|
|
|
api_user:
|
|
|
|
description:
|
|
|
|
- Specify the user to authenticate with.
|
2023-01-31 20:35:03 +00:00
|
|
|
- You can use C(PROXMOX_USER) environment variable.
|
2023-01-31 19:09:29 +00:00
|
|
|
type: str
|
|
|
|
required: true
|
|
|
|
api_password:
|
|
|
|
description:
|
|
|
|
- Specify the password to authenticate with.
|
|
|
|
- You can use C(PROXMOX_PASSWORD) environment variable.
|
|
|
|
type: str
|
|
|
|
api_token_id:
|
|
|
|
description:
|
|
|
|
- Specify the token ID.
|
2023-01-31 20:35:03 +00:00
|
|
|
- You can use C(PROXMOX_TOKEN_ID) environment variable.
|
2023-01-31 19:09:29 +00:00
|
|
|
type: str
|
|
|
|
version_added: 1.3.0
|
|
|
|
api_token_secret:
|
|
|
|
description:
|
|
|
|
- Specify the token secret.
|
2023-01-31 20:35:03 +00:00
|
|
|
- You can use C(PROXMOX_TOKEN_SECRET) environment variable.
|
2023-01-31 19:09:29 +00:00
|
|
|
type: str
|
|
|
|
version_added: 1.3.0
|
|
|
|
verify_ssl:
|
|
|
|
description:
|
|
|
|
- If C(false), SSL certificates will not be validated.
|
|
|
|
- This should only be used on personally controlled sites using self-signed certificates.
|
|
|
|
type: bool
|
|
|
|
default: True
|
2020-08-18 21:44:49 +00:00
|
|
|
acpi:
|
|
|
|
description:
|
|
|
|
- Specify if ACPI should be enabled/disabled.
|
2021-05-27 14:58:09 +00:00
|
|
|
- If I(proxmox_default_behavior) is set to C(compatiblity) (the default value), this
|
|
|
|
option has a default of C(yes).
|
2020-08-18 21:44:49 +00:00
|
|
|
type: bool
|
|
|
|
agent:
|
|
|
|
description:
|
|
|
|
- Specify if the QEMU Guest Agent should be enabled/disabled.
|
|
|
|
type: bool
|
|
|
|
args:
|
|
|
|
description:
|
|
|
|
- Pass arbitrary arguments to kvm.
|
|
|
|
- This option is for experts only!
|
2021-05-27 14:58:09 +00:00
|
|
|
- If I(proxmox_default_behavior) is set to C(compatiblity) (the default value), this
|
|
|
|
option has a default of C(-serial unix:/var/run/qemu-server/<vmid>.serial,server,nowait).
|
|
|
|
type: str
|
2020-08-18 21:44:49 +00:00
|
|
|
autostart:
|
|
|
|
description:
|
|
|
|
- Specify if the VM should be automatically restarted after crash (currently ignored in PVE API).
|
2021-05-27 14:58:09 +00:00
|
|
|
- If I(proxmox_default_behavior) is set to C(compatiblity) (the default value), this
|
|
|
|
option has a default of C(no).
|
2020-08-18 21:44:49 +00:00
|
|
|
type: bool
|
|
|
|
balloon:
|
|
|
|
description:
|
|
|
|
- Specify the amount of RAM for the VM in MB.
|
|
|
|
- Using zero disables the balloon driver.
|
2021-05-27 14:58:09 +00:00
|
|
|
- If I(proxmox_default_behavior) is set to C(compatiblity) (the default value), this
|
|
|
|
option has a default of C(0).
|
|
|
|
type: int
|
2020-08-18 21:44:49 +00:00
|
|
|
bios:
|
|
|
|
description:
|
|
|
|
- Specify the BIOS implementation.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: str
|
2023-01-31 19:09:29 +00:00
|
|
|
choices: ["seabios", "ovmf"]
|
2020-08-18 21:44:49 +00:00
|
|
|
boot:
|
|
|
|
description:
|
|
|
|
- Specify the boot order -> boot on floppy C(a), hard disk C(c), CD-ROM C(d), or network C(n).
|
|
|
|
- You can combine to set order.
|
2021-05-27 14:58:09 +00:00
|
|
|
- If I(proxmox_default_behavior) is set to C(compatiblity) (the default value), this
|
|
|
|
option has a default of C(cnd).
|
|
|
|
type: str
|
2020-08-18 21:44:49 +00:00
|
|
|
bootdisk:
|
|
|
|
description:
|
|
|
|
- Enable booting from specified disk. C((ide|sata|scsi|virtio)\d+)
|
2021-05-27 14:58:09 +00:00
|
|
|
type: str
|
|
|
|
cicustom:
|
2020-08-18 21:44:49 +00:00
|
|
|
description:
|
2023-01-31 19:09:29 +00:00
|
|
|
- "cloud-init: Specify custom files to replace the automatically generated ones at start."
|
2021-05-27 14:58:09 +00:00
|
|
|
type: str
|
2020-08-18 21:44:49 +00:00
|
|
|
cipassword:
|
|
|
|
description:
|
2023-01-31 19:09:29 +00:00
|
|
|
- "cloud-init: password of default user to create."
|
2021-05-27 14:58:09 +00:00
|
|
|
type: str
|
2020-08-18 21:44:49 +00:00
|
|
|
citype:
|
|
|
|
description:
|
2023-01-31 19:09:29 +00:00
|
|
|
- "cloud-init: Specifies the cloud-init configuration format."
|
2021-05-27 14:58:09 +00:00
|
|
|
- The default depends on the configured operating system type (C(ostype)).
|
|
|
|
- We use the C(nocloud) format for Linux, and C(configdrive2) for Windows.
|
|
|
|
type: str
|
2023-01-31 19:09:29 +00:00
|
|
|
choices: ["nocloud", "configdrive2"]
|
2021-05-27 14:58:09 +00:00
|
|
|
ciuser:
|
|
|
|
description:
|
2023-01-31 19:09:29 +00:00
|
|
|
- "cloud-init: username of default user to create."
|
2021-05-27 14:58:09 +00:00
|
|
|
type: str
|
2020-08-18 21:44:49 +00:00
|
|
|
clone:
|
|
|
|
description:
|
2021-05-27 14:58:09 +00:00
|
|
|
- Name of VM to be cloned. If C(vmid) is setted, C(clone) can take arbitrary value but required for initiating the clone.
|
|
|
|
type: str
|
2020-08-18 21:44:49 +00:00
|
|
|
cores:
|
|
|
|
description:
|
|
|
|
- Specify number of cores per socket.
|
2021-05-27 14:58:09 +00:00
|
|
|
- If I(proxmox_default_behavior) is set to C(compatiblity) (the default value), this
|
|
|
|
option has a default of C(1).
|
|
|
|
type: int
|
2020-08-18 21:44:49 +00:00
|
|
|
cpu:
|
|
|
|
description:
|
|
|
|
- Specify emulated CPU type.
|
2021-05-27 14:58:09 +00:00
|
|
|
- If I(proxmox_default_behavior) is set to C(compatiblity) (the default value), this
|
|
|
|
option has a default of C(kvm64).
|
|
|
|
type: str
|
2020-08-18 21:44:49 +00:00
|
|
|
cpulimit:
|
|
|
|
description:
|
|
|
|
- Specify if CPU usage will be limited. Value 0 indicates no CPU limit.
|
2021-05-27 14:58:09 +00:00
|
|
|
- If the computer has 2 CPUs, it has total of '2' CPU time
|
|
|
|
type: int
|
2020-08-18 21:44:49 +00:00
|
|
|
cpuunits:
|
|
|
|
description:
|
|
|
|
- Specify CPU weight for a VM.
|
|
|
|
- You can disable fair-scheduler configuration by setting this to 0
|
2021-05-27 14:58:09 +00:00
|
|
|
- If I(proxmox_default_behavior) is set to C(compatiblity) (the default value), this
|
|
|
|
option has a default of C(1000).
|
|
|
|
type: int
|
2020-08-18 21:44:49 +00:00
|
|
|
delete:
|
|
|
|
description:
|
|
|
|
- Specify a list of settings you want to delete.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: str
|
2020-08-18 21:44:49 +00:00
|
|
|
description:
|
|
|
|
description:
|
|
|
|
- Specify the description for the VM. Only used on the configuration web interface.
|
|
|
|
- This is saved as comment inside the configuration file.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: str
|
2020-08-18 21:44:49 +00:00
|
|
|
digest:
|
|
|
|
description:
|
|
|
|
- Specify if to prevent changes if current configuration file has different SHA1 digest.
|
|
|
|
- This can be used to prevent concurrent modifications.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: str
|
2020-08-18 21:44:49 +00:00
|
|
|
force:
|
|
|
|
description:
|
|
|
|
- Allow to force stop VM.
|
2021-05-27 14:58:09 +00:00
|
|
|
- Can be used with states C(stopped), C(restarted) and C(absent).
|
|
|
|
- If I(proxmox_default_behavior) is set to C(compatiblity) (the default value), this
|
|
|
|
option has a default of C(no).
|
2020-08-18 21:44:49 +00:00
|
|
|
type: bool
|
|
|
|
format:
|
|
|
|
description:
|
2021-05-27 14:58:09 +00:00
|
|
|
- Target drive's backing file's data format.
|
2020-08-18 21:44:49 +00:00
|
|
|
- Used only with clone
|
2021-05-27 14:58:09 +00:00
|
|
|
- Use I(format=unspecified) and I(full=false) for a linked clone.
|
|
|
|
- If I(proxmox_default_behavior) is set to C(compatiblity) (the default value), this
|
|
|
|
option has a default of C(qcow2). If I(proxmox_default_behavior) is set to C(no_defaults),
|
|
|
|
not specifying this option is equivalent to setting it to C(unspecified).
|
|
|
|
type: str
|
2023-01-31 19:09:29 +00:00
|
|
|
choices:
|
|
|
|
["cloop", "cow", "qcow", "qcow2", "qed", "raw", "vmdk", "unspecified"]
|
2020-08-18 21:44:49 +00:00
|
|
|
freeze:
|
|
|
|
description:
|
2021-05-27 14:58:09 +00:00
|
|
|
- Specify if PVE should freeze CPU at startup (use 'c' monitor command to start execution).
|
2020-08-18 21:44:49 +00:00
|
|
|
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
|
2023-01-31 19:09:29 +00:00
|
|
|
default: "yes"
|
2020-08-18 21:44:49 +00:00
|
|
|
hostpci:
|
|
|
|
description:
|
2021-05-27 14:58:09 +00:00
|
|
|
- Specify a hash/dictionary of map host pci devices into guest. C(hostpci='{"key":"value", "key":"value"}').
|
2020-08-18 21:44:49 +00:00
|
|
|
- Keys allowed are - C(hostpci[n]) where 0 ≤ n ≤ N.
|
|
|
|
- Values allowed are - C("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 C(bus:dev.func) (hexadecimal numbers).
|
|
|
|
- C(pcie=boolean) I(default=0) Choose the PCI-express bus (needs the q35 machine model).
|
2021-05-27 14:58:09 +00:00
|
|
|
- C(rombar=boolean) I(default=1) Specify whether or not the device's ROM will be visible in the guest's memory map.
|
2020-08-18 21:44:49 +00:00
|
|
|
- C(x-vga=boolean) I(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.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: dict
|
2020-08-18 21:44:49 +00:00
|
|
|
hotplug:
|
|
|
|
description:
|
|
|
|
- Selectively enable hotplug features.
|
2021-05-27 14:58:09 +00:00
|
|
|
- This is a comma separated list of hotplug features C('network', 'disk', 'cpu', 'memory' and 'usb').
|
|
|
|
- Value 0 disables hotplug completely and value 1 is an alias for the default C('network,disk,usb').
|
|
|
|
type: str
|
2020-08-18 21:44:49 +00:00
|
|
|
hugepages:
|
|
|
|
description:
|
|
|
|
- Enable/disable hugepages memory.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: str
|
2023-01-31 19:09:29 +00:00
|
|
|
choices: ["any", "2", "1024"]
|
2020-08-18 21:44:49 +00:00
|
|
|
ide:
|
|
|
|
description:
|
2021-05-27 14:58:09 +00:00
|
|
|
- A hash/dictionary of volume used as IDE hard disk or CD-ROM. C(ide='{"key":"value", "key":"value"}').
|
2020-08-18 21:44:49 +00:00
|
|
|
- Keys allowed are - C(ide[n]) where 0 ≤ n ≤ 3.
|
|
|
|
- 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.
|
2021-05-27 14:58:09 +00:00
|
|
|
- C(format) is the drive's backing file's data format. C(qcow2|raw|subvol).
|
|
|
|
type: dict
|
2020-08-18 21:44:49 +00:00
|
|
|
ipconfig:
|
2021-05-27 14:58:09 +00:00
|
|
|
description:
|
2023-01-31 19:09:29 +00:00
|
|
|
- "cloud-init: Set the IP configuration."
|
2021-05-27 14:58:09 +00:00
|
|
|
- A hash/dictionary of network ip configurations. C(ipconfig='{"key":"value", "key":"value"}').
|
|
|
|
- Keys allowed are - C(ipconfig[n]) where 0 ≤ n ≤ network interfaces.
|
|
|
|
- Values allowed are - C("[gw=<GatewayIPv4>] [,gw6=<GatewayIPv6>] [,ip=<IPv4Format/CIDR>] [,ip6=<IPv6Format/CIDR>]").
|
2023-01-31 19:09:29 +00:00
|
|
|
- "cloud-init: Specify IP addresses and gateways for the corresponding interface."
|
2021-05-27 14:58:09 +00:00
|
|
|
- IP addresses use CIDR notation, gateways are optional but they should be in the same subnet of specified IP address.
|
|
|
|
- The special string 'dhcp' can be used for IP addresses to use DHCP, in which case no explicit gateway should be provided.
|
|
|
|
- For IPv6 the special string '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
|
2020-08-18 21:44:49 +00:00
|
|
|
keyboard:
|
|
|
|
description:
|
|
|
|
- Sets the keyboard layout for VNC server.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: str
|
2020-08-18 21:44:49 +00:00
|
|
|
kvm:
|
|
|
|
description:
|
|
|
|
- Enable/disable KVM hardware virtualization.
|
2021-05-27 14:58:09 +00:00
|
|
|
- If I(proxmox_default_behavior) is set to C(compatiblity) (the default value), this
|
|
|
|
option has a default of C(yes).
|
2020-08-18 21:44:49 +00:00
|
|
|
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.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: str
|
2023-01-31 19:09:29 +00:00
|
|
|
choices: ["migrate", "backup", "snapshot", "rollback"]
|
2020-08-18 21:44:49 +00:00
|
|
|
machine:
|
|
|
|
description:
|
|
|
|
- Specifies the Qemu machine type.
|
|
|
|
- type => C((pc|pc(-i440fx)?-\d+\.\d+(\.pxe)?|q35|pc-q35-\d+\.\d+(\.pxe)?))
|
2021-05-27 14:58:09 +00:00
|
|
|
type: str
|
2020-08-18 21:44:49 +00:00
|
|
|
memory:
|
|
|
|
description:
|
|
|
|
- Memory size in MB for instance.
|
2021-05-27 14:58:09 +00:00
|
|
|
- If I(proxmox_default_behavior) is set to C(compatiblity) (the default value), this
|
|
|
|
option has a default of C(512).
|
|
|
|
type: int
|
2020-08-18 21:44:49 +00:00
|
|
|
migrate_downtime:
|
|
|
|
description:
|
|
|
|
- Sets maximum tolerated downtime (in seconds) for migrations.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: int
|
2020-08-18 21:44:49 +00:00
|
|
|
migrate_speed:
|
|
|
|
description:
|
|
|
|
- Sets maximum speed (in MB/s) for migrations.
|
|
|
|
- A value of 0 is no limit.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: int
|
2020-08-18 21:44:49 +00:00
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- Specifies the VM name. Only used on the configuration web interface.
|
|
|
|
- Required only for C(state=present).
|
2021-05-27 14:58:09 +00:00
|
|
|
type: str
|
|
|
|
nameservers:
|
|
|
|
description:
|
2023-01-31 19:09:29 +00:00
|
|
|
- "cloud-init: DNS server IP address(es)."
|
2021-05-27 14:58:09 +00:00
|
|
|
- If unset, PVE host settings are used.
|
|
|
|
type: list
|
|
|
|
elements: str
|
2020-08-18 21:44:49 +00:00
|
|
|
net:
|
|
|
|
description:
|
2021-05-27 14:58:09 +00:00
|
|
|
- A hash/dictionary of network interfaces for the VM. C(net='{"key":"value", "key":"value"}').
|
2020-08-18 21:44:49 +00:00
|
|
|
- Keys allowed are - C(net[n]) where 0 ≤ n ≤ N.
|
2021-05-27 14:58:09 +00:00
|
|
|
- Values allowed are - C("model="XX:XX:XX:XX:XX:XX",bridge="value",rate="value",tag="value",firewall="1|0",trunks="vlanid"").
|
2020-08-18 21:44:49 +00:00
|
|
|
- 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.
|
2021-05-27 14:58:09 +00:00
|
|
|
- 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
|
2020-08-18 21:44:49 +00:00
|
|
|
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.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: int
|
2020-08-18 21:44:49 +00:00
|
|
|
numa:
|
|
|
|
description:
|
2021-05-27 14:58:09 +00:00
|
|
|
- A hash/dictionaries of NUMA topology. C(numa='{"key":"value", "key":"value"}').
|
2020-08-18 21:44:49 +00:00
|
|
|
- Keys allowed are - C(numa[n]) where 0 ≤ n ≤ N.
|
|
|
|
- Values allowed are - C("cpu="<id[-id];...>",hostnodes="<id[-id];...>",memory="number",policy="(bind|interleave|preferred)"").
|
|
|
|
- C(cpus) CPUs accessing this NUMA node.
|
|
|
|
- C(hostnodes) Host NUMA nodes to use.
|
|
|
|
- C(memory) Amount of memory this NUMA node provides.
|
|
|
|
- C(policy) NUMA allocation policy.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: dict
|
|
|
|
numa_enabled:
|
|
|
|
description:
|
|
|
|
- Enables NUMA.
|
|
|
|
type: bool
|
2020-08-18 21:44:49 +00:00
|
|
|
onboot:
|
|
|
|
description:
|
|
|
|
- Specifies whether a VM will be started during system bootup.
|
2021-05-27 14:58:09 +00:00
|
|
|
- If I(proxmox_default_behavior) is set to C(compatiblity) (the default value), this
|
|
|
|
option has a default of C(yes).
|
2020-08-18 21:44:49 +00:00
|
|
|
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.
|
2021-05-27 14:58:09 +00:00
|
|
|
- If I(proxmox_default_behavior) is set to C(compatiblity) (the default value), this
|
|
|
|
option has a default of C(l26).
|
|
|
|
type: str
|
2023-01-31 19:09:29 +00:00
|
|
|
choices:
|
|
|
|
[
|
|
|
|
"other",
|
|
|
|
"wxp",
|
|
|
|
"w2k",
|
|
|
|
"w2k3",
|
|
|
|
"w2k8",
|
|
|
|
"wvista",
|
|
|
|
"win7",
|
|
|
|
"win8",
|
|
|
|
"win10",
|
|
|
|
"l24",
|
|
|
|
"l26",
|
|
|
|
"solaris",
|
|
|
|
]
|
2020-08-18 21:44:49 +00:00
|
|
|
parallel:
|
|
|
|
description:
|
2021-05-27 14:58:09 +00:00
|
|
|
- A hash/dictionary of map host parallel devices. C(parallel='{"key":"value", "key":"value"}').
|
2020-08-18 21:44:49 +00:00
|
|
|
- Keys allowed are - (parallel[n]) where 0 ≤ n ≤ 2.
|
|
|
|
- Values allowed are - C("/dev/parport\d+|/dev/usb/lp\d+").
|
2021-05-27 14:58:09 +00:00
|
|
|
type: dict
|
2020-08-18 21:44:49 +00:00
|
|
|
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 C(yes), the VM exit on reboot.
|
|
|
|
type: bool
|
|
|
|
revert:
|
|
|
|
description:
|
|
|
|
- Revert a pending change.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: str
|
2020-08-18 21:44:49 +00:00
|
|
|
sata:
|
|
|
|
description:
|
2021-05-27 14:58:09 +00:00
|
|
|
- A hash/dictionary of volume used as sata hard disk or CD-ROM. C(sata='{"key":"value", "key":"value"}').
|
2020-08-18 21:44:49 +00:00
|
|
|
- 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.
|
2021-05-27 14:58:09 +00:00
|
|
|
- C(format) is the drive's backing file's data format. C(qcow2|raw|subvol).
|
|
|
|
type: dict
|
2020-08-18 21:44:49 +00:00
|
|
|
scsi:
|
|
|
|
description:
|
2021-05-27 14:58:09 +00:00
|
|
|
- A hash/dictionary of volume used as SCSI hard disk or CD-ROM. C(scsi='{"key":"value", "key":"value"}').
|
2020-08-18 21:44:49 +00:00
|
|
|
- Keys allowed are - C(sata[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.
|
2021-05-27 14:58:09 +00:00
|
|
|
- C(format) is the drive's backing file's data format. C(qcow2|raw|subvol).
|
|
|
|
type: dict
|
2020-08-18 21:44:49 +00:00
|
|
|
scsihw:
|
|
|
|
description:
|
|
|
|
- Specifies the SCSI controller model.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: str
|
2023-01-31 19:09:29 +00:00
|
|
|
choices:
|
|
|
|
[
|
|
|
|
"lsi",
|
|
|
|
"lsi53c810",
|
|
|
|
"virtio-scsi-pci",
|
|
|
|
"virtio-scsi-single",
|
|
|
|
"megasas",
|
|
|
|
"pvscsi",
|
|
|
|
]
|
2021-05-27 14:58:09 +00:00
|
|
|
searchdomains:
|
|
|
|
description:
|
2023-01-31 19:09:29 +00:00
|
|
|
- "cloud-init: Sets DNS search domain(s)."
|
2021-05-27 14:58:09 +00:00
|
|
|
- If unset, PVE host settings are used.
|
|
|
|
type: list
|
|
|
|
elements: str
|
2020-08-18 21:44:49 +00:00
|
|
|
serial:
|
|
|
|
description:
|
2021-05-27 14:58:09 +00:00
|
|
|
- A hash/dictionary of serial device to create inside the VM. C('{"key":"value", "key":"value"}').
|
2020-08-18 21:44:49 +00:00
|
|
|
- Keys allowed are - serial[n](str; required) where 0 ≤ n ≤ 3.
|
|
|
|
- Values allowed are - C((/dev/.+|socket)).
|
|
|
|
- /!\ If you pass through a host serial device, it is no longer possible to migrate such machines - use with special care.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: dict
|
2020-08-18 21:44:49 +00:00
|
|
|
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.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: int
|
2020-08-18 21:44:49 +00:00
|
|
|
skiplock:
|
|
|
|
description:
|
|
|
|
- Ignore locks
|
|
|
|
- Only root is allowed to use this option.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: bool
|
2020-08-18 21:44:49 +00:00
|
|
|
smbios:
|
|
|
|
description:
|
|
|
|
- Specifies SMBIOS type 1 fields.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: str
|
2020-08-18 21:44:49 +00:00
|
|
|
snapname:
|
|
|
|
description:
|
|
|
|
- The name of the snapshot. Used only with clone.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: str
|
2020-08-18 21:44:49 +00:00
|
|
|
sockets:
|
|
|
|
description:
|
|
|
|
- Sets the number of CPU sockets. (1 - N).
|
2021-05-27 14:58:09 +00:00
|
|
|
- If I(proxmox_default_behavior) is set to C(compatiblity) (the default value), this
|
|
|
|
option has a default of C(1).
|
|
|
|
type: int
|
2020-08-18 21:44:49 +00:00
|
|
|
sshkeys:
|
|
|
|
description:
|
2023-01-31 19:09:29 +00:00
|
|
|
- "cloud-init: SSH key to assign to the default user. NOT TESTED with multiple keys but a multi-line value should work."
|
2021-05-27 14:58:09 +00:00
|
|
|
type: str
|
2020-08-18 21:44:49 +00:00
|
|
|
startdate:
|
|
|
|
description:
|
|
|
|
- Sets the initial date of the real time clock.
|
2021-05-27 14:58:09 +00:00
|
|
|
- Valid format for date are C('now') or C('2016-09-25T16:01:21') or C('2016-09-25').
|
|
|
|
type: str
|
2020-08-18 21:44:49 +00:00
|
|
|
startup:
|
|
|
|
description:
|
|
|
|
- Startup and shutdown behavior. C([[order=]\d+] [,up=\d+] [,down=\d+]).
|
|
|
|
- Order is a non-negative number defining the general startup order.
|
|
|
|
- Shutdown in done with reverse ordering.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: str
|
2020-08-18 21:44:49 +00:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Indicates desired state of the instance.
|
2021-05-27 14:58:09 +00:00
|
|
|
- If C(current), the current state of the VM will be fetched. You can access it with C(results.status)
|
|
|
|
type: str
|
2023-01-31 19:09:29 +00:00
|
|
|
choices: ["present", "started", "absent", "stopped", "restarted", "current"]
|
2020-08-18 21:44:49 +00:00
|
|
|
default: present
|
|
|
|
storage:
|
|
|
|
description:
|
|
|
|
- Target storage for full clone.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: str
|
2020-08-18 21:44:49 +00:00
|
|
|
tablet:
|
|
|
|
description:
|
|
|
|
- Enables/disables the USB tablet device.
|
2021-05-27 14:58:09 +00:00
|
|
|
- If I(proxmox_default_behavior) is set to C(compatiblity) (the default value), this
|
|
|
|
option has a default of C(no).
|
2020-08-18 21:44:49 +00:00
|
|
|
type: bool
|
2021-05-27 14:58:09 +00:00
|
|
|
tags:
|
|
|
|
description:
|
|
|
|
- List of tags to apply to the VM instance.
|
|
|
|
- Tags must start with C([a-z0-9_]) followed by zero or more of the following characters C([a-z0-9_-+.]).
|
|
|
|
- Tags are only available in Proxmox 6+.
|
|
|
|
type: list
|
|
|
|
elements: str
|
2020-08-18 21:44:49 +00:00
|
|
|
target:
|
|
|
|
description:
|
|
|
|
- Target node. Only allowed if the original VM is on shared storage.
|
|
|
|
- Used only with clone
|
2021-05-27 14:58:09 +00:00
|
|
|
type: str
|
2020-08-18 21:44:49 +00:00
|
|
|
tdf:
|
|
|
|
description:
|
|
|
|
- Enables/disables time drift fix.
|
|
|
|
type: bool
|
|
|
|
template:
|
|
|
|
description:
|
|
|
|
- Enables/disables the template.
|
2021-05-27 14:58:09 +00:00
|
|
|
- If I(proxmox_default_behavior) is set to C(compatiblity) (the default value), this
|
|
|
|
option has a default of C(no).
|
2020-08-18 21:44:49 +00:00
|
|
|
type: bool
|
|
|
|
timeout:
|
|
|
|
description:
|
|
|
|
- Timeout for operations.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: int
|
2020-08-18 21:44:49 +00:00
|
|
|
default: 30
|
|
|
|
update:
|
|
|
|
description:
|
2021-05-27 14:58:09 +00:00
|
|
|
- If C(yes), the VM will be updated with new value.
|
|
|
|
- Update of C(pool) is disabled. It needs an additional API endpoint not covered by this module.
|
2020-08-18 21:44:49 +00:00
|
|
|
type: bool
|
2023-01-31 19:09:29 +00:00
|
|
|
default: "no"
|
2020-08-18 21:44:49 +00:00
|
|
|
vcpus:
|
|
|
|
description:
|
|
|
|
- Sets number of hotplugged vcpus.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: int
|
2020-08-18 21:44:49 +00:00
|
|
|
vga:
|
|
|
|
description:
|
2021-05-27 14:58:09 +00:00
|
|
|
- Select VGA type. If you want to use high resolution modes (>= 1280x1024x16) then you should use option 'std' or 'vmware'.
|
|
|
|
- If I(proxmox_default_behavior) is set to C(compatiblity) (the default value), this
|
|
|
|
option has a default of C(std).
|
|
|
|
type: str
|
2023-01-31 19:09:29 +00:00
|
|
|
choices:
|
|
|
|
[
|
|
|
|
"std",
|
|
|
|
"cirrus",
|
|
|
|
"vmware",
|
|
|
|
"qxl",
|
|
|
|
"serial0",
|
|
|
|
"serial1",
|
|
|
|
"serial2",
|
|
|
|
"serial3",
|
|
|
|
"qxl2",
|
|
|
|
"qxl3",
|
|
|
|
"qxl4",
|
|
|
|
]
|
2020-08-18 21:44:49 +00:00
|
|
|
virtio:
|
|
|
|
description:
|
2021-05-27 14:58:09 +00:00
|
|
|
- A hash/dictionary of volume used as VIRTIO hard disk. C(virtio='{"key":"value", "key":"value"}').
|
2020-08-18 21:44:49 +00:00
|
|
|
- Keys allowed are - C(virto[n]) where 0 ≤ n ≤ 15.
|
|
|
|
- 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.
|
2021-05-27 14:58:09 +00:00
|
|
|
- C(format) is the drive's backing file's data format. C(qcow2|raw|subvol).
|
|
|
|
type: dict
|
2020-08-18 21:44:49 +00:00
|
|
|
watchdog:
|
|
|
|
description:
|
|
|
|
- Creates a virtual hardware watchdog device.
|
2021-05-27 14:58:09 +00:00
|
|
|
type: str
|
|
|
|
proxmox_default_behavior:
|
|
|
|
description:
|
|
|
|
- Various module options used to have default values. This cause problems when
|
|
|
|
user expects different behavior from proxmox by default or fill options which cause
|
|
|
|
problems when they have been set.
|
|
|
|
- The default value is C(compatibility), which will ensure that the default values
|
|
|
|
are used when the values are not explicitly specified by the user.
|
|
|
|
type: str
|
|
|
|
choices:
|
|
|
|
- compatibility
|
|
|
|
- no_defaults
|
2020-08-18 21:44:49 +00:00
|
|
|
""" # noqa
|
|
|
|
|
|
|
|
EXAMPLES = """
|
2021-05-27 14:58:09 +00:00
|
|
|
- name: Create new VM with minimal options
|
|
|
|
xoxys.general.proxmox_kvm:
|
|
|
|
api_user: root@pam
|
2020-08-18 21:44:49 +00:00
|
|
|
api_password: secret
|
2021-05-27 14:58:09 +00:00
|
|
|
api_host: helldorado
|
|
|
|
name: spynal
|
|
|
|
node: sabrewulf
|
2020-08-18 21:44:49 +00:00
|
|
|
|
2021-05-27 14:58:09 +00:00
|
|
|
- name: Create new VM with minimal options and given vmid
|
|
|
|
xoxys.general.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
|
|
|
|
xoxys.general.proxmox_kvm:
|
|
|
|
api_user: root@pam
|
2020-08-18 21:44:49 +00:00
|
|
|
api_password: secret
|
2021-05-27 14:58:09 +00:00
|
|
|
api_host: helldorado
|
|
|
|
name: spynal
|
|
|
|
node: sabrewulf
|
|
|
|
net:
|
2023-01-31 19:09:29 +00:00
|
|
|
net0: "virtio,bridge=vmbr1,rate=200"
|
|
|
|
net1: "e1000,bridge=vmbr2"
|
2021-05-27 14:58:09 +00:00
|
|
|
|
|
|
|
- name: Create new VM with one network interface, three virto hard disk, 4 cores, and 2 vcpus
|
|
|
|
xoxys.general.proxmox_kvm:
|
|
|
|
api_user: root@pam
|
2020-08-18 21:44:49 +00:00
|
|
|
api_password: secret
|
2021-05-27 14:58:09 +00:00
|
|
|
api_host: helldorado
|
|
|
|
name: spynal
|
|
|
|
node: sabrewulf
|
|
|
|
net:
|
2023-01-31 19:09:29 +00:00
|
|
|
net0: "virtio,bridge=vmbr1,rate=200"
|
2021-05-27 14:58:09 +00:00
|
|
|
virtio:
|
2023-01-31 19:09:29 +00:00
|
|
|
virtio0: "VMs_LVM:10"
|
|
|
|
virtio1: "VMs:2,format=qcow2"
|
|
|
|
virtio2: "VMs:5,format=raw"
|
2021-05-27 14:58:09 +00:00
|
|
|
cores: 4
|
|
|
|
vcpus: 2
|
|
|
|
|
|
|
|
- name: >
|
|
|
|
Clone VM with only source VM name.
|
|
|
|
The VM source is spynal.
|
|
|
|
The target VM name is zavala
|
|
|
|
xoxys.general.proxmox_kvm:
|
|
|
|
api_user: root@pam
|
2020-08-18 21:44:49 +00:00
|
|
|
api_password: secret
|
2021-05-27 14:58:09 +00:00
|
|
|
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
|
|
|
|
xoxys.general.proxmox_kvm:
|
|
|
|
api_user: root@pam
|
2020-08-18 21:44:49 +00:00
|
|
|
api_password: secret
|
2021-05-27 14:58:09 +00:00
|
|
|
api_host: helldorado
|
|
|
|
clone: spynal
|
|
|
|
name: zavala
|
|
|
|
node: sabrewulf
|
|
|
|
storage: VMs
|
|
|
|
full: no
|
|
|
|
format: unspecified
|
|
|
|
timeout: 500
|
|
|
|
|
|
|
|
- name: Clone VM with source vmid and target newid and raw format
|
|
|
|
xoxys.general.proxmox_kvm:
|
|
|
|
api_user: root@pam
|
2020-08-18 21:44:49 +00:00
|
|
|
api_password: secret
|
2021-05-27 14:58:09 +00:00
|
|
|
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
|
|
|
|
xoxys.general.proxmox_kvm:
|
|
|
|
api_user: root@pam
|
2020-08-18 21:44:49 +00:00
|
|
|
api_password: secret
|
2021-05-27 14:58:09 +00:00
|
|
|
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
|
|
|
|
xoxys.general.proxmox_kvm:
|
|
|
|
api_user: root@pam
|
2020-08-18 21:44:49 +00:00
|
|
|
api_password: secret
|
2021-05-27 14:58:09 +00:00
|
|
|
api_host: helldorado
|
|
|
|
name: spynal
|
|
|
|
node: sabrewulf
|
|
|
|
protection: yes
|
|
|
|
|
|
|
|
- name: Create new VM using cloud-init with a username and password
|
|
|
|
xoxys.general.proxmox_kvm:
|
|
|
|
node: sabrewulf
|
|
|
|
api_user: root@pam
|
2020-08-18 21:44:49 +00:00
|
|
|
api_password: secret
|
2021-05-27 14:58:09 +00:00
|
|
|
api_host: helldorado
|
|
|
|
name: spynal
|
|
|
|
ide:
|
2023-01-31 19:09:29 +00:00
|
|
|
ide2: "local:cloudinit,format=qcow2"
|
2021-05-27 14:58:09 +00:00
|
|
|
ciuser: mylinuxuser
|
|
|
|
cipassword: supersecret
|
2023-01-31 19:09:29 +00:00
|
|
|
searchdomains: "mydomain.internal"
|
2021-05-27 14:58:09 +00:00
|
|
|
nameservers: 1.1.1.1
|
|
|
|
net:
|
2023-01-31 19:09:29 +00:00
|
|
|
net0: "virtio,bridge=vmbr1,tag=77"
|
2021-05-27 14:58:09 +00:00
|
|
|
ipconfig:
|
2023-01-31 19:09:29 +00:00
|
|
|
ipconfig0: "ip=192.168.1.1/24,gw=192.168.1.1"
|
2021-05-27 14:58:09 +00:00
|
|
|
|
|
|
|
- name: Create new VM using Cloud-Init with an ssh key
|
|
|
|
xoxys.general.proxmox_kvm:
|
|
|
|
node: sabrewulf
|
|
|
|
api_user: root@pam
|
2020-08-18 21:44:49 +00:00
|
|
|
api_password: secret
|
2021-05-27 14:58:09 +00:00
|
|
|
api_host: helldorado
|
|
|
|
name: spynal
|
|
|
|
ide:
|
2023-01-31 19:09:29 +00:00
|
|
|
ide2: "local:cloudinit,format=qcow2"
|
|
|
|
sshkeys: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILJkVm98B71lD5XHfihwcYHE9TVpsJmK1vR1JcaU82L+"
|
|
|
|
searchdomains: "mydomain.internal"
|
2021-05-27 14:58:09 +00:00
|
|
|
nameservers:
|
2023-01-31 19:09:29 +00:00
|
|
|
- "1.1.1.1"
|
|
|
|
- "8.8.8.8"
|
2021-05-27 14:58:09 +00:00
|
|
|
net:
|
2023-01-31 19:09:29 +00:00
|
|
|
net0: "virtio,bridge=vmbr1,tag=77"
|
2021-05-27 14:58:09 +00:00
|
|
|
ipconfig:
|
2023-01-31 19:09:29 +00:00
|
|
|
ipconfig0: "ip=192.168.1.1/24"
|
2021-05-27 14:58:09 +00:00
|
|
|
|
|
|
|
- name: Start VM
|
|
|
|
xoxys.general.proxmox_kvm:
|
|
|
|
api_user: root@pam
|
2020-08-18 21:44:49 +00:00
|
|
|
api_password: secret
|
2021-05-27 14:58:09 +00:00
|
|
|
api_host: helldorado
|
|
|
|
name: spynal
|
|
|
|
node: sabrewulf
|
|
|
|
state: started
|
|
|
|
|
|
|
|
- name: Stop VM
|
|
|
|
xoxys.general.proxmox_kvm:
|
|
|
|
api_user: root@pam
|
2020-08-18 21:44:49 +00:00
|
|
|
api_password: secret
|
2021-05-27 14:58:09 +00:00
|
|
|
api_host: helldorado
|
|
|
|
name: spynal
|
|
|
|
node: sabrewulf
|
|
|
|
state: stopped
|
|
|
|
|
|
|
|
- name: Stop VM with force
|
|
|
|
xoxys.general.proxmox_kvm:
|
|
|
|
api_user: root@pam
|
2020-08-18 21:44:49 +00:00
|
|
|
api_password: secret
|
2021-05-27 14:58:09 +00:00
|
|
|
api_host: helldorado
|
|
|
|
name: spynal
|
|
|
|
node: sabrewulf
|
|
|
|
state: stopped
|
|
|
|
force: yes
|
|
|
|
|
|
|
|
- name: Restart VM
|
|
|
|
xoxys.general.proxmox_kvm:
|
|
|
|
api_user: root@pam
|
2020-08-18 21:44:49 +00:00
|
|
|
api_password: secret
|
2021-05-27 14:58:09 +00:00
|
|
|
api_host: helldorado
|
|
|
|
name: spynal
|
|
|
|
node: sabrewulf
|
|
|
|
state: restarted
|
|
|
|
|
|
|
|
- name: Remove VM
|
|
|
|
xoxys.general.proxmox_kvm:
|
|
|
|
api_user: root@pam
|
2020-08-18 21:44:49 +00:00
|
|
|
api_password: secret
|
2021-05-27 14:58:09 +00:00
|
|
|
api_host: helldorado
|
|
|
|
name: spynal
|
|
|
|
node: sabrewulf
|
|
|
|
state: absent
|
|
|
|
|
|
|
|
- name: Get VM current state
|
|
|
|
xoxys.general.proxmox_kvm:
|
|
|
|
api_user: root@pam
|
2020-08-18 21:44:49 +00:00
|
|
|
api_password: secret
|
2021-05-27 14:58:09 +00:00
|
|
|
api_host: helldorado
|
|
|
|
name: spynal
|
|
|
|
node: sabrewulf
|
|
|
|
state: current
|
|
|
|
|
|
|
|
- name: Update VM configuration
|
|
|
|
xoxys.general.proxmox_kvm:
|
|
|
|
api_user: root@pam
|
2020-08-18 21:44:49 +00:00
|
|
|
api_password: secret
|
2021-05-27 14:58:09 +00:00
|
|
|
api_host: helldorado
|
|
|
|
name: spynal
|
|
|
|
node: sabrewulf
|
|
|
|
cores: 8
|
|
|
|
memory: 16384
|
|
|
|
update: yes
|
|
|
|
|
|
|
|
- name: Delete QEMU parameters
|
|
|
|
xoxys.general.proxmox_kvm:
|
|
|
|
api_user: root@pam
|
|
|
|
api_password: secret
|
|
|
|
api_host: helldorado
|
|
|
|
name: spynal
|
|
|
|
node: sabrewulf
|
2023-01-31 19:09:29 +00:00
|
|
|
delete: "args,template,cpulimit"
|
2021-05-27 14:58:09 +00:00
|
|
|
|
|
|
|
- name: Revert a pending change
|
|
|
|
xoxys.general.proxmox_kvm:
|
|
|
|
api_user: root@pam
|
|
|
|
api_password: secret
|
|
|
|
api_host: helldorado
|
|
|
|
name: spynal
|
|
|
|
node: sabrewulf
|
2023-01-31 19:09:29 +00:00
|
|
|
revert: "template,cpulimit"
|
|
|
|
|
|
|
|
"""
|
2020-08-18 21:44:49 +00:00
|
|
|
|
|
|
|
RETURN = """
|
|
|
|
vmid:
|
2021-05-27 14:58:09 +00:00
|
|
|
description: The VM vmid.
|
|
|
|
returned: success
|
|
|
|
type: int
|
|
|
|
sample: 115
|
2020-08-18 21:44:49 +00:00
|
|
|
status:
|
2021-05-27 14:58:09 +00:00
|
|
|
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"
|
|
|
|
"""
|
|
|
|
|
|
|
|
import re
|
2021-12-05 20:47:23 +00:00
|
|
|
import string
|
2021-05-27 14:58:09 +00:00
|
|
|
import time
|
|
|
|
import traceback
|
2023-01-31 19:09:29 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
|
2021-05-27 14:58:09 +00:00
|
|
|
from ansible.module_utils.six.moves.urllib.parse import quote
|
2023-07-30 10:43:36 +00:00
|
|
|
from ansible_collections.xoxys.general.plugins.module_utils.version import LooseVersion
|
2020-08-18 21:44:49 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
from proxmoxer import ProxmoxAPI
|
2023-12-13 10:23:28 +00:00
|
|
|
|
2020-08-18 21:44:49 +00:00
|
|
|
HAS_PROXMOXER = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_PROXMOXER = False
|
|
|
|
|
2023-01-31 19:09:29 +00:00
|
|
|
try:
|
|
|
|
from requests.packages import urllib3
|
2023-12-13 10:23:28 +00:00
|
|
|
|
2023-01-31 19:09:29 +00:00
|
|
|
HAS_URLLIB3 = True
|
|
|
|
except ImportError:
|
|
|
|
try:
|
|
|
|
import urllib3
|
2023-12-13 10:23:28 +00:00
|
|
|
|
2023-01-31 19:09:29 +00:00
|
|
|
HAS_URLLIB3 = True
|
|
|
|
except ImportError:
|
|
|
|
HAS_URLLIB3 = False
|
|
|
|
|
2021-05-27 14:58:09 +00:00
|
|
|
from ansible.module_utils._text import to_native
|
2023-01-31 19:09:29 +00:00
|
|
|
from ansible.module_utils.basic import AnsibleModule, env_fallback
|
2020-08-18 21:44:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_nextvmid(module, proxmox):
|
|
|
|
try:
|
2023-12-13 10:23:28 +00:00
|
|
|
return proxmox.cluster.nextid.get()
|
2023-01-31 19:09:29 +00:00
|
|
|
except Exception as e: # noqa
|
2020-08-18 21:44:49 +00:00
|
|
|
module.fail_json(
|
2023-01-31 19:09:29 +00:00
|
|
|
msg=f"Unable to get next vmid. Failed with exception: {to_native(e)}",
|
2023-12-13 10:23:28 +00:00
|
|
|
exception=traceback.format_exc(),
|
2020-08-18 21:44:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def get_vmid(proxmox, name):
|
2021-05-27 14:58:09 +00:00
|
|
|
return [
|
|
|
|
vm["vmid"] for vm in proxmox.cluster.resources.get(type="vm") if vm.get("name") == name
|
|
|
|
]
|
2020-08-18 21:44:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_vm(proxmox, vmid):
|
|
|
|
return [vm for vm in proxmox.cluster.resources.get(type="vm") if vm["vmid"] == int(vmid)]
|
|
|
|
|
|
|
|
|
|
|
|
def node_check(proxmox, node):
|
|
|
|
return [True for nd in proxmox.nodes.get() if nd["node"] == node]
|
|
|
|
|
|
|
|
|
|
|
|
def get_vminfo(module, proxmox, node, vmid, **kwargs):
|
2023-01-31 19:09:29 +00:00
|
|
|
global results
|
2020-08-18 21:44:49 +00:00
|
|
|
results = {}
|
|
|
|
try:
|
|
|
|
vm = proxmox.nodes(node).qemu(vmid).config.get()
|
2023-01-31 19:09:29 +00:00
|
|
|
except Exception as e: # noqa
|
2020-08-18 21:44:49 +00:00
|
|
|
module.fail_json(
|
2023-01-31 19:09:29 +00:00
|
|
|
msg=f"Getting information for VM with vmid = {vmid} failed with exception: {e}"
|
2020-08-18 21:44:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Sanitize kwargs. Remove not defined args and ensure True and False converted to int.
|
|
|
|
kwargs = dict((k, v) for k, v in kwargs.items() if v is not None)
|
|
|
|
|
|
|
|
# Convert all dict in kwargs to elements.
|
2021-05-27 14:58:09 +00:00
|
|
|
for k in list(kwargs.keys()):
|
2020-08-18 21:44:49 +00:00
|
|
|
if isinstance(kwargs[k], dict):
|
|
|
|
kwargs.update(kwargs[k])
|
|
|
|
del kwargs[k]
|
|
|
|
|
2021-12-06 20:12:30 +00:00
|
|
|
results["nets"] = _extract_nets(vm)
|
|
|
|
results["disks"] = _extract_disks(vm)
|
2020-08-18 21:44:49 +00:00
|
|
|
results["vmid"] = int(vmid)
|
2021-12-05 20:47:23 +00:00
|
|
|
results["_raw"] = vm
|
2020-08-18 21:44:49 +00:00
|
|
|
|
|
|
|
|
2023-01-31 19:09:29 +00:00
|
|
|
def settings(module, proxmox, vmid, node, name, **kwargs): # noqa
|
2020-08-18 21:44:49 +00:00
|
|
|
proxmox_node = proxmox.nodes(node)
|
|
|
|
|
|
|
|
# Sanitize kwargs. Remove not defined args and ensure True and False converted to int.
|
|
|
|
kwargs = dict((k, v) for k, v in kwargs.items() if v is not None)
|
|
|
|
|
2023-12-13 10:23:28 +00:00
|
|
|
return proxmox_node.qemu(vmid).config.set(**kwargs) is None
|
2020-08-18 21:44:49 +00:00
|
|
|
|
|
|
|
|
2021-05-27 14:58:09 +00:00
|
|
|
def wait_for_task(module, proxmox, node, taskid):
|
|
|
|
timeout = module.params["timeout"]
|
|
|
|
|
|
|
|
while timeout:
|
|
|
|
task = proxmox.nodes(node).tasks(taskid).status.get()
|
|
|
|
if task["status"] == "stopped" and task["exitstatus"] == "OK":
|
|
|
|
# Wait an extra second as the API can be a ahead of the hypervisor
|
|
|
|
time.sleep(1)
|
|
|
|
return True
|
|
|
|
timeout = timeout - 1
|
|
|
|
if timeout == 0:
|
|
|
|
break
|
|
|
|
time.sleep(1)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2020-08-18 21:44:49 +00:00
|
|
|
def create_vm(
|
2021-05-27 14:58:09 +00:00
|
|
|
module, proxmox, vmid, newid, node, name, memory, cpu, cores, sockets, update, **kwargs
|
2020-08-18 21:44:49 +00:00
|
|
|
):
|
|
|
|
# Available only in PVE 4
|
|
|
|
only_v4 = ["force", "protection", "skiplock"]
|
2021-05-27 14:58:09 +00:00
|
|
|
only_v6 = ["ciuser", "cipassword", "sshkeys", "ipconfig", "tags"]
|
2020-08-18 21:44:49 +00:00
|
|
|
|
|
|
|
# valide clone parameters
|
|
|
|
valid_clone_params = ["format", "full", "pool", "snapname", "storage", "target"]
|
|
|
|
clone_params = {}
|
2021-05-27 14:58:09 +00:00
|
|
|
# Default args for vm. Note: -args option is for experts only. It allows you
|
|
|
|
# to pass arbitrary arguments to kvm.
|
2023-01-31 19:09:29 +00:00
|
|
|
vm_args = f"-serial unix:/var/run/qemu-server/{vmid}.serial,server,nowait"
|
2020-08-18 21:44:49 +00:00
|
|
|
|
|
|
|
proxmox_node = proxmox.nodes(node)
|
|
|
|
|
|
|
|
# Sanitize kwargs. Remove not defined args and ensure True and False converted to int.
|
|
|
|
kwargs = dict((k, v) for k, v in kwargs.items() if v is not None)
|
|
|
|
kwargs.update(dict([k, int(v)] for k, v in kwargs.items() if isinstance(v, bool)))
|
|
|
|
|
2021-05-27 14:58:09 +00:00
|
|
|
# The features work only on PVE 4+
|
2023-01-31 19:09:29 +00:00
|
|
|
if PVE_MAJOR_VERSION < 4:
|
2020-08-18 21:44:49 +00:00
|
|
|
for p in only_v4:
|
|
|
|
if p in kwargs:
|
|
|
|
del kwargs[p]
|
|
|
|
|
2021-05-27 14:58:09 +00:00
|
|
|
# The features work only on PVE 6
|
2023-01-31 19:09:29 +00:00
|
|
|
if PVE_MAJOR_VERSION < 6:
|
2021-05-27 14:58:09 +00:00
|
|
|
for p in only_v6:
|
|
|
|
if p in kwargs:
|
|
|
|
del kwargs[p]
|
|
|
|
|
|
|
|
# "sshkeys" param expects an urlencoded string
|
|
|
|
if "sshkeys" in kwargs:
|
|
|
|
urlencoded_ssh_keys = quote(kwargs["sshkeys"], safe="")
|
|
|
|
kwargs["sshkeys"] = str(urlencoded_ssh_keys)
|
|
|
|
|
2021-12-06 20:12:30 +00:00
|
|
|
disks = {}
|
2021-12-05 20:47:23 +00:00
|
|
|
for item in [kwargs[i] for i in ["scsi", "virtio", "ide", "sata"] if i in kwargs]:
|
2021-12-06 20:12:30 +00:00
|
|
|
disks = _extract_disks(item)
|
|
|
|
|
|
|
|
nets = {}
|
|
|
|
for item in [kwargs[i] for i in ["net"] if i in kwargs]:
|
|
|
|
nets = _extract_nets(item)
|
2021-12-05 20:47:23 +00:00
|
|
|
|
|
|
|
# If update, ensure existing disks are not recreated.
|
2020-08-18 21:44:49 +00:00
|
|
|
if update:
|
2021-12-06 20:12:30 +00:00
|
|
|
for k, v in disks.items():
|
|
|
|
if results["disks"].get(k):
|
2023-01-31 19:09:29 +00:00
|
|
|
storage_id = results["disks"][k]["storage_id"]
|
|
|
|
storage_opts = results["disks"][k]["storage_opts"]
|
|
|
|
opts = ",".join(v["opts"])
|
|
|
|
kwargs[k.rstrip(string.digits)][k] = f"{storage_id}:{storage_opts},{opts}"
|
2021-12-06 20:12:30 +00:00
|
|
|
|
|
|
|
for k, v in nets.items():
|
|
|
|
if results["nets"].get(k):
|
2023-01-31 19:09:29 +00:00
|
|
|
net_id = results["nets"][k]["net_id"]
|
|
|
|
net_opts = results["nets"][k]["net_opts"]
|
|
|
|
opts = ",".join(v["opts"])
|
|
|
|
kwargs[k.rstrip(string.digits)][k] = f"{net_id}={net_opts},{opts}"
|
2020-08-18 21:44:49 +00:00
|
|
|
|
|
|
|
# Convert all dict in kwargs to elements.
|
2021-05-27 14:58:09 +00:00
|
|
|
for k in list(kwargs.keys()):
|
2020-08-18 21:44:49 +00:00
|
|
|
if isinstance(kwargs[k], dict):
|
|
|
|
kwargs.update(kwargs[k])
|
|
|
|
del kwargs[k]
|
|
|
|
|
2021-05-27 14:58:09 +00:00
|
|
|
# Rename numa_enabled to numa. According the API documentation
|
2020-08-18 21:44:49 +00:00
|
|
|
if "numa_enabled" in kwargs:
|
|
|
|
kwargs["numa"] = kwargs["numa_enabled"]
|
|
|
|
del kwargs["numa_enabled"]
|
|
|
|
|
2021-05-27 14:58:09 +00:00
|
|
|
# PVE api expects strings for the following params
|
|
|
|
if "nameservers" in module.params:
|
|
|
|
nameservers = module.params.pop("nameservers")
|
|
|
|
if nameservers:
|
|
|
|
kwargs["nameserver"] = " ".join(nameservers)
|
|
|
|
if "searchdomains" in module.params:
|
|
|
|
searchdomains = module.params.pop("searchdomains")
|
|
|
|
if searchdomains:
|
|
|
|
kwargs["searchdomain"] = " ".join(searchdomains)
|
|
|
|
|
|
|
|
# VM tags are expected to be valid and presented as a comma/semi-colon delimited string
|
|
|
|
if "tags" in kwargs:
|
|
|
|
for tag in kwargs["tags"]:
|
|
|
|
if not re.match(r"^[a-z0-9_][a-z0-9_\-\+\.]*$", tag):
|
2023-01-31 19:09:29 +00:00
|
|
|
module.fail_json(msg=f"{tag} is not a valid tag")
|
2021-05-27 14:58:09 +00:00
|
|
|
kwargs["tags"] = ",".join(kwargs["tags"])
|
|
|
|
|
|
|
|
# -args and skiplock require root@pam user - but can not use api tokens
|
2023-01-31 19:09:29 +00:00
|
|
|
if (
|
2023-12-13 10:23:28 +00:00
|
|
|
module.params["api_user"] == "root@pam"
|
|
|
|
and module.params["args"] is None
|
|
|
|
and not update
|
2023-01-31 19:09:29 +00:00
|
|
|
and module.params["proxmox_default_behavior"] == "compatibility"
|
|
|
|
):
|
|
|
|
kwargs["args"] = vm_args
|
2020-08-18 21:44:49 +00:00
|
|
|
elif module.params["api_user"] == "root@pam" and module.params["args"] is not None:
|
|
|
|
kwargs["args"] = module.params["args"]
|
|
|
|
elif module.params["api_user"] != "root@pam" and module.params["args"] is not None:
|
|
|
|
module.fail_json(msg="args parameter require root@pam user. ")
|
|
|
|
|
|
|
|
if module.params["api_user"] != "root@pam" and module.params["skiplock"] is not None:
|
|
|
|
module.fail_json(msg="skiplock parameter require root@pam user. ")
|
|
|
|
|
|
|
|
if update:
|
2023-01-31 19:09:29 +00:00
|
|
|
return (
|
2023-12-13 10:23:28 +00:00
|
|
|
proxmox_node.qemu(vmid).config.set(
|
|
|
|
name=name, memory=memory, cpu=cpu, cores=cores, sockets=sockets, **kwargs
|
|
|
|
)
|
|
|
|
is None
|
2023-01-31 19:09:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if module.params["clone"] is not None:
|
2020-08-18 21:44:49 +00:00
|
|
|
for param in valid_clone_params:
|
|
|
|
if module.params[param] is not None:
|
|
|
|
clone_params[param] = module.params[param]
|
|
|
|
clone_params.update(
|
|
|
|
dict([k, int(v)] for k, v in clone_params.items() if isinstance(v, bool))
|
|
|
|
)
|
|
|
|
taskid = proxmox_node.qemu(vmid).clone.post(newid=newid, name=name, **clone_params)
|
|
|
|
else:
|
2021-05-27 14:58:09 +00:00
|
|
|
taskid = proxmox_node.qemu.create(
|
2020-08-18 21:44:49 +00:00
|
|
|
vmid=vmid, name=name, memory=memory, cpu=cpu, cores=cores, sockets=sockets, **kwargs
|
|
|
|
)
|
|
|
|
|
2021-05-27 14:58:09 +00:00
|
|
|
if not wait_for_task(module, proxmox, node, taskid):
|
|
|
|
module.fail_json(
|
|
|
|
msg="Reached timeout while waiting for creating VM."
|
2023-01-31 19:09:29 +00:00
|
|
|
f"Last line in task before timeout: {proxmox_node.tasks(taskid).log.get()[:1]}"
|
2021-05-27 14:58:09 +00:00
|
|
|
)
|
|
|
|
return False
|
|
|
|
return True
|
2020-08-18 21:44:49 +00:00
|
|
|
|
|
|
|
|
2021-05-27 14:58:09 +00:00
|
|
|
def start_vm(module, proxmox, vm):
|
|
|
|
vmid = vm[0]["vmid"]
|
|
|
|
proxmox_node = proxmox.nodes(vm[0]["node"])
|
|
|
|
taskid = proxmox_node.qemu(vmid).status.start.post()
|
|
|
|
if not wait_for_task(module, proxmox, vm[0]["node"], taskid):
|
|
|
|
module.fail_json(
|
|
|
|
msg="Reached timeout while waiting for starting VM."
|
2023-01-31 19:09:29 +00:00
|
|
|
f"Last line in task before timeout: {proxmox_node.tasks(taskid).log.get()[:1]}"
|
2021-05-27 14:58:09 +00:00
|
|
|
)
|
|
|
|
return False
|
|
|
|
return True
|
2020-08-18 21:44:49 +00:00
|
|
|
|
|
|
|
|
2021-05-27 14:58:09 +00:00
|
|
|
def stop_vm(module, proxmox, vm, force):
|
|
|
|
vmid = vm[0]["vmid"]
|
|
|
|
proxmox_node = proxmox.nodes(vm[0]["node"])
|
|
|
|
taskid = proxmox_node.qemu(vmid).status.shutdown.post(forceStop=(1 if force else 0))
|
|
|
|
if not wait_for_task(module, proxmox, vm[0]["node"], taskid):
|
|
|
|
module.fail_json(
|
|
|
|
msg="Reached timeout while waiting for stopping VM."
|
2023-01-31 19:09:29 +00:00
|
|
|
f"Last line in task before timeout: {proxmox_node.tasks(taskid).log.get()[:1]}"
|
2021-05-27 14:58:09 +00:00
|
|
|
)
|
|
|
|
return False
|
|
|
|
return True
|
2020-08-18 21:44:49 +00:00
|
|
|
|
2021-05-27 14:58:09 +00:00
|
|
|
|
|
|
|
def proxmox_version(proxmox):
|
|
|
|
apireturn = proxmox.version.get()
|
|
|
|
return LooseVersion(apireturn["version"])
|
2020-08-18 21:44:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
2021-05-27 14:58:09 +00:00
|
|
|
acpi=dict(type="bool"),
|
2020-08-18 21:44:49 +00:00
|
|
|
agent=dict(type="bool"),
|
2021-05-27 14:58:09 +00:00
|
|
|
args=dict(type="str"),
|
2023-01-31 20:35:03 +00:00
|
|
|
api_host=dict(required=True, type="str", fallback=(env_fallback, ["PROXMOX_SERVER"])),
|
|
|
|
api_user=dict(required=True, type="str", fallback=(env_fallback, ["PROXMOX_USER"])),
|
|
|
|
api_password=dict(
|
|
|
|
no_log=True, type="str", fallback=(env_fallback, ["PROXMOX_PASSWORD"])
|
|
|
|
),
|
|
|
|
api_token_id=dict(
|
|
|
|
no_log=True, type="str", fallback=(env_fallback, ["PROXMOX_TOKEN_ID"])
|
|
|
|
),
|
|
|
|
api_token_secret=dict(
|
|
|
|
no_log=True, type="str", fallback=(env_fallback, ["PROXMOX_TOKEN_SECRET"])
|
|
|
|
),
|
2021-05-27 14:58:09 +00:00
|
|
|
autostart=dict(type="bool"),
|
|
|
|
balloon=dict(type="int"),
|
2020-08-18 21:44:49 +00:00
|
|
|
bios=dict(choices=["seabios", "ovmf"]),
|
2021-05-27 14:58:09 +00:00
|
|
|
boot=dict(type="str"),
|
2020-08-18 21:44:49 +00:00
|
|
|
bootdisk=dict(type="str"),
|
2021-05-27 14:58:09 +00:00
|
|
|
cicustom=dict(type="str"),
|
|
|
|
cipassword=dict(type="str", no_log=True),
|
|
|
|
citype=dict(type="str", choices=["nocloud", "configdrive2"]),
|
|
|
|
ciuser=dict(type="str"),
|
|
|
|
clone=dict(type="str"),
|
|
|
|
cores=dict(type="int"),
|
|
|
|
cpu=dict(type="str"),
|
2020-08-18 21:44:49 +00:00
|
|
|
cpulimit=dict(type="int"),
|
2021-05-27 14:58:09 +00:00
|
|
|
cpuunits=dict(type="int"),
|
|
|
|
delete=dict(type="str"),
|
2020-08-18 21:44:49 +00:00
|
|
|
description=dict(type="str"),
|
|
|
|
digest=dict(type="str"),
|
2021-05-27 14:58:09 +00:00
|
|
|
force=dict(type="bool"),
|
2020-08-18 21:44:49 +00:00
|
|
|
format=dict(
|
|
|
|
type="str",
|
2023-12-13 10:23:28 +00:00
|
|
|
choices=["cloop", "cow", "qcow", "qcow2", "qed", "raw", "vmdk", "unspecified"],
|
2020-08-18 21:44:49 +00:00
|
|
|
),
|
|
|
|
freeze=dict(type="bool"),
|
2021-05-27 14:58:09 +00:00
|
|
|
full=dict(type="bool", default=True),
|
2020-08-18 21:44:49 +00:00
|
|
|
hostpci=dict(type="dict"),
|
|
|
|
hotplug=dict(type="str"),
|
|
|
|
hugepages=dict(choices=["any", "2", "1024"]),
|
2021-05-27 14:58:09 +00:00
|
|
|
ide=dict(type="dict"),
|
|
|
|
ipconfig=dict(type="dict"),
|
2020-08-18 21:44:49 +00:00
|
|
|
keyboard=dict(type="str"),
|
2021-05-27 14:58:09 +00:00
|
|
|
kvm=dict(type="bool"),
|
2020-08-18 21:44:49 +00:00
|
|
|
localtime=dict(type="bool"),
|
|
|
|
lock=dict(choices=["migrate", "backup", "snapshot", "rollback"]),
|
|
|
|
machine=dict(type="str"),
|
2021-05-27 14:58:09 +00:00
|
|
|
memory=dict(type="int"),
|
2020-08-18 21:44:49 +00:00
|
|
|
migrate_downtime=dict(type="int"),
|
|
|
|
migrate_speed=dict(type="int"),
|
|
|
|
name=dict(type="str"),
|
2021-05-27 14:58:09 +00:00
|
|
|
nameservers=dict(type="list", elements="str"),
|
2020-08-18 21:44:49 +00:00
|
|
|
net=dict(type="dict"),
|
2021-05-27 14:58:09 +00:00
|
|
|
newid=dict(type="int"),
|
2023-01-31 19:09:29 +00:00
|
|
|
node=dict(type="str"),
|
2020-08-18 21:44:49 +00:00
|
|
|
numa=dict(type="dict"),
|
|
|
|
numa_enabled=dict(type="bool"),
|
2021-05-27 14:58:09 +00:00
|
|
|
onboot=dict(type="bool"),
|
2020-08-18 21:44:49 +00:00
|
|
|
ostype=dict(
|
|
|
|
choices=[
|
2023-12-13 10:23:28 +00:00
|
|
|
"other",
|
|
|
|
"wxp",
|
|
|
|
"w2k",
|
|
|
|
"w2k3",
|
|
|
|
"w2k8",
|
|
|
|
"wvista",
|
|
|
|
"win7",
|
|
|
|
"win8",
|
|
|
|
"win10",
|
|
|
|
"l24",
|
|
|
|
"l26",
|
|
|
|
"solaris",
|
2020-08-18 21:44:49 +00:00
|
|
|
]
|
|
|
|
),
|
|
|
|
parallel=dict(type="dict"),
|
|
|
|
pool=dict(type="str"),
|
|
|
|
protection=dict(type="bool"),
|
|
|
|
reboot=dict(type="bool"),
|
2021-05-27 14:58:09 +00:00
|
|
|
revert=dict(type="str"),
|
2020-08-18 21:44:49 +00:00
|
|
|
sata=dict(type="dict"),
|
|
|
|
scsi=dict(type="dict"),
|
|
|
|
scsihw=dict(
|
|
|
|
choices=[
|
2023-12-13 10:23:28 +00:00
|
|
|
"lsi",
|
|
|
|
"lsi53c810",
|
|
|
|
"virtio-scsi-pci",
|
|
|
|
"virtio-scsi-single",
|
|
|
|
"megasas",
|
|
|
|
"pvscsi",
|
2020-08-18 21:44:49 +00:00
|
|
|
]
|
|
|
|
),
|
|
|
|
serial=dict(type="dict"),
|
2021-05-27 14:58:09 +00:00
|
|
|
searchdomains=dict(type="list", elements="str"),
|
2020-08-18 21:44:49 +00:00
|
|
|
shares=dict(type="int"),
|
|
|
|
skiplock=dict(type="bool"),
|
|
|
|
smbios=dict(type="str"),
|
|
|
|
snapname=dict(type="str"),
|
2021-05-27 14:58:09 +00:00
|
|
|
sockets=dict(type="int"),
|
|
|
|
sshkeys=dict(type="str", no_log=False),
|
2020-08-18 21:44:49 +00:00
|
|
|
startdate=dict(type="str"),
|
2023-01-31 19:09:29 +00:00
|
|
|
startup=dict(type="str"),
|
2020-08-18 21:44:49 +00:00
|
|
|
state=dict(
|
|
|
|
default="present",
|
2023-12-13 10:23:28 +00:00
|
|
|
choices=["present", "absent", "stopped", "started", "restarted", "current"],
|
2020-08-18 21:44:49 +00:00
|
|
|
),
|
|
|
|
storage=dict(type="str"),
|
2021-05-27 14:58:09 +00:00
|
|
|
tablet=dict(type="bool"),
|
|
|
|
tags=dict(type="list", elements="str"),
|
2020-08-18 21:44:49 +00:00
|
|
|
target=dict(type="str"),
|
|
|
|
tdf=dict(type="bool"),
|
2021-05-27 14:58:09 +00:00
|
|
|
template=dict(type="bool"),
|
2020-08-18 21:44:49 +00:00
|
|
|
timeout=dict(type="int", default=30),
|
2021-05-27 14:58:09 +00:00
|
|
|
update=dict(type="bool", default=False),
|
2023-01-31 19:09:29 +00:00
|
|
|
verify_ssl=dict(type="bool", default=True),
|
2021-05-27 14:58:09 +00:00
|
|
|
vcpus=dict(type="int"),
|
2020-08-18 21:44:49 +00:00
|
|
|
vga=dict(
|
|
|
|
choices=[
|
2023-12-13 10:23:28 +00:00
|
|
|
"std",
|
|
|
|
"cirrus",
|
|
|
|
"vmware",
|
|
|
|
"qxl",
|
|
|
|
"serial0",
|
|
|
|
"serial1",
|
|
|
|
"serial2",
|
|
|
|
"serial3",
|
|
|
|
"qxl2",
|
|
|
|
"qxl3",
|
|
|
|
"qxl4",
|
2020-08-18 21:44:49 +00:00
|
|
|
]
|
|
|
|
),
|
2021-05-27 14:58:09 +00:00
|
|
|
virtio=dict(type="dict"),
|
|
|
|
vmid=dict(type="int"),
|
2023-01-31 19:09:29 +00:00
|
|
|
watchdog=dict(type="str"),
|
2021-05-27 14:58:09 +00:00
|
|
|
proxmox_default_behavior=dict(type="str", choices=["compatibility", "no_defaults"]),
|
2020-08-18 21:44:49 +00:00
|
|
|
),
|
2023-12-13 10:23:28 +00:00
|
|
|
mutually_exclusive=[
|
|
|
|
("delete", "revert"),
|
|
|
|
("delete", "update"),
|
|
|
|
("revert", "update"),
|
|
|
|
("clone", "update"),
|
|
|
|
("clone", "delete"),
|
|
|
|
("clone", "revert"),
|
|
|
|
],
|
2021-05-27 14:58:09 +00:00
|
|
|
required_together=[("api_token_id", "api_token_secret")],
|
|
|
|
required_one_of=[("name", "vmid"), ("api_password", "api_token_id")],
|
|
|
|
required_if=[("state", "present", ["node"])],
|
2020-08-18 21:44:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if not HAS_PROXMOXER:
|
|
|
|
module.fail_json(msg="proxmoxer required for this module")
|
|
|
|
|
|
|
|
api_host = module.params["api_host"]
|
|
|
|
api_password = module.params["api_password"]
|
2021-05-27 14:58:09 +00:00
|
|
|
api_token_id = module.params["api_token_id"]
|
|
|
|
api_token_secret = module.params["api_token_secret"]
|
|
|
|
api_user = module.params["api_user"]
|
2020-08-18 21:44:49 +00:00
|
|
|
clone = module.params["clone"]
|
|
|
|
cpu = module.params["cpu"]
|
|
|
|
cores = module.params["cores"]
|
|
|
|
delete = module.params["delete"]
|
|
|
|
memory = module.params["memory"]
|
|
|
|
name = module.params["name"]
|
|
|
|
newid = module.params["newid"]
|
|
|
|
node = module.params["node"]
|
|
|
|
revert = module.params["revert"]
|
|
|
|
sockets = module.params["sockets"]
|
|
|
|
state = module.params["state"]
|
|
|
|
update = bool(module.params["update"])
|
|
|
|
vmid = module.params["vmid"]
|
2023-01-31 19:09:29 +00:00
|
|
|
verify_ssl = module.params["verify_ssl"]
|
2020-08-18 21:44:49 +00:00
|
|
|
|
2021-05-27 14:58:09 +00:00
|
|
|
if module.params["proxmox_default_behavior"] is None:
|
|
|
|
module.params["proxmox_default_behavior"] = "compatibility"
|
|
|
|
if module.params["proxmox_default_behavior"] == "compatibility":
|
|
|
|
old_default_values = dict(
|
|
|
|
acpi=True,
|
|
|
|
autostart=False,
|
|
|
|
balloon=0,
|
|
|
|
boot="cnd",
|
|
|
|
cores=1,
|
|
|
|
cpu="kvm64",
|
|
|
|
cpuunits=1000,
|
|
|
|
format="qcow2",
|
|
|
|
kvm=True,
|
|
|
|
memory=512,
|
|
|
|
ostype="l26",
|
|
|
|
sockets=1,
|
|
|
|
tablet=False,
|
|
|
|
template=False,
|
|
|
|
vga="std",
|
|
|
|
)
|
|
|
|
for param, value in old_default_values.items():
|
|
|
|
if module.params[param] is None:
|
|
|
|
module.params[param] = value
|
|
|
|
|
|
|
|
if module.params["format"] == "unspecified":
|
|
|
|
module.params["format"] = None
|
|
|
|
|
|
|
|
auth_args = {"user": api_user}
|
|
|
|
if not (api_token_id and api_token_secret):
|
|
|
|
auth_args["password"] = api_password
|
|
|
|
else:
|
|
|
|
auth_args["token_name"] = api_token_id
|
|
|
|
auth_args["token_value"] = api_token_secret
|
2020-08-18 21:44:49 +00:00
|
|
|
|
2023-01-31 19:09:29 +00:00
|
|
|
if not verify_ssl and HAS_URLLIB3:
|
|
|
|
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
|
|
|
|
2020-08-18 21:44:49 +00:00
|
|
|
try:
|
2023-01-31 19:09:29 +00:00
|
|
|
proxmox = ProxmoxAPI(api_host, verify_ssl=verify_ssl, **auth_args)
|
|
|
|
global PVE_MAJOR_VERSION
|
2021-05-27 14:58:09 +00:00
|
|
|
version = proxmox_version(proxmox)
|
|
|
|
PVE_MAJOR_VERSION = 3 if version < LooseVersion("4.0") else version.version[0]
|
2023-01-31 19:09:29 +00:00
|
|
|
except Exception as e: # noqa
|
|
|
|
module.fail_json(msg=f"authorization on proxmox cluster failed with exception: {e}")
|
2020-08-18 21:44:49 +00:00
|
|
|
|
2021-05-27 14:58:09 +00:00
|
|
|
# If vmid is not defined then retrieve its value from the vm name,
|
|
|
|
# the cloned vm name or retrieve the next free VM id from ProxmoxAPI.
|
2020-08-18 21:44:49 +00:00
|
|
|
if not vmid:
|
2021-05-27 14:58:09 +00:00
|
|
|
if state == "present" and not update and not clone and not delete and not revert:
|
2020-08-18 21:44:49 +00:00
|
|
|
try:
|
|
|
|
vmid = get_nextvmid(module, proxmox)
|
2023-01-31 19:09:29 +00:00
|
|
|
except Exception: # noqa
|
2020-08-18 21:44:49 +00:00
|
|
|
module.fail_json(
|
2023-01-31 19:09:29 +00:00
|
|
|
msg=f"Can't get the next vmid for VM {name} automatically."
|
|
|
|
"Ensure your cluster state is good"
|
2020-08-18 21:44:49 +00:00
|
|
|
)
|
|
|
|
else:
|
2021-05-27 14:58:09 +00:00
|
|
|
clone_target = clone or name
|
2020-08-18 21:44:49 +00:00
|
|
|
try:
|
2021-05-27 14:58:09 +00:00
|
|
|
vmid = get_vmid(proxmox, clone_target)[0]
|
2023-01-31 19:09:29 +00:00
|
|
|
except Exception: # noqa
|
2021-05-27 14:58:09 +00:00
|
|
|
vmid = -1
|
2020-08-18 21:44:49 +00:00
|
|
|
|
|
|
|
if clone is not None:
|
2021-05-27 14:58:09 +00:00
|
|
|
# If newid is not defined then retrieve the next free id from ProxmoxAPI
|
2020-08-18 21:44:49 +00:00
|
|
|
if not newid:
|
|
|
|
try:
|
|
|
|
newid = get_nextvmid(module, proxmox)
|
2023-01-31 19:09:29 +00:00
|
|
|
except Exception: # noqa
|
2020-08-18 21:44:49 +00:00
|
|
|
module.fail_json(
|
2023-01-31 19:09:29 +00:00
|
|
|
msg=f"Can't get the next vmid for VM {name} automatically."
|
|
|
|
"Ensure your cluster state is good"
|
2020-08-18 21:44:49 +00:00
|
|
|
)
|
|
|
|
|
2021-05-27 14:58:09 +00:00
|
|
|
# Ensure source VM name exists when cloning
|
2023-12-13 10:23:28 +00:00
|
|
|
if vmid == -1:
|
2023-01-31 19:09:29 +00:00
|
|
|
module.fail_json(msg=f"VM with name = {clone} does not exist in cluster")
|
2021-05-27 14:58:09 +00:00
|
|
|
|
|
|
|
# Ensure source VM id exists when cloning
|
|
|
|
if not get_vm(proxmox, vmid):
|
2023-01-31 19:09:29 +00:00
|
|
|
module.fail_json(vmid=vmid, msg=f"VM with vmid = {vmid} does not exist in cluster")
|
2021-05-27 14:58:09 +00:00
|
|
|
|
|
|
|
# Ensure the choosen VM name doesn't already exist when cloning
|
|
|
|
if get_vmid(proxmox, name):
|
2023-01-31 19:09:29 +00:00
|
|
|
module.exit_json(changed=False, vmid=vmid, msg=f"VM with name <{name}> already exists")
|
2021-05-27 14:58:09 +00:00
|
|
|
|
|
|
|
# Ensure the choosen VM id doesn't already exist when cloning
|
|
|
|
if get_vm(proxmox, newid):
|
|
|
|
module.exit_json(
|
2023-01-31 19:09:29 +00:00
|
|
|
changed=False, vmid=vmid, msg=f"vmid {newid} with VM name {name} already exists"
|
2021-05-27 14:58:09 +00:00
|
|
|
)
|
|
|
|
|
2020-08-18 21:44:49 +00:00
|
|
|
if delete is not None:
|
|
|
|
try:
|
2021-05-27 14:58:09 +00:00
|
|
|
settings(module, proxmox, vmid, node, name, delete=delete)
|
2020-08-18 21:44:49 +00:00
|
|
|
module.exit_json(
|
2023-01-31 19:09:29 +00:00
|
|
|
changed=True, vmid=vmid, msg=f"Settings has deleted on VM {name} with vmid {vmid}"
|
2020-08-18 21:44:49 +00:00
|
|
|
)
|
2023-01-31 19:09:29 +00:00
|
|
|
except Exception as e: # noqa
|
2020-08-18 21:44:49 +00:00
|
|
|
module.fail_json(
|
2023-12-13 10:23:28 +00:00
|
|
|
vmid=vmid, msg=f"Unable to delete settings on VM {name} with vmid {vmid}: {e!s}"
|
2020-08-18 21:44:49 +00:00
|
|
|
)
|
2021-05-27 14:58:09 +00:00
|
|
|
|
|
|
|
if revert is not None:
|
2020-08-18 21:44:49 +00:00
|
|
|
try:
|
2021-05-27 14:58:09 +00:00
|
|
|
settings(module, proxmox, vmid, node, name, revert=revert)
|
2020-08-18 21:44:49 +00:00
|
|
|
module.exit_json(
|
2023-12-13 10:23:28 +00:00
|
|
|
changed=True, vmid=vmid, msg=f"Settings has reverted on VM {name} with vmid {vmid}"
|
2020-08-18 21:44:49 +00:00
|
|
|
)
|
2023-01-31 19:09:29 +00:00
|
|
|
except Exception as e: # noqa
|
2020-08-18 21:44:49 +00:00
|
|
|
module.fail_json(
|
2021-05-27 14:58:09 +00:00
|
|
|
vmid=vmid,
|
2023-01-31 19:09:29 +00:00
|
|
|
msg=f"Unable to revert settings on VM {name} with vmid {vmid}:"
|
2023-12-13 10:23:28 +00:00
|
|
|
f"Maybe is not a pending task...{e!s}",
|
2020-08-18 21:44:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if state == "present":
|
|
|
|
try:
|
|
|
|
if get_vm(proxmox, vmid) and not (update or clone):
|
|
|
|
module.exit_json(
|
2023-01-31 19:09:29 +00:00
|
|
|
changed=False, vmid=vmid, msg=f"VM with vmid <{vmid}> already exists"
|
2020-08-18 21:44:49 +00:00
|
|
|
)
|
|
|
|
elif get_vmid(proxmox, name) and not (update or clone):
|
|
|
|
module.exit_json(
|
2023-01-31 19:09:29 +00:00
|
|
|
changed=False, vmid=vmid, msg=f"VM with name <{name}> already exists"
|
2020-08-18 21:44:49 +00:00
|
|
|
)
|
|
|
|
elif not (node, name):
|
|
|
|
module.fail_json(msg="node, name is mandatory for creating/updating vm")
|
|
|
|
elif not node_check(proxmox, node):
|
2023-01-31 19:09:29 +00:00
|
|
|
module.fail_json(msg=f"node '{node}' does not exist in cluster")
|
2020-08-18 21:44:49 +00:00
|
|
|
|
2021-12-05 20:47:23 +00:00
|
|
|
if not clone:
|
|
|
|
get_vminfo(
|
|
|
|
module,
|
|
|
|
proxmox,
|
|
|
|
node,
|
|
|
|
vmid,
|
|
|
|
ide=module.params["ide"],
|
|
|
|
net=module.params["net"],
|
|
|
|
sata=module.params["sata"],
|
|
|
|
scsi=module.params["scsi"],
|
2023-12-13 10:23:28 +00:00
|
|
|
virtio=module.params["virtio"],
|
2021-12-05 20:47:23 +00:00
|
|
|
)
|
|
|
|
|
2020-08-18 21:44:49 +00:00
|
|
|
create_vm(
|
|
|
|
module,
|
|
|
|
proxmox,
|
|
|
|
vmid,
|
|
|
|
newid,
|
|
|
|
node,
|
|
|
|
name,
|
|
|
|
memory,
|
|
|
|
cpu,
|
|
|
|
cores,
|
|
|
|
sockets,
|
|
|
|
update,
|
|
|
|
acpi=module.params["acpi"],
|
|
|
|
agent=module.params["agent"],
|
|
|
|
autostart=module.params["autostart"],
|
|
|
|
balloon=module.params["balloon"],
|
|
|
|
bios=module.params["bios"],
|
|
|
|
boot=module.params["boot"],
|
|
|
|
bootdisk=module.params["bootdisk"],
|
2021-05-27 14:58:09 +00:00
|
|
|
cicustom=module.params["cicustom"],
|
2020-08-18 21:44:49 +00:00
|
|
|
cipassword=module.params["cipassword"],
|
|
|
|
citype=module.params["citype"],
|
2021-05-27 14:58:09 +00:00
|
|
|
ciuser=module.params["ciuser"],
|
2020-08-18 21:44:49 +00:00
|
|
|
cpulimit=module.params["cpulimit"],
|
|
|
|
cpuunits=module.params["cpuunits"],
|
|
|
|
description=module.params["description"],
|
|
|
|
digest=module.params["digest"],
|
|
|
|
force=module.params["force"],
|
|
|
|
freeze=module.params["freeze"],
|
|
|
|
hostpci=module.params["hostpci"],
|
|
|
|
hotplug=module.params["hotplug"],
|
|
|
|
hugepages=module.params["hugepages"],
|
|
|
|
ide=module.params["ide"],
|
|
|
|
ipconfig=module.params["ipconfig"],
|
|
|
|
keyboard=module.params["keyboard"],
|
|
|
|
kvm=module.params["kvm"],
|
|
|
|
localtime=module.params["localtime"],
|
|
|
|
lock=module.params["lock"],
|
|
|
|
machine=module.params["machine"],
|
|
|
|
migrate_downtime=module.params["migrate_downtime"],
|
|
|
|
migrate_speed=module.params["migrate_speed"],
|
|
|
|
net=module.params["net"],
|
|
|
|
numa=module.params["numa"],
|
|
|
|
numa_enabled=module.params["numa_enabled"],
|
|
|
|
onboot=module.params["onboot"],
|
|
|
|
ostype=module.params["ostype"],
|
|
|
|
parallel=module.params["parallel"],
|
|
|
|
pool=module.params["pool"],
|
|
|
|
protection=module.params["protection"],
|
|
|
|
reboot=module.params["reboot"],
|
|
|
|
sata=module.params["sata"],
|
|
|
|
scsi=module.params["scsi"],
|
|
|
|
scsihw=module.params["scsihw"],
|
|
|
|
serial=module.params["serial"],
|
|
|
|
shares=module.params["shares"],
|
|
|
|
skiplock=module.params["skiplock"],
|
|
|
|
smbios1=module.params["smbios"],
|
|
|
|
snapname=module.params["snapname"],
|
|
|
|
sshkeys=module.params["sshkeys"],
|
|
|
|
startdate=module.params["startdate"],
|
|
|
|
startup=module.params["startup"],
|
|
|
|
tablet=module.params["tablet"],
|
2021-05-27 14:58:09 +00:00
|
|
|
tags=module.params["tags"],
|
2020-08-18 21:44:49 +00:00
|
|
|
target=module.params["target"],
|
|
|
|
tdf=module.params["tdf"],
|
|
|
|
template=module.params["template"],
|
|
|
|
vcpus=module.params["vcpus"],
|
|
|
|
vga=module.params["vga"],
|
|
|
|
virtio=module.params["virtio"],
|
2023-12-13 10:23:28 +00:00
|
|
|
watchdog=module.params["watchdog"],
|
2020-08-18 21:44:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if update:
|
|
|
|
module.exit_json(
|
2023-01-31 19:09:29 +00:00
|
|
|
changed=True, vmid=vmid, msg=f"VM {name} with vmid {vmid} updated"
|
2020-08-18 21:44:49 +00:00
|
|
|
)
|
|
|
|
elif clone is not None:
|
|
|
|
module.exit_json(
|
|
|
|
changed=True,
|
2021-05-27 14:58:09 +00:00
|
|
|
vmid=vmid,
|
2023-12-13 10:23:28 +00:00
|
|
|
msg=f"VM {name} with newid {newid} cloned from vm with vmid {vmid}",
|
2020-08-18 21:44:49 +00:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
module.exit_json(
|
2023-01-31 19:09:29 +00:00
|
|
|
changed=True, msg=f"VM {name} with vmid {vmid} deployed", **results
|
2020-08-18 21:44:49 +00:00
|
|
|
)
|
2023-01-31 19:09:29 +00:00
|
|
|
except Exception as e: # noqa
|
2020-08-18 21:44:49 +00:00
|
|
|
if update:
|
|
|
|
module.fail_json(
|
2023-01-31 19:09:29 +00:00
|
|
|
vmid=vmid, msg=f"Unable to update vm {name} with vmid {vmid}=" + str(e)
|
2020-08-18 21:44:49 +00:00
|
|
|
)
|
|
|
|
elif clone is not None:
|
|
|
|
module.fail_json(
|
2023-01-31 19:09:29 +00:00
|
|
|
vmid=vmid, msg=f"Unable to clone vm {name} from vmid {vmid}=" + str(e)
|
2020-08-18 21:44:49 +00:00
|
|
|
)
|
|
|
|
else:
|
2021-05-27 14:58:09 +00:00
|
|
|
module.fail_json(
|
|
|
|
vmid=vmid,
|
2023-12-13 10:23:28 +00:00
|
|
|
msg=f"creation of qemu VM {name} with vmid {vmid} failed with exception={e}",
|
2020-08-18 21:44:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
elif state == "started":
|
2021-05-27 14:58:09 +00:00
|
|
|
status = {}
|
2020-08-18 21:44:49 +00:00
|
|
|
try:
|
2023-12-13 10:23:28 +00:00
|
|
|
if vmid == -1:
|
2023-01-31 19:09:29 +00:00
|
|
|
module.fail_json(msg=f"VM with name = {name} does not exist in cluster")
|
2020-08-18 21:44:49 +00:00
|
|
|
vm = get_vm(proxmox, vmid)
|
|
|
|
if not vm:
|
2023-01-31 19:09:29 +00:00
|
|
|
module.fail_json(vmid=vmid, msg=f"VM with vmid <{vmid}> does not exist in cluster")
|
2021-05-27 14:58:09 +00:00
|
|
|
status["status"] = vm[0]["status"]
|
|
|
|
if vm[0]["status"] == "running":
|
|
|
|
module.exit_json(
|
2023-01-31 19:09:29 +00:00
|
|
|
changed=False, vmid=vmid, msg=f"VM {vmid} is already running", **status
|
2021-05-27 14:58:09 +00:00
|
|
|
)
|
2020-08-18 21:44:49 +00:00
|
|
|
|
2021-05-27 14:58:09 +00:00
|
|
|
if start_vm(module, proxmox, vm):
|
2023-01-31 19:09:29 +00:00
|
|
|
module.exit_json(changed=True, vmid=vmid, msg=f"VM {vmid} started", **status)
|
|
|
|
except Exception as e: # noqa
|
2021-05-27 14:58:09 +00:00
|
|
|
module.fail_json(
|
2023-01-31 19:09:29 +00:00
|
|
|
vmid=vmid, msg=f"starting of VM {vmid} failed with exception: {e}", **status
|
2021-05-27 14:58:09 +00:00
|
|
|
)
|
2020-08-18 21:44:49 +00:00
|
|
|
|
|
|
|
elif state == "stopped":
|
2021-05-27 14:58:09 +00:00
|
|
|
status = {}
|
2020-08-18 21:44:49 +00:00
|
|
|
try:
|
2023-12-13 10:23:28 +00:00
|
|
|
if vmid == -1:
|
2023-01-31 19:09:29 +00:00
|
|
|
module.fail_json(msg=f"VM with name = {name} does not exist in cluster")
|
2021-05-27 14:58:09 +00:00
|
|
|
|
2020-08-18 21:44:49 +00:00
|
|
|
vm = get_vm(proxmox, vmid)
|
|
|
|
if not vm:
|
2023-01-31 19:09:29 +00:00
|
|
|
module.fail_json(vmid=vmid, msg=f"VM with vmid = {vmid} does not exist in cluster")
|
2020-08-18 21:44:49 +00:00
|
|
|
|
2021-05-27 14:58:09 +00:00
|
|
|
status["status"] = vm[0]["status"]
|
|
|
|
if vm[0]["status"] == "stopped":
|
|
|
|
module.exit_json(
|
2023-01-31 19:09:29 +00:00
|
|
|
changed=False, vmid=vmid, msg=f"VM {vmid} is already stopped", **status
|
2021-05-27 14:58:09 +00:00
|
|
|
)
|
2020-08-18 21:44:49 +00:00
|
|
|
|
2021-05-27 14:58:09 +00:00
|
|
|
if stop_vm(module, proxmox, vm, force=module.params["force"]):
|
|
|
|
module.exit_json(
|
2023-01-31 19:09:29 +00:00
|
|
|
changed=True, vmid=vmid, msg=f"VM {vmid} is shutting down", **status
|
2021-05-27 14:58:09 +00:00
|
|
|
)
|
2023-01-31 19:09:29 +00:00
|
|
|
except Exception as e: # noqa
|
2021-05-27 14:58:09 +00:00
|
|
|
module.fail_json(
|
2023-01-31 19:09:29 +00:00
|
|
|
vmid=vmid, msg=f"stopping of VM {vmid} failed with exception: {e}", **status
|
2021-05-27 14:58:09 +00:00
|
|
|
)
|
2020-08-18 21:44:49 +00:00
|
|
|
|
|
|
|
elif state == "restarted":
|
2021-05-27 14:58:09 +00:00
|
|
|
status = {}
|
2020-08-18 21:44:49 +00:00
|
|
|
try:
|
2023-12-13 10:23:28 +00:00
|
|
|
if vmid == -1:
|
2023-01-31 19:09:29 +00:00
|
|
|
module.fail_json(msg=f"VM with name = {name} does not exist in cluster")
|
2020-08-18 21:44:49 +00:00
|
|
|
|
|
|
|
vm = get_vm(proxmox, vmid)
|
|
|
|
if not vm:
|
2023-01-31 19:09:29 +00:00
|
|
|
module.fail_json(vmid=vmid, msg=f"VM with vmid = {vmid} does not exist in cluster")
|
2021-05-27 14:58:09 +00:00
|
|
|
status["status"] = vm[0]["status"]
|
|
|
|
if vm[0]["status"] == "stopped":
|
2020-08-18 21:44:49 +00:00
|
|
|
module.exit_json(
|
2023-01-31 19:09:29 +00:00
|
|
|
changed=False, vmid=vmid, msg=f"VM {vmid} is not running", **status
|
2020-08-18 21:44:49 +00:00
|
|
|
)
|
|
|
|
|
2023-12-13 10:23:28 +00:00
|
|
|
if stop_vm(module, proxmox, vm, force=module.params["force"]) and start_vm(
|
|
|
|
module, proxmox, vm
|
|
|
|
):
|
2023-01-31 19:09:29 +00:00
|
|
|
module.exit_json(changed=True, vmid=vmid, msg=f"VM {vmid} is restarted", **status)
|
|
|
|
except Exception as e: # noqa
|
2021-05-27 14:58:09 +00:00
|
|
|
module.fail_json(
|
2023-01-31 19:09:29 +00:00
|
|
|
vmid=vmid, msg=f"restarting of VM {vmid} failed with exception: {e}", **status
|
2021-05-27 14:58:09 +00:00
|
|
|
)
|
2020-08-18 21:44:49 +00:00
|
|
|
|
2021-05-27 14:58:09 +00:00
|
|
|
elif state == "absent":
|
2020-08-18 21:44:49 +00:00
|
|
|
status = {}
|
|
|
|
try:
|
|
|
|
vm = get_vm(proxmox, vmid)
|
|
|
|
if not vm:
|
2021-05-27 14:58:09 +00:00
|
|
|
module.exit_json(changed=False, vmid=vmid)
|
|
|
|
|
|
|
|
proxmox_node = proxmox.nodes(vm[0]["node"])
|
|
|
|
status["status"] = vm[0]["status"]
|
|
|
|
if vm[0]["status"] == "running":
|
|
|
|
if module.params["force"]:
|
|
|
|
stop_vm(module, proxmox, vm, True)
|
|
|
|
else:
|
|
|
|
module.exit_json(
|
|
|
|
changed=False,
|
|
|
|
vmid=vmid,
|
2023-12-13 10:23:28 +00:00
|
|
|
msg=f"VM {vmid} is running. Stop it before deletion or use force=yes.",
|
2021-05-27 14:58:09 +00:00
|
|
|
)
|
|
|
|
taskid = proxmox_node.qemu.delete(vmid)
|
|
|
|
if not wait_for_task(module, proxmox, vm[0]["node"], taskid):
|
|
|
|
module.fail_json(
|
|
|
|
msg="Reached timeout while waiting for removing VM."
|
2023-01-31 19:09:29 +00:00
|
|
|
f"Last line in task before timeout: {proxmox_node.tasks(taskid).log.get()[:1]}"
|
2020-08-18 21:44:49 +00:00
|
|
|
)
|
2021-05-27 14:58:09 +00:00
|
|
|
else:
|
2023-01-31 19:09:29 +00:00
|
|
|
module.exit_json(changed=True, vmid=vmid, msg=f"VM {vmid} removed")
|
|
|
|
except Exception as e: # noqa
|
|
|
|
module.fail_json(msg=f"deletion of VM {vmid} failed with exception: {e}")
|
2021-05-27 14:58:09 +00:00
|
|
|
|
|
|
|
elif state == "current":
|
|
|
|
status = {}
|
2023-12-13 10:23:28 +00:00
|
|
|
if vmid == -1:
|
2023-01-31 19:09:29 +00:00
|
|
|
module.fail_json(msg=f"VM with name = {name} does not exist in cluster")
|
2021-05-27 14:58:09 +00:00
|
|
|
vm = get_vm(proxmox, vmid)
|
|
|
|
if not vm:
|
2023-01-31 19:09:29 +00:00
|
|
|
module.fail_json(msg=f"VM with vmid = {vmid} does not exist in cluster")
|
2021-05-27 14:58:09 +00:00
|
|
|
if not name:
|
|
|
|
name = vm[0]["name"]
|
|
|
|
current = proxmox.nodes(vm[0]["node"]).qemu(vmid).status.current.get()["status"]
|
|
|
|
status["status"] = current
|
|
|
|
if status:
|
|
|
|
module.exit_json(
|
|
|
|
changed=False,
|
|
|
|
vmid=vmid,
|
2023-01-31 19:09:29 +00:00
|
|
|
msg=f"VM {name} with vmid = {vmid} is {current}",
|
2023-12-13 10:23:28 +00:00
|
|
|
**status,
|
2020-08-18 21:44:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-12-06 20:12:30 +00:00
|
|
|
def _extract_disks(item):
|
|
|
|
disks = defaultdict(dict)
|
2021-12-05 20:47:23 +00:00
|
|
|
for k, v in item.items():
|
|
|
|
if re.match(r"(scsi|virtio|ide|sata)[0-9]", k):
|
2021-12-06 20:12:30 +00:00
|
|
|
disks[k]["opts"] = []
|
2021-12-05 20:47:23 +00:00
|
|
|
for val in v.split(","):
|
|
|
|
if len(val.split(":")) == 2:
|
|
|
|
storage = val.split(":")
|
2021-12-06 20:12:30 +00:00
|
|
|
disks[k]["storage_id"] = storage[0]
|
|
|
|
disks[k]["storage_opts"] = storage[1]
|
|
|
|
else:
|
|
|
|
disks[k]["opts"].append(val)
|
|
|
|
|
|
|
|
return disks
|
|
|
|
|
|
|
|
|
|
|
|
def _extract_nets(item):
|
|
|
|
nets = defaultdict(dict)
|
|
|
|
for k, v in item.items():
|
|
|
|
if re.match(r"net[0-9]", k):
|
|
|
|
nets[k]["opts"] = []
|
|
|
|
for val in v.split(","):
|
2023-01-31 19:09:29 +00:00
|
|
|
if (
|
|
|
|
any(val.startswith(s) for s in ["e1000", "rtl8139", "virtio", "vmxnet3"])
|
|
|
|
and len(val.split("=")) == 2
|
|
|
|
):
|
|
|
|
net = val.split("=")
|
|
|
|
nets[k]["net_id"] = net[0]
|
|
|
|
nets[k]["net_opts"] = net[1]
|
2021-12-05 20:47:23 +00:00
|
|
|
else:
|
2021-12-06 20:12:30 +00:00
|
|
|
nets[k]["opts"].append(val)
|
2021-12-05 20:47:23 +00:00
|
|
|
|
2021-12-06 20:12:30 +00:00
|
|
|
return nets
|
2021-12-05 20:47:23 +00:00
|
|
|
|
|
|
|
|
2020-08-18 21:44:49 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|