How to use vm guest_name for a win_disks module?

Hi,
I would like to read vm hostname from a VM Folder and use it to estract the list of windows disks.

I tried with the following code, but it doesn’t work.
I obtain:

TASK [debug] **********************************************************************************************************************************************************************
ok: [localhost] => {
    "hostnames": [
        "hostabc"
    ]
}

TASK [List disks with drive letters] **********************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "/bin/sh: 1: powershell: not found\n", "module_stdout": "", "msg": "The module failed to execute correctly, you probably need to set the interpreter.\nSee stdout/stderr for the exact error", "rc": 127}

This is my playbook:

- name: vm read on folder VMWARE
  hosts: localhost
  become: false
  gather_facts: true
  vars_files:
    - secrets.yml
    - vars.yml
  collections:
    - community.vmware
    - community.windows
  tasks:
    - name: Gather VM info
      community.vmware.vmware_vm_info:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        folder: "/{{ vcenter_datacenter }}/vm/Ansible"
      register: vm_info

    - set_fact:
        hostnames: "{{ vm_info.virtual_machines | json_query(query) | map(attribute='guest_name') | list }}"
      vars:
        query: "[?guest_name!=null]"

    - debug: var=hostnames

    - name: List disks with drive letters
      community.windows.win_disk_facts:
        delegate_to: "{{ hostnames }}"
        filter:
          - partitions

How could I correctly give the vms to the win_disk_facts?

Thanks a lot.

Mario

You need to loop over the hostnames array:

- name: List disks with drive letters
  community.windows.win_disk_facts:
    filter:
      - partitions
  delegate_to: "{{ item }}"
  loop: "{{ hostnames }}"

Note that the VM guest names should match the names in your inventory.

1 Like

no, I haven’t the same name in the first inventory file.

Btw, I solved creating on fly an inventory file.

The playbook reading the vm folder, create an inventory file that it is used by a secondary playbook using it.
this is the piece of code:

- name: Create file if it does not exist
  file:
    path: /ansible/inventoryonfly.ini
    state: touch


- name: Write variable to file
  copy:
    content: |
      [hostgrouponthefly]
      {{ hostnames }}
    dest: /ansible/inventoryonfly.ini

So, later, another playbook is launched with the inventory file: /ansible/inventoryonfly.ini