Advertisement
ptr727

Untitled

Aug 2nd, 2023
1,245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 4.04 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.   # https://docs.ansible.com/ansible/latest/collections/ansible/builtin/command_module.html
  27. - name: "Rescan images"
  28.   ansible.builtin.command: "qm rescan"
  29.  
  30. # https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_blocks.html
  31. - name: "Create Cloud-Init VM templates"
  32.   block:
  33.    # Download cloud images
  34.     # https://docs.ansible.com/ansible/latest/collections/ansible/builtin/get_url_module.html
  35.     - name: "Download cloud images"
  36.       # Re-create the images when the downloaded image changed
  37.       register: download
  38.       get_url:
  39.         url: "{{ item.url }}"
  40.         dest: "{{ item.file }}"
  41.         mode: "ugo+rwx"
  42.         owner: nobody
  43.         group: users
  44.         timeout: 120
  45.       with_items: "{{ images }}"
  46.  
  47.     # Destroy the image if it exists
  48.     - name: "Get image config"
  49.       register: config
  50.       ansible.builtin.command: "qm config {{ item.id }}"
  51.       ignore_errors: true
  52.       with_items: "{{ images }}"
  53.     - name: "Destroy image"
  54.       ansible.builtin.command: "qm destroy {{ item.item.id }} --destroy-unreferenced-disks 1 --skiplock 1"
  55.       when: item.rc == 0 # && download.changed
  56.       with_items: "{{ config.results }}"
  57.  
  58.     - name: "Create image"
  59.       # when: download.changed
  60.       ansible.builtin.command:
  61.         argv:
  62.          - "qm"
  63.           - "create"
  64.           - "{{ item.id }}"
  65.           - "--name"
  66.           - "{{ item.name }}"
  67.           - "--tags"
  68.           - "{{ item.tags }}"
  69.           - "--memory"
  70.           - "4096"
  71.           - "--cores"
  72.           - "2"
  73.           - "--net0"
  74.           - "virtio,bridge=vmbr1"
  75.           - "--scsihw"
  76.           - "virtio-scsi-pci"
  77.           - "--ciuser"
  78.           - "{{ cloud_init_user }}"
  79.           - "--cipassword"
  80.           - "{{ cloud_init_password }}"
  81.           - "--searchdomain"
  82.           - "{{ cloud_init_domain }}"
  83.           - "--sshkeys"
  84.           - "{{ cloud_init_ssh_keys }}"
  85.           - "--ipconfig0"
  86.           - "ip=dhcp"
  87.           - "--ostype"
  88.           - "l26"
  89.           - "--agent"
  90.           - "1"
  91.       with_items: "{{ images }}"
  92.  
  93.     - name: "Customize disk image"
  94.       # when: download.changed
  95.       ansible.builtin.command: "virt-customize -a {{ item.image }} --install qemu-guest-agent"
  96.       with_items: "{{ images }}"
  97.  
  98.     - name: "Add disk image"
  99.       # when: download.changed
  100.       ansible.builtin.command: "qm set {{ item.id }} --scsi0 vmdata:0,import-from={{ item.file }} --boot order=scsi0 --ide2 vmdata:cloudinit"
  101.       with_items: "{{ images }}"
  102.  
  103.     - name: "Resize disk image"
  104.       # when: download.changed
  105.       ansible.builtin.command: "qm resize {{ item.id }} scsi0 8G"
  106.       with_items: "{{ images }}"
  107.  
  108.     - name: "Convert to template"
  109.       # when: download.changed
  110.       ansible.builtin.command: "qm template {{ item.id }}"
  111.       with_items: "{{ images }}"
  112.  
  113.   vars:
  114.     images:
  115.      - {
  116.           id: "9001",
  117.           name: "ubuntu-jammy-template",
  118.           tags: "ubuntu,jammy,cloud-image",
  119.           file: "{{ cloud_init_dir }}/jammy-server-cloudimg-amd64.img",
  120.           url: "https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img",
  121.         }
  122.       - {
  123.           id: "9002",
  124.           name: "debian-bookworm-template",
  125.           tags: "debian,bookworm,cloud-image",
  126.           file: "{{ cloud_init_dir }}/debian-12-genericcloud-amd64.qcow2",
  127.           url: "https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-genericcloud-amd64.qcow2",
  128.         }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement