community.windows.win_defrag module – Consolidate fragmented files on local volumes

Note

This module is part of the community.windows collection (version 2.2.0).

You might already have this collection installed if you are using the ansible package. It is not included in ansible-core. To check whether it is installed, run ansible-galaxy collection list.

To install it, use: ansible-galaxy collection install community.windows. You need further requirements to be able to use this module, see Requirements for details.

To use it in a playbook, specify: community.windows.win_defrag.

Synopsis

Requirements

The below requirements are needed on the host that executes this module.

  • defrag.exe

Parameters

Parameter

Comments

exclude_volumes

list / elements=string

A list of drive letters or mount point paths to exclude from defragmentation.

freespace_consolidation

boolean

Perform free space consolidation on the specified volumes.

Choices:

  • false ← (default)

  • true

include_volumes

list / elements=string

A list of drive letters or mount point paths of the volumes to be defragmented.

If this parameter is omitted, all volumes (not excluded) will be fragmented.

parallel

boolean

Run the operation on each volume in parallel in the background.

Choices:

  • false ← (default)

  • true

priority

string

Run the operation at low or normal priority.

Choices:

  • "low" ← (default)

  • "normal"

Examples

- name: Defragment all local volumes (in parallel)
  community.windows.win_defrag:
    parallel: yes

- name: 'Defragment all local volumes, except C: and D:'
  community.windows.win_defrag:
    exclude_volumes: [ C, D ]

- name: 'Defragment volume D: with normal priority'
  community.windows.win_defrag:
    include_volumes: D
    priority: normal

- name: Consolidate free space (useful when reducing volumes)
  community.windows.win_defrag:
    freespace_consolidation: yes

Return Values

Common return values are documented here, the following are the fields unique to this module:

Key

Description

changed

boolean

Whether or not any changes were made.

Returned: always

Sample: true

cmd

string

The complete command line used by the module.

Returned: always

Sample: "defrag.exe /C /V"

msg

string

Possible error message on failure.

Returned: failed

Sample: "Command 'defrag.exe' not found in $env:PATH."

rc

integer

The return code for the command.

Returned: always

Sample: 0

stderr

string

The error output from the command.

Returned: always

stdout

string

The standard output from the command.

Returned: always

Sample: "Success."

Authors

  • Dag Wieers (@dagwieers)