Skip to content

Instantly share code, notes, and snippets.

@colbylol
Last active January 9, 2024 13:03
Show Gist options
  • Save colbylol/78a4dec09043f450bb1c8b8d8cdc5e19 to your computer and use it in GitHub Desktop.
Save colbylol/78a4dec09043f450bb1c8b8d8cdc5e19 to your computer and use it in GitHub Desktop.
Proxmox: Create VM from cloud-init image using Ansible
---
- 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
---
# 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
@Gurdt55lol
Copy link

I made a role version based of this and it worked like a charm so thank you (repo: )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment