Skip to content

Commit

Permalink
Merge pull request #13 from d-strobel/feat/add-new-module-dhcp-option…
Browse files Browse the repository at this point in the history
…-value-v4

Feat/add new module dhcp option value v4
  • Loading branch information
d-strobel authored Apr 5, 2024
2 parents 04e26e2 + 4b68a1e commit 4b1c200
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.vscode
.vagrant
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Collection of windows ansible modules.
| dhcp | dhcp_exclusion_v4 | Add or remove an IPv4 exclusion range. |
| dhcp | dhcp_failover_v4 | Add or remove an IPv4 failover. |
| dhcp | dhcp_option_definition_v4 | Add, remove or change an IPv4 option definition. |
| dhcp | dhcp_option_value_v4 | Add, remove or change an IPv4 option value. |
| dhcp | dhcp_scope_v4 | Add or remove a IPv4 dhcp scope. |
| fsrm | fsrm_file_screen_template | Add or modify file screen templates |
| fsrm | fsrm_filegroup | Add or modify file groups |
Expand Down
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace: d_strobel
name: windows
version: "1.3.1"
version: "1.4.0"
readme: README.md
authors:
- Dustin Strobel @d-strobel
Expand Down
101 changes: 101 additions & 0 deletions plugins/modules/dhcp_option_value_v4.ps1
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()
47 changes: 47 additions & 0 deletions plugins/modules/dhcp_option_value_v4.py
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)
'''
6 changes: 6 additions & 0 deletions vagrant/Vagrantfile
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

0 comments on commit 4b1c200

Please sign in to comment.