From baaa14224854b3f949cf0f401b18faace023d270 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Sun, 11 Nov 2018 22:06:55 +0100 Subject: [PATCH] win_wakeonlan: Rewrite using AnsibleModule (#48380) --- lib/ansible/modules/windows/win_wakeonlan.ps1 | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/lib/ansible/modules/windows/win_wakeonlan.ps1 b/lib/ansible/modules/windows/win_wakeonlan.ps1 index e428c169aab..f14889534d9 100644 --- a/lib/ansible/modules/windows/win_wakeonlan.ps1 +++ b/lib/ansible/modules/windows/win_wakeonlan.ps1 @@ -3,22 +3,26 @@ # Copyright: (c) 2017, Dag Wieers (@dagwieers) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) -#Requires -Module Ansible.ModuleUtils.Legacy - -$ErrorActionPreference = "Stop" +#AnsibleRequires -CSharpUtil Ansible.Basic + +$spec = @{ + options = @{ + mac = @{ type='str'; required=$true } + broadcast = @{ type='str'; default='255.255.255.255' } + port = @{ type='int'; default=7 } + } + 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) -$mac = Get-AnsibleParam -obj $params -name "mac" -type "str" -failifempty $true -$broadcast = Get-AnsibleParam -obj $params -name "broadcast" -type "str" -default "255.255.255.255" -$port = Get-AnsibleParam -obj $params -name "port" -type "int" -default 7 +$module.Result.changed = $false -$result = @{ - changed = $false -} +$mac = $module.Params.mac +$mac_orig = $module.Params.mac +$broadcast = $module.Params.broadcast +$port = $module.Params.port -$mac_orig = $mac $broadcast = [Net.IPAddress]::Parse($broadcast) # Remove possible separator from MAC address @@ -28,7 +32,7 @@ if ($mac.Length -eq (12 + 5)) { # If we don't end up with 12 hexadecimal characters, fail if ($mac.Length -ne 12) { - Fail-Json $result "Incorrect MAC address: $mac_orig" + $module.FailJson("Incorrect MAC address: $mac_orig") } # Create payload for magic packet @@ -38,11 +42,11 @@ $data = (,[byte]255 * 6) + ($target * 20) # Broadcast payload to network $udpclient = new-Object System.Net.Sockets.UdpClient -if (-not $check_mode) { +if (-not $module.CheckMode) { $udpclient.Connect($broadcast, $port) [void] $udpclient.Send($data, 102) } -$result.changed = $true +$module.Result.changed = $true -Exit-Json $result +$module.ExitJson()