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

时间:2019-03-24 10:53:11

标签: ansible kvm proxmox

我已经创建了一个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(初始磁盘大小模板)

2 个答案:

答案 0 :(得分:0)

当前不可能,因为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']
# [...]

答案 1 :(得分:0)

使用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 }}"