--- # Validation first, under always, so a bad input fails before any change whatever tags the run # selects. - name: Validate the role inputs ansible.builtin.import_tasks: validate.yml tags: always # Every Proxmox API call of the role is inside this block, so the credentials are set once here # instead of on each task; module_defaults set on a block reach tasks run through include_tasks. # They are templated per task and no secret is stored as a fact. The uri call (detect_disk) takes # host and header explicitly. - name: Manage Proxmox VM lifecycle module_defaults: community.proxmox.proxmox_kvm: api_user: "{{ pve_vm_api_user }}@{{ pve_vm_api_realm }}" api_token_id: "{{ pve_vm_api_token_id }}" api_token_secret: "{{ pve_vm_api_token_secret }}" api_host: "{{ pve_vm_api_host }}" validate_certs: false community.proxmox.proxmox_disk: api_user: "{{ pve_vm_api_user }}@{{ pve_vm_api_realm }}" api_token_id: "{{ pve_vm_api_token_id }}" api_token_secret: "{{ pve_vm_api_token_secret }}" api_host: "{{ pve_vm_api_host }}" validate_certs: false ansible.builtin.uri: validate_certs: false block: - name: Gather current VM information ansible.builtin.include_tasks: detect.yml - name: Remove VM if destruction is requested ansible.builtin.include_tasks: delete.yml when: - pve_vm_state == "absent" - pve_vm_allow_destroy | bool - pve_vm_exists - name: Stop processing for deleted VM ansible.builtin.meta: end_host when: - pve_vm_state == "absent" - pve_vm_allow_destroy | bool - name: Create VM if it does not exist ansible.builtin.include_tasks: create.yml when: - pve_vm_state != "absent" - not pve_vm_exists - not ansible_check_mode - name: Preview VM creation ansible.builtin.debug: msg: >- VM {{ pve_vm_vmid }} does not exist on {{ pve_vm_node }}: a real run clones it from template {{ pve_vm_clone_vmid | default('(unset)') }} and then configures it. Nothing after the lookup runs for it in this check. changed_when: true when: - pve_vm_state != "absent" - not pve_vm_exists - ansible_check_mode - name: Refresh VM information after changes ansible.builtin.include_tasks: detect.yml - name: Gather current VM disk information ansible.builtin.include_tasks: detect_disk.yml when: - pve_vm_exists - pve_vm_state != "absent" - name: Update existing VM configuration ansible.builtin.include_tasks: update.yml when: - pve_vm_state != "absent" - pve_vm_exists - name: Ensure VM power state ansible.builtin.include_tasks: state.yml when: - pve_vm_exists - pve_vm_state in ["started", "stopped"] - name: Wait for VM to become ready ansible.builtin.include_tasks: wait.yml when: - pve_vm_exists - pve_vm_state == "started" - pve_vm_wait_for_guest | bool