---

- block:

    - name: check for cleanmgr executable
      win_stat:
        path: '{{ ansible_env.windir }}\System32\cleanmgr.exe'
      register: check_cleanmgr_file

    - include_tasks: copy_cleanmgr.yml
      vars:
        os_short_name: 2008r2
      when:
        - not check_cleanmgr_file.stat.exists
        - ('Windows Server 2008 R2' in ansible_distribution)

    - include_tasks: copy_cleanmgr.yml
      vars:
        os_short_name: 2012
      when:
        - not check_cleanmgr_file.stat.exists
        - ('Windows Server 2012' in ansible_distribution)
        - (not 'Windows Server 2012 R2' in ansible_distribution)

    - name: get free space
      win_shell: Get-PSDrive C | Select-Object Free | ConvertTo-Json
      register: free_space_before_cleanup

    - name: ensure cleanup registry paths exist
      win_regedit:
        path: HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\{{ item }}
      loop: "{{ cleanup_registry_keys }}"

    - name: set cleanup registry keys
      win_regedit:
        path: HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\{{ item }}
        name: StateFlags0012
        data: 2
        type: dword
      loop: "{{ cleanup_registry_keys }}"

    - name: run cleanmgr
      win_shell: cleanmgr /sagerun:12

    - name: wait for cleanmgr to finish
      win_shell: (get-wmiobject win32_process | where-object {$_.processname -eq 'cleanmgr.exe'} | measure).count
      register: check_cleanmgr_process
      until: check_cleanmgr_process.stdout is defined and check_cleanmgr_process.stdout|int == 0
      delay: 5
      retries: 300

    - name: get free space
      win_shell: Get-PSDrive C | Select-Object Free | ConvertTo-Json
      register: free_space_after_cleanup

    - debug:
        msg:
          - "Free space before cleanup: {{ ((free_space_before_cleanup.stdout | from_json)['Free']|int / (1024*1024*1024)) | round(2, 'floor') }} GB"
          - "Free space after cleanup: {{ ((free_space_after_cleanup.stdout | from_json)['Free']|int / (1024*1024*1024)) | round(2, 'floor') }} GB"

  rescue:
    - name: ignore any errors
      debug:
        msg: "ignoring any error with clean up with cleanmgr"