win_environment: Clean up, check-mode and diff support (#21356)
* win_environment: Clean up, check-mode and diff support Changes include: - Remove trailing semi-colons - Replaced PSObjects into normal hashes - Make use of Get-AnsibleParam and types - Added check-mode support - Added diff support * Improve diff-support by using standard naming I started to use the variable $diff_support for the boolean that takes care of diff output support.
This commit is contained in:
parent
b1f6344ecb
commit
581bf7bb97
1 changed files with 57 additions and 24 deletions
|
@ -19,35 +19,68 @@
|
||||||
# WANT_JSON
|
# WANT_JSON
|
||||||
# POWERSHELL_COMMON
|
# POWERSHELL_COMMON
|
||||||
|
|
||||||
$params = Parse-Args $args;
|
$params = Parse-Args $args -supports_check_mode $true
|
||||||
$state = Get-AnsibleParam -obj $params -name "state" -default "present" -validateSet "present","absent"
|
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
|
||||||
$name = Get-AnsibleParam -obj $params -name "name" -failifempty $true
|
$diff_support = Get-AnsibleParam -obj $params -name "_ansible_diff" -type "bool" -default $false
|
||||||
$level = Get-AnsibleParam -obj $params -name "level" -validateSet "machine","process","user" -failifempty $true
|
|
||||||
$value = Get-AnsibleParam -obj $params -name "value"
|
|
||||||
|
|
||||||
If ($level) {
|
$name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true
|
||||||
$level = $level.ToString().ToLower()
|
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -default "present" -validateset "present","absent"
|
||||||
}
|
$value = Get-AnsibleParam -obj $params -name "value" -type "str"
|
||||||
|
$level = Get-AnsibleParam -obj $params -name "level" -type "str" -validateSet "machine","user","process" -failifempty $true
|
||||||
|
|
||||||
$before_value = [Environment]::GetEnvironmentVariable($name, $level)
|
$before_value = [Environment]::GetEnvironmentVariable($name, $level)
|
||||||
|
|
||||||
$state = $state.ToString().ToLower()
|
# When removing environment, set value to $null if set
|
||||||
if ($state -eq "present" ) {
|
if ($state -eq "absent" -and $value) {
|
||||||
|
$value = $null
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = @{
|
||||||
|
before_value = $before_value
|
||||||
|
changed = $false
|
||||||
|
level = $level
|
||||||
|
name = $name
|
||||||
|
value = $value
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($diff_support) {
|
||||||
|
$result.diff = @{}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($state -eq "present" -and $before_value -ne $value) {
|
||||||
|
if (-not $check_mode) {
|
||||||
[Environment]::SetEnvironmentVariable($name, $value, $level)
|
[Environment]::SetEnvironmentVariable($name, $value, $level)
|
||||||
} Elseif ($state -eq "absent") {
|
}
|
||||||
|
$result.changed = $true
|
||||||
|
|
||||||
|
if ($diff_support) {
|
||||||
|
if ($before_value -eq $null) {
|
||||||
|
$result.diff.prepared = @"
|
||||||
|
[$level]
|
||||||
|
+$NAME = $value
|
||||||
|
"@
|
||||||
|
} else {
|
||||||
|
$result.diff.prepared = @"
|
||||||
|
[$level]
|
||||||
|
-$NAME = $before_value
|
||||||
|
+$NAME = $value
|
||||||
|
"@
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} elseif ($state -eq "absent" -and $before_value -ne $null) {
|
||||||
|
if (-not $check_mode) {
|
||||||
[Environment]::SetEnvironmentVariable($name, $null, $level)
|
[Environment]::SetEnvironmentVariable($name, $null, $level)
|
||||||
|
}
|
||||||
|
$result.changed = $true
|
||||||
|
|
||||||
|
if ($diff_support) {
|
||||||
|
$result.diff.prepared = @"
|
||||||
|
[$level]
|
||||||
|
-$NAME = $before_value
|
||||||
|
"@
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$after_value = [Environment]::GetEnvironmentVariable($name, $level)
|
Exit-Json $result
|
||||||
|
|
||||||
$result = New-Object PSObject;
|
|
||||||
Set-Attr $result "changed" $false;
|
|
||||||
Set-Attr $result "name" $name;
|
|
||||||
Set-Attr $result "before_value" $before_value;
|
|
||||||
Set-Attr $result "value" $after_value;
|
|
||||||
Set-Attr $result "level" $level;
|
|
||||||
if ($before_value -ne $after_value) {
|
|
||||||
Set-Attr $result "changed" $true;
|
|
||||||
}
|
|
||||||
|
|
||||||
Exit-Json $result;
|
|
||||||
|
|
Loading…
Reference in a new issue