Mikael Flora
Mikael Flora

Reputation: 306

How I resize hard disk with ansible module proxmox_kvm

I've created a 8Gb disksize ProxMox VM template. Using ansible module proxmo_kvm I clone it but I can't resize the SCSI disk. Is it possible to do ? if it is could you give me some explanations to understand how ?

Project file hierarchy:

.
├── 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 }}"

Running Ansible playbook:

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

I'm expecting a new 16Gb disksize VM, but the actual disksize is 8Gb (initial disksize template)

Upvotes: 4

Views: 4292

Answers (2)

Mikael Flora
Mikael Flora

Reputation: 306

It's possible with ansible module uri. Add following tasks to the file 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 }}"

Upvotes: 2

Mikael Flora
Mikael Flora

Reputation: 306

Currently it is not possible because ansible module proxmox_kvm does't allow it (neither for clone nor for update). See the 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']
# [...]

Upvotes: 1

Related Questions