Skip to content

Instantly share code, notes, and snippets.

@mraenker
Forked from jkkor/tasks.yml
Created June 10, 2025 18:50
Show Gist options
  • Save mraenker/c46f6df8dbe8352126709110bf1b4b7b to your computer and use it in GitHub Desktop.
Save mraenker/c46f6df8dbe8352126709110bf1b4b7b to your computer and use it in GitHub Desktop.

Revisions

  1. @corncobble corncobble revised this gist Jun 1, 2023. No changes.
  2. @corncobble corncobble revised this gist Jun 1, 2023. No changes.
  3. @corncobble corncobble created this gist Jun 1, 2023.
    134 changes: 134 additions & 0 deletions tasks.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,134 @@
    ---
    - name: Download cloud-init image
    register: image
    ansible.builtin.get_url:
    url: "{{ image_url }}"
    dest: /tmp
    mode: '0644'
    force: true

    - name: Install pip
    ansible.builtin.apt:
    name:
    - pip
    update_cache: true
    install_recommends: false
    state: present

    - name: Install proxmoxer
    ansible.builtin.pip:
    name: proxmoxer
    state: present

    - name: Create new VM template from cloud-init image
    community.general.proxmox_kvm:
    api_host: "{{ api_host }}"
    api_user: "{{ api_user }}"
    api_password: "{{ api_password }}"
    node: "{{ node_target }}"
    name: "{{ template_name }}"
    bios: ovmf
    boot: 'order=scsi0'
    cores: 1
    sockets: 1
    machine: q35
    memory: 4096
    ostype: "{{ image_ostype }}"
    efidisk0:
    storage: "{{ storage_target }}"
    format: raw
    efitype: 4m
    pre_enrolled_keys: 1
    serial:
    serial0: 'socket'
    vga: serial0
    scsihw: 'virtio-scsi-single'
    scsi:
    scsi0: "{{ storage_target }}:0,iothread=1,discard=on,import-from={{ image.dest }},format=raw"
    ide:
    ide2: "{{ storage_target }}:cloudinit,format=raw"
    net:
    net0: 'virtio,bridge=vmbr0,firewall=1'
    template: true

    - name: Clone {{ item.name }} VM from cloud-init template
    register: vm_result
    community.general.proxmox_kvm:
    api_host: "{{ api_host }}"
    api_user: "{{ api_user }}"
    api_token_id: "{{ api_token_id }}"
    api_token_secret: "{{ api_token_secret }}"
    name: "{{ item.name }}"
    node: "{{ node_target }}"
    clone: "{{ template_name }}"
    storage: "{{ storage_target }}"

    - name: Grow {{ item.name }} VM disk
    community.general.proxmox_disk:
    api_host: "{{ api_host }}"
    api_user: "{{ api_user }}"
    api_token_id: "{{ api_token_id }}"
    api_token_secret: "{{ api_token_secret }}"
    vmid: "{{ vm_result.vmid }}"
    disk: scsi0
    size: "{{ item.size }}"
    state: resized

    - name: Update {{ item.name }} VM configuration
    vars:
    ssh_key_path: "{{ lookup('env', 'HOME') }}/.ssh/id_rsa.pub"
    community.general.proxmox_kvm:
    api_host: "{{ api_host }}"
    api_user: "{{ api_user }}"
    api_token_id: "{{ api_token_id }}"
    api_token_secret: "{{ api_token_secret }}"
    node: "{{ node_target }}"
    vmid: "{{ vm_result.vmid }}"
    agent: 1
    cores: "{{ item.cores }}"
    memory: "{{ item.memory }}"
    ciuser: "{{ ci_user }}"
    cipassword: "{{ ci_password }}"
    ipconfig:
    ipconfig0: 'ip=dhcp'
    sshkeys: "{{ lookup('ansible.builtin.file', '{{ ssh_key_path }}') }}"
    update: true

    - name: Start {{ item.name }} VM
    community.general.proxmox_kvm:
    api_host: "{{ api_host }}"
    api_user: "{{ api_user }}"
    api_token_id: "{{ api_token_id }}"
    api_token_secret: "{{ api_token_secret }}"
    node: "{{ node_target }}"
    vmid: "{{ vm_result.vmid }}"
    state: started

    - name: Prompt for {{ item.name }} VM host
    register: target_host_result
    ansible.builtin.pause:
    prompt: "Enter host/IP for {{ item.name }} VM"

    - name: Add {{ item.name }} VM host
    ansible.builtin.add_host:
    name: "{{ target_host_result.user_input }}"
    groups: "{{ item.name }}"
    ansible_user: "{{ ci_user }}"

    - name: Install qemu guest agent on {{ item.name }} VM
    delegate_to: "{{ target_host_result.user_input }}"
    delegate_facts: true
    become: yes
    ansible.builtin.package:
    name: qemu-guest-agent
    state: present
    update_cache: true
    install_recommends: false

    - name: Restart qemu guest agent on {{ item.name }} VM
    delegate_to: "{{ target_host_result.user_input }}"
    delegate_facts: true
    become: yes
    ansible.builtin.service:
    name: qemu-guest-agent
    state: restarted
    36 changes: 36 additions & 0 deletions variables.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    ---

    # cloud-init image configuration
    image_url: "https://cloud.debian.org/images/cloud/sid/daily/latest/debian-sid-generic-amd64-daily.qcow2"
    image_ostype: l26

    # Proxmox API variables
    api_host: "{{ ansible_host }}"
    api_user: root@pam
    api_password: "{{ ansible_password }}"
    api_token_id: ansible
    api_token_secret: 3520be4c-0f7a-472f-8944-089076dfdf84

    # Proxmox node target
    node_target: proxmox

    # VM storage target
    storage_target: local-lvm

    # VM template name
    template_name: debian-sid-daily

    # cloud-init configuration
    ci_user:
    ci_password:

    # VM configuration (looped over)
    machines:
    - name: test1
    size: 16G
    cores: 1
    memory: 8192
    - name: test2
    size: 32G
    cores: 2
    memory: 16384