-
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.
Merge pull request #13 from d-strobel/feat/add-new-module-dhcp-option…
…-value-v4 Feat/add new module dhcp option value v4
- Loading branch information
Showing
6 changed files
with
157 additions
and
1 deletion.
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
.vscode | ||
.vagrant |
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
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
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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#!powershell | ||
|
||
# Copyright: (c) 2022, Dustin Strobel (@d-strobel) | ||
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
#AnsibleRequires -CSharpUtil Ansible.Basic | ||
#AnsibleRequires -PowerShell Ansible.ModuleUtils.AddType | ||
|
||
$spec = @{ | ||
options = @{ | ||
option_id = @{ type = "int"; required = $true } | ||
scope_id = @{ type = "str" } | ||
reserved_ip = @{ type = "str" } | ||
value = @{ type = "list"; elements = "str"; required = $true } | ||
state = @{ type = "str"; choices = "absent", "present" ; default = "present" } | ||
} | ||
|
||
mutually_exclusive = @( | ||
, @("scope_id", "reserved_ip") | ||
) | ||
supports_check_mode = $true | ||
} | ||
|
||
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) | ||
|
||
# Map variables | ||
$optionID = $module.Params.option_id | ||
$scopeID = $module.Params.scope_id | ||
$reservedIP = $module.Params.reserved_ip | ||
$value = $module.Params.value | ||
$state = $module.Params.state | ||
|
||
# ErrorAction | ||
$ErrorActionPreference = 'Stop' | ||
|
||
# Get option value for specific input | ||
if ($scopeID) { | ||
# Need try/catch block to handle non-existing scope. | ||
# This seems like an issue with the ErrorAction handler. | ||
try { | ||
$dhcpServerOptionValue = Get-DhcpServerv4OptionValue -OptionID $optionID -ScopeId $scopeID -ErrorAction SilentlyContinue | ||
} | ||
catch {} | ||
} | ||
elseif ($reservedIP) { | ||
# Need try/catch block to handle non-existing reservations. | ||
# This seems like an issue with the ErrorAction handler. | ||
try { | ||
$dhcpServerOptionValue = Get-DhcpServerv4OptionValue -OptionID $optionID -ReservedIP $reservedIP -ErrorAction SilentlyContinue | ||
} | ||
catch {} | ||
} | ||
else { | ||
$dhcpServerOptionValue = Get-DhcpServerv4OptionValue -OptionID $optionID -ErrorAction SilentlyContinue | ||
} | ||
|
||
# Early exit | ||
if (($null -eq $dhcpServerOptionValue) -and ($state -eq "absent")) { | ||
$module.ExitJson() | ||
} | ||
|
||
# Remove option value | ||
if (($null -ne $dhcpServerOptionValue) -and ($state -eq "absent")) { | ||
try { | ||
if ($scopeID) { | ||
Remove-DhcpServerv4OptionValue -OptionID $optionID -ScopeId $scopeID -Confirm:$false -WhatIf:$module.CheckMode | Out-Null | ||
} | ||
elseif ($reservedIP) { | ||
Remove-DhcpServerv4OptionValue -OptionID $optionID -ReservedIP $reservedIP -Confirm:$false -WhatIf:$module.CheckMode | Out-Null | ||
} | ||
else { | ||
Remove-DhcpServerv4OptionValue -OptionID $optionID -Confirm:$false -WhatIf:$module.CheckMode | Out-Null | ||
} | ||
$module.Result.changed = $true | ||
$module.ExitJson() | ||
} | ||
catch { | ||
$module.FailJson("Failed to remove DHCP-OptionValue.", $_) | ||
} | ||
} | ||
|
||
# New option value | ||
if ((($null -eq $dhcpServerOptionValue) -and ($state -eq "present")) -or (Compare-Object -ReferenceObject $dhcpServerOptionValue.Value -DifferenceObject $value)) { | ||
try { | ||
if ($scopeID) { | ||
Set-DhcpServerv4OptionValue -OptionID $optionID -ScopeId $scopeID -Value $value -Force -WhatIf:$module.CheckMode | Out-Null | ||
} | ||
elseif ($reservedIP) { | ||
Set-DhcpServerv4OptionValue -OptionID $optionID -ReservedIP $reservedIP -Value $value -Force -WhatIf:$module.CheckMode | Out-Null | ||
} | ||
else { | ||
Set-DhcpServerv4OptionValue -OptionID $optionID -Value $value -Force -WhatIf:$module.CheckMode | Out-Null | ||
} | ||
$module.Result.changed = $true | ||
} | ||
catch { | ||
$module.FailJson("Failed to set DHCP-OptionValue.", $_) | ||
} | ||
} | ||
|
||
$module.ExitJson() |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright: (c) 2022, Dustin Strobel (@d-strobel), | ||
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
DOCUMENTATION = r''' | ||
--- | ||
module: dhcp_option_value_v4 | ||
short_description: Add, remove or change an IPv4 option value. | ||
description: | ||
- Add, remove or change an IPv4 option value. | ||
options: | ||
option_id: | ||
description: | ||
- The identifier of the option. | ||
type: int | ||
required: true | ||
scope_id: | ||
description: | ||
- The identifier of the scope for which the value should be set. | ||
- Only one of C(scope_id) or C(reserved_ip) can be set. | ||
type: str | ||
required: false | ||
reserved_ip: | ||
description: | ||
- The reserverd ip fo which the value shoud be set. | ||
- Only one of C(scope_id) or C(reserved_ip) can be set. | ||
type: str | ||
required: false | ||
value: | ||
description: | ||
- The value to set. | ||
type: list | ||
elements: str | ||
required: true | ||
state: | ||
description: | ||
- Set to C(present) to ensure the value is set. | ||
- Set to C(absent) to ensure the value is removed. | ||
type: str | ||
default: present | ||
choices: [ absent, present ] | ||
author: | ||
- Dustin Strobel (@d-strobel) | ||
''' |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Vagrant.configure("2") do |config| | ||
config.vm.box = "d-strobel/win2022sc-ad" | ||
config.vm.box_version = "1.0.0" | ||
config.ssh.username = "vagrant\\vagrant" | ||
config.ssh.password = "vagrant" | ||
end |