我如何使用 ansible 模块 proxmox_kvm 调整硬盘大小

How I resize hard disk with ansible module proxmox_kvm

我创建了一个 8Gb 磁盘大小的 ProxMox VM 模板。使用 ansible 模块 proxmo_kvm 我克隆了它,但我无法调整 SCSI 磁盘的大小。有可能吗?如果是的话,你能给我一些解释吗?

项目文件层次结构:

.
├── playbook.yml
└─── proxmox-vm/
    ├── tasks/
    │   └── main.yml
    └── vars/
        └── main.yml

playbook.yml:

- hosts: localhost
  connection: local

  roles:
    - proxmox-vm

proxmox-vm/vars/main.yml:

---
# vars file for proxmox-vm
proxmox:
  api:
    host: 127.0.0.1
    user: 'user@pam'
    password: 'password'
  node: workstation
  vm:
    name: test-debian
    clone: debian-9.8
    full: yes
    scsi:
      size: 16G

proxmox-vm/tasks/main.yml:

---
# tasks file for proxmox-vm
- name: create VM from template
  proxmox_kvm:
    api_host: "{{ proxmox.api.host }}"
    api_user: "{{ proxmox.api.user }}"
    api_password: "{{ proxmox.api.password }}"
    node: "{{ proxmox.node }}"
    name: "{{ proxmox.vm.name }}"
    clone: "{{ proxmox.vm.clone }}"
    full: yes
    scsi: "{{ proxmox.vm.scsi }}"

运行 Ansible 剧本:

ansible-playbook playbook.yml

PLAY [localhost] ***************************************************************

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [proxmox-vm : create VM from template] ************************************
changed: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0

我期待一个新的 16Gb 磁盘大小 VM,但实际磁盘大小为 8Gb(初始磁盘大小模板)

目前不可能,因为 ansible 模块 proxmox_kvm 不允许(cloneupdate 都不允许)。见source code of the module (proxmox_kvm.py)

# [...]
def create_vm(module, proxmox, vmid, newid, node, name, memory, cpu, cores, sockets, timeout, update, **kwargs):
# [...]
    # valide clone parameters
    valid_clone_params = ['format', 'full', 'pool', 'snapname', 'storage', 'target']
# [...]
    elif module.params['clone'] is not None:
        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)
# [...]
    # If update, don't update disk (virtio, ide, sata, scsi) and network interface
    if update:
# [...]
        if 'scsi' in kwargs:
            del kwargs['scsi']
# [...]

ansible 模块 uri 是可能的。将以下任务添加到文件 proxmox-vm/tasks/main.yml

# get VMID
- name: get VM state
  proxmox_kvm:
    api_host: "{{ proxmox.api.host }}"
    api_user: "{{ proxmox.api.user }}"
    api_password: "{{ proxmox.api.password }}"
    node: "{{ proxmox.node}}"
    name: "{{ proxmox.vm.name }}"
    state: current
  register: result
- name: set fact VMID
  set_fact:
    vmid: "{{ result.msg | regex_replace('.*vmid = ([1-9][0-9]{0,3}).*', '\1') }}"
# Authentication
- name: authentication
  uri:
    url: "https://{{ proxmox.api.host }}:8006/api2/json/access/ticket"
    method: POST
    body_format: form-urlencoded
    body:
      username: "{{ proxmox.api.user }}"
      password: "{{ proxmox.api.password }}"
  register: auth
# Resize disk
- name: resize disk
  uri:
    url: "https://{{ proxmox.api.host }}:8006/api2/json/nodes/{{ proxmox.node }}/qemu/{{ vmid }}/resize"
    method: PUT
    headers: 
      Cookie: "PVEAuthCookie={{ auth.json.data.ticket }}"
      CSRFPreventionToken: "{{ auth.json.data.CSRFPreventionToken }}"
    body_format: form-urlencoded
    body:
      disk: scsi0
      size: "{{ proxmox.vm.scsi.size }}"