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 }}"
- # 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 }}"
- # Destroy the image if it exists
- - name: "Get image config"
- register: config
- ansible.builtin.command: "qm config {{ item.id }}"
- ignore_errors: true
- with_items: "{{ images }}"
- - name: "Destroy image"
- ansible.builtin.command: "qm destroy {{ item.item.id }} --destroy-unreferenced-disks 1 --skiplock 1"
- when: item.rc == 0 # && download.changed
- with_items: "{{ config.results }}"
- - name: "Create image"
- # when: download.changed
- ansible.builtin.command:
- argv:
- - "qm"
- - "create"
- - "{{ item.id }}"
- - "--name"
- - "{{ item.name }}"
- - "--tags"
- - "{{ item.tags }}"
- - "--memory"
- - "4096"
- - "--cores"
- - "2"
- - "--net0"
- - "virtio,bridge=vmbr1"
- - "--scsihw"
- - "virtio-scsi-pci"
- - "--ciuser"
- - "{{ cloud_init_user }}"
- - "--cipassword"
- - "{{ cloud_init_password }}"
- - "--searchdomain"
- - "{{ cloud_init_domain }}"
- - "--sshkeys"
- - "{{ cloud_init_ssh_keys }}"
- - "--ipconfig0"
- - "ip=dhcp"
- - "--ostype"
- - "l26"
- - "--agent"
- - "1"
- with_items: "{{ images }}"
- - name: "Customize disk image"
- # when: download.changed
- ansible.builtin.command: "virt-customize -a {{ item.image }} --install qemu-guest-agent"
- with_items: "{{ images }}"
- - name: "Add disk image"
- # when: download.changed
- ansible.builtin.command: "qm set {{ item.id }} --scsi0 vmdata:0,import-from={{ item.file }} --boot order=scsi0 --ide2 vmdata:cloudinit"
- with_items: "{{ images }}"
- - name: "Resize disk image"
- # when: download.changed
- ansible.builtin.command: "qm resize {{ item.id }} scsi0 8G"
- with_items: "{{ images }}"
- - 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",
- 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",
- url: "https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-amd64.qcow2",
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement