# Ansible hangs on Windows cleanmgr.exe

**URL:** <https://forum.ansible.com/t/ansible-hangs-on-windows-cleanmgr-exe/28395>\
**Category:** Ansible Project\
**Tags:** windows\
**Created:** [August 22, 2018, 12:43pm UTC](https://forum.ansible.com/t/ansible-hangs-on-windows-cleanmgr-exe/28395 "2018-08-22T12:43:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SurferL](https://avatars.discourse-cdn.com/v4/letter/s/ecb155/32.png) [@SurferL](https://forum.ansible.com/u/SurferL)\
**Post date:** [August 22, 2018, 12:43pm UTC](https://forum.ansible.com/t/ansible-hangs-on-windows-cleanmgr-exe/28395/1 "2018-08-22T12:43:24Z")

</div>

Using Ansible 2.6.2, I’ve set `cleanmgr.exe /sagerun:3` to everything possible manually on the Windows 10 machine.

In my playbook I have the following task:

- name: windows | disk cleanup  
win\_shell: “cmd.exe /c cleanmgr.exe /sagerun:3”  
args:  
chdir: “C:\Windows\system32”

However it hangs on this task it seems, and I can’t seem to figure out why, or what I could be debugging.

Running the command manually on the machine locally works fine. Also, the command works fine for Windows 7 machine, so I’m not sure this could be a configuration issue on the Windows 10 machine?

Ansible debug (showing the ping task just before it hangs):

TASK [cleanup : windows | verify connection] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /Users/ansible/builds/0/clean\_base\_vms/ansible/roles/cleanup/tasks/windows.yml:3  
ok: [win10\_base] =\> {  
“changed”: false,  
“ping”: “pong”  
}  
Using module file /Users/ansible/clean\_base\_vms/virtualenv/lib/python2.7/site-packages/ansible/modules/windows/win\_shell.ps1  
 ESTABLISH WINRM CONNECTION FOR USER: ansible on PORT 5985 TO   
checking if winrm\_host is an IPv6 address  
 WINRM CONNECT: transport=plaintext endpoint=http://:5985/wsman  
 WINRM OPEN SHELL:   
EXEC (via pipeline wrapper)  
 WINRM EXEC ‘PowerShell’ [‘-NoProfile’, ‘-NonInteractive’, ‘-ExecutionPolicy’, ‘Unrestricted’, ‘-’]

I’ve left this task running for 5 hours before it fails…

Any ideas would be greatly appreciated, thank you in advance 🙂

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [September 4, 2018, 9:36am UTC](https://forum.ansible.com/t/ansible-hangs-on-windows-cleanmgr-exe/28395/2 "2018-09-04T09:36:11Z")

</div>

cleanmgr is probably waiting for window to display progress, but winrm has no window available so that’s probably why it hangs

See [https://stackoverflow.com/questions/28852786/automate-process-of-disk-cleanup-cleanmgr-exe-without-user-intervention](https://stackoverflow.com/questions/28852786/automate-process-of-disk-cleanup-cleanmgr-exe-without-user-intervention)

You might be able to use something like this, but I haven’t tried it myself

```auto
Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:1' -WindowStyle Hidden -Wait

```

```auto

```

```auto
or perhaps

```

```auto

```

---

<div class="post-metadata">

**Author:** ![SurferL](https://avatars.discourse-cdn.com/v4/letter/s/ecb155/32.png) [@SurferL](https://forum.ansible.com/u/SurferL)\
**Post date:** [September 4, 2018, 9:41am UTC](https://forum.ansible.com/t/ansible-hangs-on-windows-cleanmgr-exe/28395/3 "2018-09-04T09:41:47Z")

</div>

Thanks for the response - I’ll give this a test!

One thing I did find weird was that I left the job running overnight, and it succeeded in about 20 hours… so not sure what’s happening there. (When running manually it takes ~5 minutes).

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [September 4, 2018, 8:37pm UTC](https://forum.ansible.com/t/ansible-hangs-on-windows-cleanmgr-exe/28395/4 "2018-09-04T20:37:23Z")

</div>

cleanmgr.exe is an annoying one to run in a non-interactive process. Unless you get it just right it will run and just wait for user input which won’t happen as there’s no UI. I’ve been able to get it working which you can see at [https://github.com/jborean93/packer-windoze/blob/master/roles/cleanup-winsxs/tasks/cleanmgr.yml#L61-L79](https://github.com/jborean93/packer-windoze/blob/master/roles/cleanup-winsxs/tasks/cleanmgr.yml#L61-L79). The state flag number isn’t important, was just a number I chose to have a bit of fun but it can be anything as long as they match each other.

Thanks

Jordan
