From f0e102f1841096cb13b5d83bf2a62dd91eaaf4ca Mon Sep 17 00:00:00 2001 From: Dustin Strobel Date: Fri, 5 Apr 2024 00:04:14 +0200 Subject: [PATCH 1/6] feat: add module dhcp_server_option_value_v4 --- plugins/modules/dhcp_option_value_v4.ps1 | 101 +++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 plugins/modules/dhcp_option_value_v4.ps1 diff --git a/plugins/modules/dhcp_option_value_v4.ps1 b/plugins/modules/dhcp_option_value_v4.ps1 new file mode 100644 index 0000000..8288cd4 --- /dev/null +++ b/plugins/modules/dhcp_option_value_v4.ps1 @@ -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() From 9c6d3ecc0b969d6174cdf004bfdc4d88149d59e7 Mon Sep 17 00:00:00 2001 From: Dustin Strobel Date: Fri, 5 Apr 2024 10:58:39 +0200 Subject: [PATCH 2/6] docs: add documentation for new module dhcp option value --- plugins/modules/dhcp_option_value_v4.py | 47 +++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 plugins/modules/dhcp_option_value_v4.py diff --git a/plugins/modules/dhcp_option_value_v4.py b/plugins/modules/dhcp_option_value_v4.py new file mode 100644 index 0000000..1029680 --- /dev/null +++ b/plugins/modules/dhcp_option_value_v4.py @@ -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) +''' From 0313e962d875060a4389b2877b8cf3733e73510b Mon Sep 17 00:00:00 2001 From: Dustin Strobel Date: Fri, 5 Apr 2024 10:59:00 +0200 Subject: [PATCH 3/6] chore: bump galaxy version with feature release --- galaxy.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/galaxy.yml b/galaxy.yml index b50eb34..f58f644 100755 --- a/galaxy.yml +++ b/galaxy.yml @@ -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 From 33537874967c10575ee3350591481c633015e7a5 Mon Sep 17 00:00:00 2001 From: Dustin Strobel Date: Fri, 5 Apr 2024 10:59:38 +0200 Subject: [PATCH 4/6] chore: add vagrantfile used for local tests --- vagrant/Vagrantfile | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 vagrant/Vagrantfile diff --git a/vagrant/Vagrantfile b/vagrant/Vagrantfile new file mode 100644 index 0000000..04a7e3c --- /dev/null +++ b/vagrant/Vagrantfile @@ -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 From f291579e72a223ae61141bbf6ffef44ab7202029 Mon Sep 17 00:00:00 2001 From: Dustin Strobel Date: Fri, 5 Apr 2024 10:59:49 +0200 Subject: [PATCH 5/6] chore: add vagrant to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 722d5e7..2a8e70d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .vscode +.vagrant From 4b68a1ea74fd39a0dabf4b99ab8afd0439d2a2b9 Mon Sep 17 00:00:00 2001 From: Dustin Strobel Date: Fri, 5 Apr 2024 11:02:22 +0200 Subject: [PATCH 6/6] docs: update readme with new module --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index cac1eda..e58306b 100644 --- a/README.md +++ b/README.md @@ -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 |