From 8a247989c57e14e6a499a48cc88c100899f99e01 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Tue, 19 Sep 2017 14:18:52 +1000 Subject: [PATCH] windows: fix list type in legacy module utils (#30483) * windows: fix list type in legacy module utils * only change the return for the list type instead of affecting it all * additional null check when using an array (cherry picked from commit 01563ccd5dfc9936397b7480f3f50531fb54edbd) --- .../powershell/Ansible.ModuleUtils.Legacy.psm1 | 6 +++++- .../library/testlist.ps1 | 12 ++++++++++++ .../win_module_utils_legacy/tasks/main.yml | 18 ++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 test/integration/targets/win_module_utils_legacy/library/testlist.ps1 diff --git a/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.Legacy.psm1 b/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.Legacy.psm1 index a526211be0f..76f5f1454fa 100644 --- a/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.Legacy.psm1 +++ b/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.Legacy.psm1 @@ -201,7 +201,9 @@ Function Get-AnsibleParam($obj, $name, $default = $null, $resultobj = @{}, $fail # If $value -eq $null, the parameter was unspecified by the user (deliberately or not) # Please leave $null-values intact, modules need to know if a parameter was specified - if ($value -ne $null) { + # When $value is already an array, we cannot rely on the null check, as an empty list + # is seen as null in the check below + if ($value -ne $null -or $value -is [array]) { if ($type -eq "path") { # Expand environment variables on path-type $value = Expand-Environment($value) @@ -240,6 +242,8 @@ Function Get-AnsibleParam($obj, $name, $default = $null, $resultobj = @{}, $fail } else { Fail-Json -obj $resultobj -message "Get-AnsibleParam: Parameter '$name' is not a YAML list." } + # , is not a typo, forces it to return as a list when it is empty or only has 1 entry + return ,$value } } diff --git a/test/integration/targets/win_module_utils_legacy/library/testlist.ps1 b/test/integration/targets/win_module_utils_legacy/library/testlist.ps1 new file mode 100644 index 00000000000..06ef17b435f --- /dev/null +++ b/test/integration/targets/win_module_utils_legacy/library/testlist.ps1 @@ -0,0 +1,12 @@ +#powershell + +#Requires -Module Ansible.ModuleUtils.Legacy + +$params = Parse-Args $args +$value = Get-AnsibleParam -Obj $params -Name value -Type list + +if ($value -isnot [array]) { + Fail-Json -obj @{} -message "value was not a list but was $($value.GetType().FullName)" +} + +Exit-Json @{ count = $value.Count } diff --git a/test/integration/targets/win_module_utils_legacy/tasks/main.yml b/test/integration/targets/win_module_utils_legacy/tasks/main.yml index 85c81938cd0..94df11cb161 100644 --- a/test/integration/targets/win_module_utils_legacy/tasks/main.yml +++ b/test/integration/targets/win_module_utils_legacy/tasks/main.yml @@ -21,3 +21,21 @@ - path: '{{ bogus_driveletter.stdout_lines[0] }}:\goodpath' - path: '{{ bogus_driveletter.stdout_lines[0] }}:\badpath*%@:\blar' should_fail: true + +- name: test list parameters + testlist: + value: '{{item.value}}' + register: list_tests + failed_when: list_tests|failed or list_tests.count != item.count + with_items: + - value: [] + count: 0 + - value: + - 1 + - 2 + count: 2 + - value: + - 1 + count: 1 + - value: "1, 2" + count: 2