Ansible find files in drives found

29 Views Asked by At

Using ansible playbook, I want to find all drives found in windows and then look for a specific files in the whole drives directory and delete if found

 - name: Get disk facts
    #community.windows.win_disk_facts:
    ansible.windows.win_shell: "[System.IO.DriveInfo]::GetDrives() | Where-Object { $_.DriveType -     eq 'Fixed' -and $_.DriveType -ne '2' } | Select-Object -ExpandProperty Name"
    register: disk_info

  - name: Display disk information
    debug:
      msg: "Found drives: {{ disk_info.stdout_lines }}"

  - name: Find files
    ansible.windows.win_find:
      paths: {{ item }}
      patterns: ['*Ansible']
      recurse: yes
    loop: "{{ disk_info.stdout_lines }}"
    register: found_files

  - name: Display found files
    debug:
      msg: "{{ found_files.results | map(attribute='stdout_lines') | list }}"
    when: 
      - found_files is defined 
      - found_files.results | length > 0

  - name: Delete found files
    ansible.windows.win_file:
      path: "{{ item.path }}"
      state: absent`your text`
    loop: "{{ found_files.files }}"
    when: found_files.files | length > 0

The full traceback is:

2024-02-26T19:57:01.0665891Z Exception calling "EnumerateFileSystemInfos" with "2" argument(s): "The name of the file cannot be resolved by the system. 2024-02-26T19:57:01.0674901Z " 2024-02-26T19:57:01.0675715Z At line:234 char:17 2024-02-26T19:57:01.0676357Z + ... Search-Path -Path $dir_child.FullName @PSBoundParameters

This is the error I got.

0

There are 0 best solutions below