Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

win_powershell doesn't respect depth parameter when using pwsh executable #642

Closed
DevOpsJeremy opened this issue Aug 13, 2024 · 1 comment · Fixed by #646
Closed

win_powershell doesn't respect depth parameter when using pwsh executable #642

DevOpsJeremy opened this issue Aug 13, 2024 · 1 comment · Fixed by #646

Comments

@DevOpsJeremy
Copy link

DevOpsJeremy commented Aug 13, 2024

SUMMARY

In the win_powershell module, when using pwsh as the executable, the module fails to respect the depth parameter when converting $Ansible.Result values.

ISSUE TYPE
  • Bug Report
COMPONENT NAME

win_powershell

ANSIBLE VERSION
ansible [core 2.12.10]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/runner/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.8/site-packages/ansible
  ansible collection location = /runner/requirements_collections:/home/runner/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.8.13 (default, Jun 14 2022, 17:49:07) [GCC 8.5.0 20210514 (Red Hat 8.5.0-13)]
  jinja version = 2.11.3
  libyaml = True
COLLECTION VERSION
# /usr/share/ansible/collections/ansible_collections
Collection      Version
--------------- -------
ansible.windows 2.1.0  
CONFIGURATION
ANSIBLE_FORCE_COLOR(env: ANSIBLE_FORCE_COLOR) = True
COLLECTIONS_PATHS(env: ANSIBLE_COLLECTIONS_PATHS) = ['/runner/requirements_collections', '/home/runner/.ansible/collections', '/usr/share/ansible/collections']
DEFAULT_CALLBACK_PLUGIN_PATH(env: ANSIBLE_CALLBACK_PLUGINS) = ['/runner/artifacts/20952/callback']
DEFAULT_ROLES_PATH(env: ANSIBLE_ROLES_PATH) = ['/runner/requirements_roles', '/home/runner/.ansible/roles', '/usr/share/ansible/roles', '/etc/ansible/roles']
DEFAULT_STDOUT_CALLBACK(env: ANSIBLE_STDOUT_CALLBACK) = awx_display
HOST_KEY_CHECKING(env: ANSIBLE_HOST_KEY_CHECKING) = False
INVENTORY_UNPARSED_IS_FAILED(env: ANSIBLE_INVENTORY_UNPARSED_FAILED) = True
RETRY_FILES_ENABLED(env: ANSIBLE_RETRY_FILES_ENABLED) = False
OS / ENVIRONMENT

Windows 10 v20.05

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.4
PSEdition                      Core
GitCommitId                    7.4.4
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
STEPS TO REPRODUCE

Pass any nested .NET object(s) to the $Ansible.Result variable. If a simple hashtable is passed to $Ansible.Result, the issue does not arise (output is successfully converted as expected). The output returned by the script (e.g.: via return $output instead of $Ansible.Result = $output), the output appears to always be converted successfully. The issue only arises with the following factors:

  • The value is passed to $Ansible.Result
  • The value is a nested object (not hashtable or simple type such as int/string)
- name: Return output
  hosts: win-host
  tasks:
    - name: With pwsh
      ansible.windows.win_powershell:
        depth: 5
        executable: pwsh
        script: |
          $Ansible.Result = [PSCustomObject] @{
            level1 = [PSCustomObject] @{
              level2 = @(
                [PSCustomObject] @{
                  level3a = "a"
                  level3b = "b"
                },
                [PSCustomObject] @{
                  level3c = "c"
                  level3d = "d"
                }
              )
            }
          }
          return $Ansible.Result
EXPECTED RESULTS

The win_powershell module should convert the return data from the $Ansible.Result ({ result: {} } in the task output) variable to the depth defined by the depth parameter (in this case: 5). The below is the result when executable: pwsh is not used. Both output and result are converted to the correct depth.

changed: [adp-backfit] => {
    "changed": true,
    "debug": [],
    "error": [],
    "host_err": "",
    "host_out": "",
    "information": [],
    "invocation": {
        "module_args": {
            "arguments": null,
            "chdir": null,
            "creates": null,
            "depth": 5,
            "error_action": "continue",
            "executable": null,
            "parameters": null,
            "removes": null,
            "script": "$Ansible.Result = [PSCustomObject] @{\\n  level1 = [PSCustomObject] @{\\n    level2 = @(\\n      [PSCustomObject] @{\\n        level3a = \\"a\\"\\n        level3b = \\"b\\"\\n      },\\n      [PSCustomObject] @{\\n        level3c = \\"c\\"\\n        level3d = \\"d\\"\\n      }\\n    )\\n  }\\n}\\nreturn $Ansible.Result\\n"
        }
    },
    "output": [
        {
            "level1": {
                "level2": [
                    {
                        "level3a": "a",
                        "level3b": "b"
                    },
                    {
                        "level3c": "c",
                        "level3d": "d"
                    }
                ]
            }
        }
    ],
    "result": {
        "level1": {
            "level2": [
                {
                    "level3a": "a",
                    "level3b": "b"
                },
                {
                    "level3c": "c",
                    "level3d": "d"
                }
            ]
        }
    },
    "verbose": [],
    "warning": []
}
ACTUAL RESULTS

The below is the result when executable: pwsh is used. While the output value was converted to the appropriate depth, the result value was not converted at all.

changed: [win-host] => {
    "changed": true,
    "debug": [],
    "error": [],
    "host_err": "",
    "host_out": "",
    "information": [],
    "invocation": {
        "module_args": {
            "arguments": null,
            "chdir": null,
            "creates": null,
            "depth": 5,
            "error_action": "continue",
            "executable": "pwsh",
            "parameters": null,
            "removes": null,
            "script": "$Ansible.Result = [PSCustomObject] @{\\n  level1 = [PSCustomObject] @{\\n    level2 = @(\\n      [PSCustomObject] @{\\n        level3a = \\"a\\"\\n        level3b = \\"b\\"\\n      },\\n      [PSCustomObject] @{\\n        level3c = \\"c\\"\\n        level3d = \\"d\\"\\n      }\\n    )\\n  }\\n}\\nreturn $Ansible.Result\\n"
        }
    },
    "output": [
        {
            "level1": {
                "level2": [
                    {
                        "level3a": "a",
                        "level3b": "b"
                    },
                    {
                        "level3c": "c",
                        "level3d": "d"
                    }
                ]
            }
        }
    ],
    "result": "@{level1=}",
    "verbose": [],
    "warning": []
}
@jborean93
Copy link
Collaborator

This sounds like it might be due to the serialization depth that is set by default. Will have to look into how that can be controlled so we can use our own custom depth value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants