Has anyone managed to set tags on a VM with Ansible?

Apr 30, 2025
8
0
1
I cannot get tags on a VM created with Ansible, no matter how I try it. All I can do is set the tag manually after the VM has been created and started.

The template I use is created in a separate stage, also with Ansible. I could of course try to set tags on the template, but I don't want to because the tag is supposed to reflect the VM role, and also be used to get Ansible dynamic inventory from Proxmox.

But here is a short snippet I wrote just to re-create the issue and to show what I've tried to set the tags. I have also tried setting them as a string, which produced no errors, but also no tags.

YAML:
---
- name: Proxmox VM Tagging Demo
  hosts: localhost
  gather_facts: false

  vars:
    # VM Configuration
    proxmox_pool_name: "nfs-servers"
    proxmox_node: dev-vmm01
    proxmox_storage: vmpool01
    proxmox_disk_size: 50G
    proxmox_vm_name: "nfs-server-demo"
    proxmox_template_name: debian-12-template
    proxmox_bridge: vmbr0
    proxmox_machine: q35
    proxmox_ostype: l26
    proxmox_tags:
      - nfs

  tasks:
    - name: Ensure resource pool exists
      community.proxmox.proxmox_pool:
        poolid: "{{ proxmox_pool_name }}"
        state: present
        comment: "Ansible managed"
        api_user: "{{ lookup('env', 'PROXMOX_USER') }}"
        api_token_id: "{{ lookup('env', 'PROXMOX_TOKEN_ID') }}"
        api_token_secret: "{{ lookup('env', 'PROXMOX_TOKEN_SECRET') }}"
        api_host: "{{ lookup('env', 'PROXMOX_HOST') }}"
        validate_certs: true

    - name: Create VM from template
      community.proxmox.proxmox_kvm:
        state: present
        api_user: "{{ lookup('env', 'PROXMOX_USER') }}"
        api_token_id: "{{ lookup('env', 'PROXMOX_TOKEN_ID') }}"
        api_token_secret: "{{ lookup('env', 'PROXMOX_TOKEN_SECRET') }}"
        api_host: "{{ lookup('env', 'PROXMOX_HOST') }}"
        validate_certs: true

        node: "{{ proxmox_node }}"
        pool: "{{ proxmox_pool_name }}"
        name: "{{ proxmox_vm_name }}"
        machine: "{{ proxmox_machine }}"
        clone: "{{ proxmox_template_name }}"
        full: true
        ostype: "{{ proxmox_ostype }}"

        scsi:
          scsi0: "{{ proxmox_storage }}:50"
          scsi1: "{{ proxmox_storage }}:{{ proxmox_disk_size }}"
        net:
          net0: "virtio,bridge={{ proxmox_bridge }}"
        tags: "{{ proxmox_tags }}"

    - name: Start VM and attempt to apply tags
      community.proxmox.proxmox_kvm:
        state: started
        api_user: "{{ lookup('env', 'PROXMOX_USER') }}"
        api_token_id: "{{ lookup('env', 'PROXMOX_TOKEN_ID') }}"
        api_token_secret: "{{ lookup('env', 'PROXMOX_TOKEN_SECRET') }}"
        api_host: "{{ lookup('env', 'PROXMOX_HOST') }}"
        validate_certs: true

        node: "{{ proxmox_node }}"
        name: "{{ proxmox_vm_name }}"
        tags: "{{ proxmox_tags }}"
        update: true
      register: start_vm_result
      until: start_vm_result is not failed
      retries: 10
      delay: 15

    - name: Get VM info to verify tags
      community.proxmox.proxmox_vm_info:
        api_user: "{{ lookup('env', 'PROXMOX_USER') }}"
        api_token_id: "{{ lookup('env', 'PROXMOX_TOKEN_ID') }}"
        api_token_secret: "{{ lookup('env', 'PROXMOX_TOKEN_SECRET') }}"
        api_host: "{{ lookup('env', 'PROXMOX_HOST') }}"
        validate_certs: true

        node: "{{ proxmox_node }}"
        name: "{{ proxmox_vm_name }}"
      register: vm_info
      until: vm_info is not failed
      retries: 10
      delay: 10

    - name: Display VM info (check for tags here)
      debug:
        var: vm_info

The VM is created, IP is set, cloud-init runs and everything but I can't get the tags to work. If I can't get tags to work then I'll have to use the proxmox_pool_nfs_servers group from dynamic inventory instead.