Search code examples
ansibleqemuproxmox

Proxmox Config File Does Not Exist


Given the following Ansible playbook:

---
- name: Create VM template
  hosts: pve
  gather_facts: false
  vars:
    vm:
      cloud_image_url: https://cloud-images.ubuntu.com/minimal/releases/jammy/release/ubuntu-22.04-minimal-cloudimg-amd64.img
      cloud_image_path: /tmp/ubuntu-2204-minimal-cloudimg-amd64.qcow2
      template_id: 900
      template_name: ubuntu-2204-cloudinit-template
      template_memory: 2048
  tasks:
    - name: Download cloud image
      ansible.builtin.get_url:
        url: "{{ vm.cloud_image_url }}"
        dest: "{{ vm.cloud_image_path }}"
        mode: 0700

    - name: Create a VM to use as a template
      ansible.builtin.command:
        cmd: "qm create {{ vm.template_id }} --name {{ vm.template_name }} --memory {{ vm.template_memory }} --net0 virtio,bridge=vmbr0"
        creates: "{{ vm.cloud_image_path }}"
      become: true

    - name: Import disk image
      ansible.builtin.command:
        cmd: "qm importdisk {{ vm.template_id }} {{ vm.cloud_image_path }} local"
      register: result
      changed_when: result.changed == true
      become: true

I get the following error at the 'Import Disk' step:

fatal: [proxmox-master]: FAILED! => {"changed": true, "cmd": ["qm", "importdisk", "900", "/tmp/ubuntu-2204-minimal-cloudimg-amd64.qcow2", "local"], "delta": "0:00:00.440777", "end": "2022-11-23 09:39:47.476356", "msg": "non-zero return code", "rc": 2, "start": "2022-11-23 09:39:47.035579", "stderr": "Configuration file 'nodes/pve/qemu-server/900.conf' does not exist", "stderr_lines": ["Configuration file 'nodes/pve/qemu-server/900.conf' does not exist"], "stdout": "", "stdout_lines": []}

I've never created any config when creating VM's using the PVE GUI, and I don't know what config file is being referenced in the error. Is this something I need to produce?


Solution

  • I was able to fix this by changing the "Create a VM...." step, excluding the creates directive and instead including register and changed_when:

    - name: Create a VM to use as a template
          ansible.builtin.command:
            cmd: "qm create {{ vm.template_id }} --name {{ vm.template_name }} --memory {{ vm.template_memory }} --net0 virtio,bridge=vmbr0"
          register: result
          changed_when: result.changed == true
          become: true