# How to use vm guest\_name for a win\_disks module?

**URL:** <https://forum.ansible.com/t/how-to-use-vm-guest-name-for-a-win-disks-module/10320>\
**Category:** Get Help\
**Tags:** winrm, windows, vmware\
**Created:** [October 16, 2024, 7:11am UTC](https://forum.ansible.com/t/how-to-use-vm-guest-name-for-a-win-disks-module/10320 "2024-10-16T07:11:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ansibliano](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@ansibliano](https://forum.ansible.com/u/ansibliano)\
**Post date:** [October 16, 2024, 7:11am UTC](https://forum.ansible.com/t/how-to-use-vm-guest-name-for-a-win-disks-module/10320/1 "2024-10-16T07:11:53Z")

</div>

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:

```auto
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:

```auto
- 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

---

<div class="post-metadata">

**Author:** ![ildjarn](https://avatars.discourse-cdn.com/v4/letter/i/8491ac/32.png) [@ildjarn](https://forum.ansible.com/u/ildjarn)\
**Post date:** [October 16, 2024, 12:08pm UTC](https://forum.ansible.com/t/how-to-use-vm-guest-name-for-a-win-disks-module/10320/2 "2024-10-16T12:08:17Z")

</div>

You need to loop over the hostnames array:

```yaml
- 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.

---

<div class="post-metadata">

**Author:** ![ansibliano](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@ansibliano](https://forum.ansible.com/u/ansibliano)\
**Post date:** [October 17, 2024, 7:46am UTC](https://forum.ansible.com/t/how-to-use-vm-guest-name-for-a-win-disks-module/10320/3 "2024-10-17T07:46:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 16, 2024, 7:47am UTC](https://forum.ansible.com/t/how-to-use-vm-guest-name-for-a-win-disks-module/10320/4 "2024-11-16T07:47:22Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
