Advertisement
ptr727

Untitled

Aug 3rd, 2023
860
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 5.59 KB | None | 0 0
  1. ---
  2. # Create Cloud-Init VM templates
  3. # https://pve.proxmox.com/wiki/Cloud-Init_Support
  4.  
  5. # Install libguestfs
  6. # https://docs.ansible.com/ansible/latest/modules/apt_module.html
  7. - name: "Install libguestfs"
  8.   apt:
  9.     name: ["libguestfs-tools"]
  10.     state: latest
  11.     update_cache: yes
  12.     cache_valid_time: 3600
  13.  
  14. # https://docs.ansible.com/ansible/latest/modules/file_module.html
  15. - name: "Create Cloud Init directory"
  16.   file:
  17.     path: "{{ item }}"
  18.     state: directory
  19.     mode: "ugo+rwx"
  20.     owner: nobody
  21.     group: users
  22.     recurse: true
  23.   with_items:
  24.    - "{{ cloud_init_dir }}"
  25.  
  26. # Fix locked images: sudo qm destroy 9001 --destroy-unreferenced-disks 1 --skiplock 1
  27. # https://docs.ansible.com/ansible/latest/collections/ansible/builtin/command_module.html
  28. - name: "Rescan images"
  29.   ansible.builtin.command: "qm rescan"
  30.  
  31. # https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_blocks.html
  32. - name: "Create Cloud-Init VM templates"
  33.   block:
  34.    # Download cloud images
  35.     # https://docs.ansible.com/ansible/latest/collections/ansible/builtin/get_url_module.html
  36.     - name: "Download cloud images"
  37.       # Re-create the images when the downloaded image changed
  38.       register: download
  39.       get_url:
  40.         url: "{{ item.url }}"
  41.         dest: "{{ item.file }}"
  42.         mode: "ugo+rwx"
  43.         owner: nobody
  44.         group: users
  45.         timeout: 120
  46.       with_items: "{{ images }}"
  47.  
  48.     # Copy cloud image to disk file, make modifications to copy not original download
  49.     - name: "Copy cloud image"
  50.       # when: download.changed
  51.       ansible.builtin.copy:
  52.         src: "{{ item.file }}"
  53.         dest: "{{ item.disk }}"
  54.         mode: "ugo+rwx"
  55.         owner: nobody
  56.         group: users
  57.       with_items: "{{ images }}"
  58.  
  59.     # TODO: Use snippets: https://forum.proxmox.com/threads/combining-custom-cloud-init-with-auto-generated.59008/page-3
  60.     # Install guest agent in disk image
  61.     - name: "Install guest agent in image"
  62.       # when: download.changed
  63.       ansible.builtin.command: "virt-customize -a {{ item.disk }} --install qemu-guest-agent"
  64.       with_items: "{{ images }}"
  65.  
  66.     # Destroy the VM
  67.     # https://docs.ansible.com/ansible/latest/collections/community/general/proxmox_kvm_module.html
  68.     - name: "Destroy VM"
  69.       # when: download.changed
  70.       community.general.proxmox_kvm:
  71.         api_user: "{{ proxmox_api_user }}"
  72.         api_token_id: "{{ proxmox_api_token }}"
  73.         api_token_secret: "{{ proxmox_api_secret }}"
  74.         api_host: "{{ inventory_hostname }}"
  75.         node: "{{ inventory_hostname_short }}"
  76.         vmid: "{{ item.id }}" # TODO: Docs show using name, we use Id for everything?
  77.         state: "absent"
  78.       with_items: "{{ images }}"
  79.  
  80.     - name: "Create VM"
  81.       # when: download.changed
  82.       community.general.proxmox_kvm:
  83.         api_user: "{{ proxmox_api_user }}"
  84.         api_token_id: "{{ proxmox_api_token }}"
  85.         api_token_secret: "{{ proxmox_api_secret }}"
  86.         api_host: "{{ ansible_host }}"
  87.         node: "server-1" # TODO How to get non-localhost hostname? "{{ inventory_hostname }}"
  88.         vmid: "{{ item.id }}"
  89.         name: "{{ item.name }}"
  90.         tags: "{{ item.tags }}"
  91.         memory: 4096
  92.         cores: 2
  93.         net:
  94.           net0: "virtio,bridge=vmbr1"
  95.         ipconfig:
  96.           ipconfig0: "ip=dhcp"
  97.         scsihw: "virtio-scsi-pci"
  98.         # "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.",
  99.         #scsi:
  100.         #  scsi0: "vmdata:0,import-from={{ item.disk }}"
  101.         ide:
  102.           ide2: "vmdata:cloudinit"
  103.         boot: "order=scsi0"
  104.         ciuser: "{{ cloud_init_user }}"
  105.         cipassword: "{{ cloud_init_password }}"
  106.         searchdomains: "{{ cloud_init_domain }}"
  107.         sshkeys: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKhUZslPHARlFH0rpVheirXfSv/H0VL5EoBHsItszT2R piete@Pieter-Desktop" # TODO: Read from authorized_keys file
  108.         ostype: "l26"
  109.         agent: "enabled=1"
  110.         onboot: false
  111.         state: "present"
  112.       with_items: "{{ images }}"
  113.  
  114.     - name: "Add disk image"
  115.       # when: download.changed
  116.       ansible.builtin.command: "qm set {{ item.id }} --scsi0 vmdata:0,import-from={{ item.disk }}"
  117.       with_items: "{{ images }}"
  118.  
  119.     - name: "Resize disk image"
  120.       # when: download.changed
  121.       ansible.builtin.command: "qm resize {{ item.id }} scsi0 8G"
  122.       with_items: "{{ images }}"
  123.  
  124.     # TODO: Try a rescan to see if template error 25 goes away?
  125.     - name: "Rescan images"
  126.       ansible.builtin.command: "qm rescan"
  127.  
  128.     - name: "Convert to template"
  129.       # when: download.changed
  130.       ansible.builtin.command: "qm template {{ item.id }}"
  131.       with_items: "{{ images }}"
  132.  
  133.   vars:
  134.     images:
  135.      - {
  136.           id: "9001",
  137.           name: "ubuntu-jammy-template",
  138.           tags: [ "ubuntu", "jammy", "cloud-image" ],
  139.           file: "{{ cloud_init_dir }}/jammy-server-cloudimg-amd64.img",
  140.           disk: "{{ cloud_init_dir }}/jammy-server-cloudimg-amd64-disk.img",
  141.           url: "https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img",
  142.         }
  143.       - {
  144.           id: "9002",
  145.           name: "debian-bookworm-template",
  146.           tags: "debian,bookworm,cloud-image",
  147.           file: "{{ cloud_init_dir }}/debian-12-genericcloud-amd64.qcow2",
  148.           disk: "{{ cloud_init_dir }}/debian-12-genericcloud-amd64-disk.qcow2",
  149.           url: "https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-amd64.qcow2",
  150.         }
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement