regenerate cloud init image using ansible

dasblabla

New Member
Oct 14, 2020
5
0
1
24
Hi, I want to deploy from a Proxmox Host a VM that is convertet to a template to clone other VMs out of these. the Templating role works fine but if i clone the template with ansible, starting the VM and want to sign in the preset login combi don´t work, but if i click on the "Regenerate Image" Button manually in gui and than start the VM the right login combi is used. Is there something I have to add to my ansible role to regenerate the image or is there a preset to do in some config file in proxmox?

This is the template generator:

YAML:
---
  - name: download cloud image
    get_url:
      url: https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img
      dest: /home/user/focal-server-cloudimg-amd64.img

  - name: Create empty VM using Cloud-Init
    community.general.proxmox_kvm:
      node: pve
      api_user: root@pam
      api_password: "{{ lookup('keepass', 'pveroot', 'password') }}"
      api_host: pve
      name: testvm-template
      scsihw: virtio-scsi-pci
      scsi:
       scsi0: 'local-lvm:1,format=raw'
      ide:
        ide2: 'local:cloudinit,format=qcow2'
      bootdisk: scsi0
      ciuser: user
      cipassword: hallowelt
      net:
        net0: 'virtio,bridge=vmbr0'
      ipconfig:
        ipconfig0: 'ip=192.168.56.230/24'
      proxmox_default_behavior: compatibility

  - name: import init disk
    ansible.builtin.command:
      cmd: "qm importdisk 100 /home/user/focal-server-cloudimg-amd64.img local-lvm"
      creates: "/dev/mapper/pve-vm--100--disk--1"


  - name: attache base image disk
    ansible.builtin.lineinfile:
      path: /etc/pve/local/qemu-server/100.conf
      regexp: '^scsi0:.*'
      line: 'scsi0: local-lvm:vm-100-disk-1'

  - name: make vm to template
    community.general.proxmox_kvm:
      node: pve
      api_user: root@pam
      api_password: "{{ lookup('keepass', 'pveroot', 'password') }}"
      api_host: pve
      vmid: 100
      name: testvm-template
      scsihw: virtio-scsi-pci
      ide:
        ide2: 'local:cloudinit,format=qcow2'
      bootdisk: scsi0
      ciuser: user
      cipassword: hallowelt
      proxmox_default_behavior: compatibility
      template: yes
      update: yes


and here are the clone role:
YAML:
---
  - name: Clone VM
    community.general.proxmox_kvm:
      api_user: root@pam
      api_password: "{{ lookup('keepass', 'pveroot', 'password') }}"
      api_host: pve
      clone: testvm-template
      name: testvm-clone2
      node: pve
      storage: local-lvm
      format: raw
      timeout: 500
      net:
        net0: "virtio,bridge=vmbr0"
      ipconfig:
        ipconfig0: "ip=192.168.56.201/24"
      ciuser: user
      cipassword: hallowelt
 
Hi @dasblabla,

did you find a solution yet? Im also interested in this topic. Re-generating the cloud-init template for a Windows VM is currently the only way for me to get the Windows VM deployment via Ansible running. But I still did not find any solution to automatically re-generating the cloud-init template for Windows VMs.
 
We analyzed the Proxmox API call which is called when you click on "VM --> Cloud-Init --> Regenerate Template". The call consists of two steps:
  1. Remove cloud-init drive from VM
  2. Attach cloud-init drive to VM
So the according Ansilbe code is the following:
YAML:
- name: "Regenerate cloud-init image - Step 1"
  proxmox_api:
    endpoint: "/nodes/{{ proxmox_node }}/qemu/{{ virtual_machine.vm_id }}/config"
    method: post
    host: "{{ proxmox_api_host}}"
    user: "{{ proxmox_api_user }}"
    password: "{{ proxmox_api_password }}"
    parameters:
      ide0: 'none,media=cdrom'
    verify_ssl: false
  delegate_to: "127.0.0.1"
  when: "virtual_machine.os_type == 'win10'"
  tags: "regenerate_image"

- name: "Regenerate cloud-init image - Step 2"
  proxmox_api:
    endpoint: "/nodes/{{ proxmox_node }}/qemu/{{ virtual_machine.vm_id }}/config"
    method: post
    host: "{{ proxmox_api_host}}"
    user: "{{ proxmox_api_user }}"
    password: "{{ proxmox_api_password }}"
    parameters:
      ide0: '{{ proxmox_cloud_init_default_storage }}:cloudinit'
    verify_ssl: false
  delegate_to: "127.0.0.1"
  when: "virtual_machine.os_type == 'win10'"
  tags: "regenerate_image"

Therefore I used the following Ansible plugin: https://github.com/robinelfrink/ansible-proxmox-api
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!