ansible-collections / ansible.windows

Windows core collection for Ansible
https://galaxy.ansible.com/ansible/windows
GNU General Public License v3.0
246 stars 168 forks source link

Powershell script (windows.win_powershell) where parameters are not being passed through to the script #664

Open sjanssen15 opened 6 days ago

sjanssen15 commented 6 days ago
SUMMARY

When running a Powershell script with the module ansible.windows.win_powershell + Parameters (see second example on the docs website) I get no passthrough of the parameter.

ISSUE TYPE
COMPONENT NAME

ansible.windows.win_powershell

ANSIBLE VERSION
ansible-playbook [core 2.16.3]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/xxxx/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.12/site-packages/ansible
  ansible collection location = /home/xxxx/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible-playbook
  python version = 3.12.5 (main, Aug 26 2024, 10:40:21) [GCC 8.5.0 20210514 (Red Hat 8.5.0-22)] (/usr/bin/python3.12)
  jinja version = 3.1.2
  libyaml = True
COLLECTION VERSION
ansible-galaxy collection list ansible.windows

# /usr/lib/python3.12/site-packages/ansible_collections
Collection      Version
--------------- -------
ansible.windows 2.2.0  
CONFIGURATION
ansible-config dump --only-changed
CONFIG_FILE() = /etc/ansible/ansible.cfg
OS / ENVIRONMENT

OS: Red Hat Enterprise Linux 8.10 Kernel: 4.18.0-553.22.1.el8_10.x86_64

STEPS TO REPRODUCE

Running the following Playbook to a Windows Server 2022 standard host via WinRM with Kerberos auth.

---
- name: Test Windows Ansible playbook
  hosts: all
  tasks:
    - name: Test script
      ansible.windows.win_powershell:
        script: |
          Write-Host $Path
        parameters:
          Path: C:\temp
      register: script

    - name: debug
      debug:
        msg: "{{ script }}"
EXPECTED RESULTS

The parameter to be passed on to the script and being usable in the script.

ACTUAL RESULTS

Output shows the empty parameter output. This is the same task as the second example in the examples on the Ansible docs: https://docs.ansible.com/ansible/latest/collections/ansible/windows/win_powershell_module.html

PLAY [Test Windows Ansible playbook] **************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************
ok: [SERVER]

TASK [Test script] ********************************************************************************************************************************************************************************
changed: [SERVER]

TASK [debug] **************************************************************************************************************************************************************************************
ok: [SERVER] => 
  msg:
    changed: true
    debug: []
    error: []
    failed: false
    host_err: ''
    host_out: ""
    information:
    - message_data:
        BackgroundColor: null
        ForegroundColor: null
        Message: ''
        NoNewLine: false
      source: Write-Host
      tags:
      - PSHOST
      time_generated: '2024-10-10T13:07:56.9684666Z'
    output: []
    result: {}
    verbose: []
    warning: []

PLAY RECAP ****************************************************************************************************************************************************************************************
SERVER        : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
jborean93 commented 6 days ago

The parameters option just sets the parameters when calling the script, you still need to specify the param block to map those parameters or access them through $args. In your example you just want to add param($Path) to the top of the script being run and it will be able to access the parameter you specified.

    - name: Test script
      ansible.windows.win_powershell:
        script: |
          param($Path)

          Write-Host $Path
        parameters:
          Path: C:\temp
      register: script
sjanssen15 commented 6 days ago

The parameters option just sets the parameters when calling the script, you still need to specify the param block to map those parameters or access them through $args. In your example you just want to add param($Path) to the top of the script being run and it will be able to access the parameter you specified.


    - name: Test script

      ansible.windows.win_powershell:

        script: |

          param($Path)

          Write-Host $Path

        parameters:

          Path: C:\temp

      register: script

Thanks for your time to reply. Sorry I didn't understood this. The documentation doesn't say this at parameters. Perhaps good to explain?

jborean93 commented 6 days ago

The script is treated as a ScriptBlock to run, the parameter don't set variables but pass it through as parameters when invoking it. Consider this example

Function My-Function {
    Write-Host "Path is '$Path'"
}

My-Function -Path 'foo'

The $Path variable is not set in the function because you don't actually specify the parameters for the function. To get it working you need to specify the parameter in the function/scriptblock itself:

Function My-Function {
    param ($Path)

    Write-Host "Path is '$Path'"
}

My-Function -Path 'foo'

You could also access it through $args[1] ($args[0] is the parameter name -Path) but that's less obvious and I wouldn't recommend it

Function My-Function {
    Write-Host "Path is '$($args[1])'"
}

My-Function -Path 'foo'

The documentation for the module is at https://github.com/ansible-collections/ansible.windows/blob/5f859d110c8fc3eabd2bc435ecdd2ab01f3ccd46/plugins/modules/win_powershell.py#L62, if you can figure out a better way of explaining it then we are happy to see a PR with the change.