Some useful ansible tasks for proxmox management

Posted on Apr 2, 2023

Introduction

Re-erecting my broken proxmox homelab cluster is not an easy task (I will discuss that in the future), maintaining it also takes efforts either.

Here are some tasks that I think that is helpful to maintain a proxmox cluster.

Removing pve-enterprise from sources.list

This is a must if you are like me (who is too poor to obtain an enterprise license)

- name: Copy | Backup /etc/apt/sources.list.d/pve-enterprise.list
   become: true
   copy:
     remote_src: true
     src: /etc/apt/sources.list.d/pve-enterprise.list
     dest: /etc/apt/sources.list.d/pve-enterprise.list.backup
     force: no
 
 - name: LineInFile | Comment out sources.list file
   become: true
   lineinfile:
     path: /etc/apt/sources.list.d/pve-enterprise.list
     backrefs: yes
     regexp: '^((deb|deb-src).*.$)'
     line: '# \1'

Removing “You do not have a valid subscription for this server” popup message

Any Proxmox users may find the message annoying when accessing the proxmox UI.

Even if you remove the message line manually in proxmoxlib.js, as soon as you update your proxmox device, the message will appear again. Below is my hack to remove this message.

 - name: Replace | Comment out No vaild subscription
   become: true
   notify: "restart pveproxy.service"
   replace:
     path: /usr/share/javascript/proxmox-widget-toolkit/proxmoxlib.js
       # regexp: '(Ext\.Msg\.show.*.\n.*.No valid subscription.*,)'
     regexp: '((?<!\/\* Added by ansible-pve \*\/ void\({ \/\/)Ext\.Msg\.show\({\n.*.No valid subscription.*.,)'
     replace: '/* Added by ansible-pve */ void({ //\1'

Creating VM templates on every node

This is very useful when you want to do mass deploy linux VMs in proxmox clusters.

My own practice will be creating VM template with ansible but maintain state/deploying VMs with terraform.

task

- name: Create Ubuntu 22.04 VM Template
  run_once: true
  community.general.proxmox_kvm:
    api_host: "{{ item.host }}"
    api_user: "root@pam"
    api_token_id: ansible-prod
    api_token_secret: <% Your token secret here %>

    node: "{{ item.host }}"
    vmid: "{{ item.vmid }}"
    net         : '{"net0":"virtio,bridge=vmbr0"}'
    vga: serial0
    serial: '{"serial0": "socket"}'
    template: true
  loop:
    - { host: "pve01", vmid: 9001 }
    - { host: "pve02", vmid: 9002 }
    - { host: "pve03", vmid: 9003 }

- name: Create new disk in VM template on pve01 (do not rewrite in case it exists already)
  run_once: true
  community.general.proxmox_disk:
    api_host: "{{ item.host }}"
    api_user: 'root@pam'
    api_password: '<% Your password here %>'
    api_token_id: ansible-prod
    api_token_secret: <% Your token secret here %>

    vmid: "{{ item.vmid }}"
    disk: virtio0
    import_from: "{{ vm_templates.ubuntu[0].path }}"
    storage: local-lvm
    state: present
  loop:
    - { host: "pve01", vmid: 9001 }
    - { host: "pve02", vmid: 9002 }
    - { host: "pve03", vmid: 9003 }

variable

vm_templates:
  ubuntu:
    - name: jammy-server-cloudimg-amd64-disk-kvm
      path: /mnt/pve/osImage/Linux/jammy-server-cloudimg-amd64-disk-kvm.img
  debian:
    - name: debian-11-generic-arm64
      path: /mnt/pve/osImage/Linux/debian-11-generic-amd64-20230124-1270.qcow2