Created
October 19, 2023 15:41
-
-
Save murrahjm/b2a7af0b54583342579b6445bde81afc to your computer and use it in GitHub Desktop.
Sample template for writing ansible modules in Powershell 7 for use on linux and windows
This file contains hidden or 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
#!/usr/bin/pwsh | |
#WANT_JSON | |
#sample module for using powershell core instead of python for module code | |
#biggest different is that the legacy windows powershell functions are not available | |
#example uses a 'main' function as the primary executing code, providing parameter validation and error handling in a powershell-y way | |
$ErrorActionPreference = "Stop" | |
$ProgressPreference = "SilentlyContinue" | |
function Main { | |
[CmdletBinding(SupportsShouldProcess)] | |
# | |
param( | |
[Parameter(Mandatory = $True)] | |
[ValidateNotNullOrEmpty()] | |
[String[]]$sample | |
) | |
# initialize output hashtable | |
$result = @{} | |
# do stuff | |
# create output object hashtable or powershell object | |
# do error handling with throw or write-error like normal powershell | |
# don't use write host or have any uncontrolled outputs | |
# | |
# use shouldprocess like normal, the ansible check mode parameter translates to the -Whatif powershell parameter | |
if ($PSCmdlet.ShouldProcess($something)){ | |
# ansible will look for a 'changed' boolean value | |
$result['changed'] = $true | |
} | |
return $result | |
} | |
# get main function parameters | |
$ast = (get-command 'Main').scriptblock.ast | |
$parameter_list = $ast.FindAll( { $args[0] -is [System.Management.Automation.Language.ParameterAst] }, $true) | foreach-object {$_.name.variablepath.tostring()} | |
# import passed parameters and build filtered list for splatting | |
$input_json = get-content -path $args[0] -Raw | |
$params = Convertfrom-json -InputObject $input_json -asHashTable | |
# $params includes everything passed to the module | |
# $filtered_params includes only the passed parameters that match values in the 'Main' function | |
$filtered_params = $params.clone() | |
$params.Keys | foreach-object { | |
if ($parameter_list -notcontains $_) { | |
$filtered_params.Remove($_) | |
} | |
} | |
# pass check mode paramter as a boolean Whatif value to the module | |
$filtered_params.Add('Whatif', $params._ansible_check_mode) | |
# do not confirm changes if check mode is false | |
$filtered_params.Add('Confirm', $params._ansible_check_mode) | |
# run function and return output | |
Try { | |
$results = Main @filtered_params -verbose -WarningVariable warningvar -InformationVariable infovar -ErrorVariable errorvar -warningaction silentlycontinue 4> {$verbosevar} | |
} Catch { | |
#if main functions returns any error, return error output as failure message | |
$results=@{} | |
$results['failed'] = $true | |
$results['msg'] = $errorvar | |
} Finally { | |
#populate other streams and return output | |
$results['warning_stream'] = $warningvar.Message | |
$results['information_stream'] = $infovar.MessageData | |
$results['verbose_stream'] = $verbosevar | |
$results | convertto-json -Compress | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment