Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---
- # Create Cloud-Init VM templates
- # https://pve.proxmox.com/wiki/Cloud-Init_Support
- # Install libguestfs
- # https://docs.ansible.com/ansible/latest/modules/apt_module.html
- - name: "Install libguestfs"
- apt:
- name: ["libguestfs-tools"]
- state: latest
- update_cache: yes
- cache_valid_time: 3600
- # https://docs.ansible.com/ansible/latest/modules/file_module.html
- - name: "Create Cloud Init directory"
- file:
- path: "{{ item }}"
- state: directory
- mode: "ugo+rwx"
- owner: nobody
- group: users
- recurse: true
- with_items:
- - "{{ cloud_init_dir }}"
- # Fix locked images: sudo qm destroy 9001 --destroy-unreferenced-disks 1 --skiplock 1
- # https://docs.ansible.com/ansible/latest/collections/ansible/builtin/command_module.html
- - name: "Rescan images"
- ansible.builtin.command: "qm rescan"
- # https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_blocks.html
- - name: "Create Cloud-Init VM templates"
- block:
- # Download cloud images
- # https://docs.ansible.com/ansible/latest/collections/ansible/builtin/get_url_module.html
- - name: "Download cloud images"
- # Re-create the images when the downloaded image changed
- register: download
- get_url:
- url: "{{ item.url }}"
- dest: "{{ item.file }}"
- mode: "ugo+rwx"
- owner: nobody
- group: users
- timeout: 120
- with_items: "{{ images }}"
- # Copy cloud image to disk file, make modifications to copy not original download
- - name: "Copy cloud image"
- # when: download.changed
- ansible.builtin.copy:
- src: "{{ item.file }}"
- dest: "{{ item.disk }}"
- mode: "ugo+rwx"
- owner: nobody
- group: users
- with_items: "{{ images }}"
- # TODO: Use snippets: https://forum.proxmox.com/threads/combining-custom-cloud-init-with-auto-generated.59008/page-3
- # Install guest agent in disk image
- - name: "Install guest agent in image"
- # when: download.changed
- ansible.builtin.command: "virt-customize -a {{ item.disk }} --install qemu-guest-agent"
- with_items: "{{ images }}"
- # Destroy the VM
- # https://docs.ansible.com/ansible/latest/collections/community/general/proxmox_kvm_module.html
- - name: "Destroy VM"
- # when: download.changed
- community.general.proxmox_kvm:
- api_user: "{{ proxmox_api_user }}"
- api_token_id: "{{ proxmox_api_token }}"
- api_token_secret: "{{ proxmox_api_secret }}"
- api_host: "{{ inventory_hostname }}"
- node: "{{ inventory_hostname_short }}"
- vmid: "{{ item.id }}" # TODO: Docs show using name, we use Id for everything?
- state: "absent"
- with_items: "{{ images }}"
- - name: "Create VM"
- # when: download.changed
- community.general.proxmox_kvm:
- api_user: "{{ proxmox_api_user }}"
- api_token_id: "{{ proxmox_api_token }}"
- api_token_secret: "{{ proxmox_api_secret }}"
- api_host: "{{ ansible_host }}"
- node: "server-1" # TODO How to get non-localhost hostname? "{{ inventory_hostname }}"
- vmid: "{{ item.id }}"
- name: "{{ item.name }}"
- tags: "{{ item.tags }}"
- memory: 4096
- cores: 2
- net:
- net0: "virtio,bridge=vmbr1"
- ipconfig:
- ipconfig0: "ip=dhcp"
- scsihw: "virtio-scsi-pci"
- # "msg": "creation of qemu VM debian-bookworm-template with vmid 9002 failed with exception=500 Internal Server Error: Only root can pass arbitrary filesystem paths. at /usr/share/perl5/PVE/Storage.pm line 542.",
- #scsi:
- # scsi0: "vmdata:0,import-from={{ item.disk }}"
- ide:
- ide2: "vmdata:cloudinit"
- boot: "order=scsi0"
- ciuser: "{{ cloud_init_user }}"
- cipassword: "{{ cloud_init_password }}"
- searchdomains: "{{ cloud_init_domain }}"
- sshkeys: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKhUZslPHARlFH0rpVheirXfSv/H0VL5EoBHsItszT2R piete@Pieter-Desktop" # TODO: Read from authorized_keys file
- ostype: "l26"
- agent: "enabled=1"
- onboot: false
- state: "present"
- with_items: "{{ images }}"
- - name: "Add disk image"
- # when: download.changed
- ansible.builtin.command: "qm set {{ item.id }} --scsi0 vmdata:0,import-from={{ item.disk }}"
- with_items: "{{ images }}"
- - name: "Resize disk image"
- # when: download.changed
- ansible.builtin.command: "qm resize {{ item.id }} scsi0 8G"
- with_items: "{{ images }}"
- # TODO: Try a rescan to see if template error 25 goes away?
- - name: "Rescan images"
- ansible.builtin.command: "qm rescan"
- - name: "Convert to template"
- # when: download.changed
- ansible.builtin.command: "qm template {{ item.id }}"
- with_items: "{{ images }}"
- vars:
- images:
- - {
- id: "9001",
- name: "ubuntu-jammy-template",
- tags: [ "ubuntu", "jammy", "cloud-image" ],
- file: "{{ cloud_init_dir }}/jammy-server-cloudimg-amd64.img",
- disk: "{{ cloud_init_dir }}/jammy-server-cloudimg-amd64-disk.img",
- url: "https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img",
- }
- - {
- id: "9002",
- name: "debian-bookworm-template",
- tags: "debian,bookworm,cloud-image",
- file: "{{ cloud_init_dir }}/debian-12-genericcloud-amd64.qcow2",
- disk: "{{ cloud_init_dir }}/debian-12-genericcloud-amd64-disk.qcow2",
- url: "https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-amd64.qcow2",
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement