1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

win_wakeonlan: Rewrite using AnsibleModule (#48380)

This commit is contained in:
Dag Wieers 2018-11-11 22:06:55 +01:00 committed by Jordan Borean
parent 2dd56eb15c
commit baaa142248

View file

@ -3,22 +3,26 @@
# Copyright: (c) 2017, Dag Wieers (@dagwieers) <dag@wieers.com>
# 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
$ErrorActionPreference = "Stop"
$params = Parse-Args $args -supports_check_mode $true
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
$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
$result = @{
changed = $false
$spec = @{
options = @{
mac = @{ type='str'; required=$true }
broadcast = @{ type='str'; default='255.255.255.255' }
port = @{ type='int'; default=7 }
}
supports_check_mode = $true
}
$mac_orig = $mac
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
$module.Result.changed = $false
$mac = $module.Params.mac
$mac_orig = $module.Params.mac
$broadcast = $module.Params.broadcast
$port = $module.Params.port
$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()