forked from ansible/ansible
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
win_defrag: Rewrite using AnsibleModule (ansible#48381)
* win_defrag: Rewrite using AnsibleModule * Fix delta return value
- Loading branch information
Showing
1 changed file
with
29 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,29 +3,35 @@ | |
# Copyright: (c) 2017, Dag Wieers (@dagwieers) <[email protected]> | ||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
#Requires -Module Ansible.ModuleUtils.Legacy | ||
#AnsibleRequires -CSharpUtil Ansible.Basic | ||
#Requires -Module Ansible.ModuleUtils.ArgvParser | ||
#Requires -Module Ansible.ModuleUtils.CommandUtil | ||
|
||
$ErrorActionPreference = "Stop" | ||
$spec = @{ | ||
options = @{ | ||
include_volumes = @{ type='list' } | ||
exclude_volumes = @{ type='list' } | ||
freespace_consolidation = @{ type='bool'; default=$false } | ||
priority = @{ type='str'; default='low'; choices=@( 'low', 'normal') } | ||
parallel = @{ type='bool'; default=$false } | ||
} | ||
supports_check_mode = $true | ||
} | ||
|
||
$params = Parse-Args $args -supports_check_mode $true | ||
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false | ||
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) | ||
|
||
$include_volumes = Get-AnsibleParam -obj $params -name "include_volumes" -type "list" | ||
$exclude_volumes = Get-AnsibleParam -obj $params -name "exclude_volumes" -type "list" | ||
$freespace_consolidation = Get-AnsibleParam -obj $params -name "freespace_consolidation" -type "bool" -default $false | ||
$priority = Get-AnsibleParam -obj $params -name "priority" -type "str" -default "low" -validateset "low","normal" | ||
$parallel = Get-AnsibleParam -obj $params -name "parallel" -type "bool" -default $false | ||
$include_volumes = $module.Params.include_volumes | ||
$exclude_volumes = $module.Params.exclude_volumes | ||
$freespace_consolidation = $module.Params.freespace_consolidation | ||
$priority = $module.Params.priority | ||
$parallel = $module.Params.parallel | ||
|
||
$result = @{ | ||
changed = $false | ||
} | ||
$module.Result.changed = $false | ||
|
||
$executable = "defrag.exe" | ||
|
||
if (-not (Get-Command -Name $executable -ErrorAction SilentlyContinue)) { | ||
Fail-Json $result "Command '$executable' not found in $env:PATH." | ||
$module.FailJson("Command '$executable' not found in $env:PATH.") | ||
} | ||
|
||
$arguments = @() | ||
|
@@ -53,7 +59,7 @@ if ($exclude_volumes) { | |
} | ||
} | ||
|
||
if ($check_mode) { | ||
if ($module.CheckMode) { | ||
$arguments += "/A" | ||
} elseif ($freespace_consolidation) { | ||
$arguments += "/X" | ||
|
@@ -72,20 +78,20 @@ $arguments += "/V" | |
$argument_string = Argv-ToString -arguments $arguments | ||
|
||
$start_datetime = [DateTime]::UtcNow | ||
$result.cmd = "$executable $argument_string" | ||
$module.Result.cmd = "$executable $argument_string" | ||
|
||
$command_result = Run-Command -command "$executable $argument_string" | ||
|
||
$end_datetime = [DateTime]::UtcNow | ||
|
||
$result.stdout = $command_result.stdout | ||
$result.stderr = $command_result.stderr | ||
$result.rc = $command_result.rc | ||
$module.Result.stdout = $command_result.stdout | ||
$module.Result.stderr = $command_result.stderr | ||
$module.Result.rc = $command_result.rc | ||
|
||
$result.start = $start_datetime.ToString("yyyy-MM-dd hh:mm:ss.ffffff") | ||
$result.end = $end_datetime.ToString("yyyy-MM-dd hh:mm:ss.ffffff") | ||
$result.delta = $($end_datetime - $start_datetime).ToString("h\:mm\:ss\.ffffff") | ||
$module.Result.start = $start_datetime.ToString("yyyy-MM-dd hh:mm:ss.ffffff") | ||
$module.Result.end = $end_datetime.ToString("yyyy-MM-dd hh:mm:ss.ffffff") | ||
$module.Result.delta = $($end_datetime - $start_datetime).ToString("h\:mm\:ss\.ffffff") | ||
|
||
$result.changed = $true | ||
$module.Result.changed = $true | ||
|
||
Exit-Json $result | ||
$module.ExitJson() |