Ansible hangs on Windows cleanmgr.exe

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 :slight_smile:

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

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

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

or perhaps

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).

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