-
Notifications
You must be signed in to change notification settings - Fork 173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
win_timezone - Upgrade to Ansible.Basic style #705
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,71 +3,75 @@ | |
# Copyright: (c) 2015, Phil Schwartz <[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 | ||
|
||
$params = Parse-Args $args -supports_check_mode $true | ||
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false | ||
$diff_support = Get-AnsibleParam -obj $params -name "_ansible_diff" -type "bool" -default $false | ||
$spec = @{ | ||
options = @{ | ||
timezone = @{ | ||
required = $true | ||
type = "str" | ||
} | ||
} | ||
supports_check_mode = $true | ||
} | ||
|
||
$timezone = Get-AnsibleParam -obj $params -name "timezone" -type "str" -failifempty $true | ||
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) | ||
|
||
$result = @{ | ||
changed = $false | ||
previous_timezone = $timezone | ||
timezone = $timezone | ||
} | ||
$timezone = $module.Params.timezone | ||
Comment on lines
+8
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using We also explicitly have to opt into saying we support check mode with |
||
|
||
Try { | ||
# Get the current timezone set | ||
$result.previous_timezone = $(tzutil.exe /g) | ||
If ($LASTEXITCODE -ne 0) { | ||
Throw "An error occurred when getting the current machine's timezone setting." | ||
} | ||
$module.Result.previous_timezone = $null | ||
$module.Result.timezone = $timezone | ||
Comment on lines
+22
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The module result is located under |
||
|
||
if ( $result.previous_timezone -eq $timezone ) { | ||
Exit-Json $result "Timezone '$timezone' is already set on this machine" | ||
} | ||
Else { | ||
# Check that timezone is listed as an available timezone to the machine | ||
$tzList = $(tzutil.exe /l).ToLower() | ||
If ($LASTEXITCODE -ne 0) { | ||
Throw "An error occurred when listing the available timezones." | ||
} | ||
Function Invoke-TzUtil { | ||
[CmdletBinding()] | ||
param ( | ||
[Parameter(Mandatory)] | ||
[object] | ||
$Module, | ||
|
||
$tzExists = $tzList.Contains(($timezone -Replace '_dstoff').ToLower()) | ||
if (-not $tzExists) { | ||
Fail-Json $result "The specified timezone: $timezone isn't supported on the machine." | ||
} | ||
[Parameter(Mandatory)] | ||
[string] | ||
$Action, | ||
|
||
if ($check_mode) { | ||
$result.changed = $true | ||
} | ||
else { | ||
tzutil.exe /s "$timezone" | ||
if ($LASTEXITCODE -ne 0) { | ||
Throw "An error occurred when setting the specified timezone with tzutil." | ||
} | ||
|
||
$new_timezone = $(tzutil.exe /g) | ||
if ($LASTEXITCODE -ne 0) { | ||
Throw "An error occurred when getting the current machine's timezone setting." | ||
} | ||
|
||
if ($timezone -eq $new_timezone) { | ||
$result.changed = $true | ||
} | ||
} | ||
[Parameter(ValueFromRemainingArguments)] | ||
[string[]] | ||
$ArgumentList | ||
) | ||
|
||
if ($diff_support) { | ||
$result.diff = @{ | ||
before = "$($result.previous_timezone)`n" | ||
after = "$timezone`n" | ||
} | ||
} | ||
$stdout = $null | ||
$stderr = . { tzutil.exe @ArgumentList | Set-Variable -Name stdout } 2>&1 | ForEach-Object ToString | ||
|
||
if ($LASTEXITCODE) { | ||
$Module.Result.stdout = $stdout -join "`n" | ||
$Module.Result.stderr = $stderr -join "`n" | ||
$Module.Result.rc = $LASTEXITCODE | ||
$Module.FailJson("An error occurred when $Action.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of
|
||
} | ||
|
||
$stdout | ||
} | ||
|
||
# Get the current timezone set | ||
$previousTz = Invoke-TzUtil /g -Module $module -Action "getting the current machine's timezone setting" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
$module.Result.previous_timezone = $previousTz | ||
|
||
if ($module.DiffMode) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use |
||
$module.Diff.before = "$previousTz`n" | ||
$module.Diff.after = "$timezone`n" | ||
Comment on lines
+59
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of |
||
} | ||
Catch { | ||
Fail-Json $result "Error setting timezone to: $timezone." | ||
|
||
if ($previousTz -ne $timezone) { | ||
# Check that timezone is listed as an available timezone to the machine | ||
$tzList = Invoke-TzUtil /l -Module $module -Action "listing the available timezones" | ||
|
||
if ($tzList -notcontains ($timezone -replace '_dstoff')) { | ||
$module.FailJson("The specified timezone: $timezone isn't supported on the machine.") | ||
} | ||
|
||
if (-not $module.CheckMode) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
$null = Invoke-TzUtil /s $timezone -Module $module -Action "setting the specified timezone" | ||
} | ||
$module.Result.changed = $true | ||
} | ||
|
||
Exit-Json $result | ||
$module.ExitJson() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This newer requires statement is used by Ansible to load in the
Ansible.Basic
C# module util.