From e9786db541d4b679fd2ba5d89a96363164f886cb Mon Sep 17 00:00:00 2001 From: Trond Hindenes Date: Wed, 30 Sep 2015 23:29:05 +0200 Subject: [PATCH] Windows: Updated docs to reflect the new name of get-attr, along with examples --- docsite/rst/developing_modules.rst | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/docsite/rst/developing_modules.rst b/docsite/rst/developing_modules.rst index 6eedcb2fa46..36c8283846a 100644 --- a/docsite/rst/developing_modules.rst +++ b/docsite/rst/developing_modules.rst @@ -509,6 +509,7 @@ Windows modules checklist * Favour native powershell and .net ways of doing things over calls to COM libraries or calls to native executables which may or may not be present in all versions of windows * modules are in powershell (.ps1 files) but the docs reside in same name python file (.py) * look at ansible/lib/ansible/module_utils/powershell.ps1 for common code, avoid duplication +* Ansible uses strictmode so be sure to test with that enabled * start with:: #!powershell @@ -518,18 +519,21 @@ then:: then:: # WANT_JSON # POWERSHELL_COMMON + +then, to parse all arguments into a variable modules generally use:: + $params = Parse-Args $args * Arguments: * Try and use state present and state absent like other modules - * You need to check that all your mandatory args are present:: - - If ($params.state) { - $state = $params.state.ToString().ToLower() - If (($state -ne 'started') -and ($state -ne 'stopped') -and ($state -ne 'restarted')) { - Fail-Json $result "state is '$state'; must be 'started', 'stopped', or 'restarted'" - } - } - + * You need to check that all your mandatory args are present. You can do this using the builtin Get-AnsibleParam function. + * Required arguments:: + $package = Get-AnsibleParam -obj $params -name name -failifempty $true + * Required arguments with name validation:: + $state = Get-AnsibleParam -obj $params -name "State" -ValidateSet "Present","Absent" -resultobj $resultobj -failifempty $true + * Optional arguments with name validation:: + $state = Get-AnsibleParam -obj $params -name "State" -default "Present" -ValidateSet "Present","Absent" + * the If "FailIfEmpty" is true, the resultobj parameter is used to specify the object returned to fail-json. You can also override the default message + using $emptyattributefailmessage (for missing required attributes) and $ValidateSetErrorMessage (for attribute validation errors) * Look at existing modules for more examples of argument checking. * Results @@ -551,7 +555,6 @@ then:: * Have you tested for powershell 3.0 and 4.0 compliance? - Deprecating and making module aliases ``````````````````````````````````````