Best way to run powershell command?

82 views
Skip to first unread message

Ab Generette

unread,
May 15, 2018, 11:20:20 AM5/15/18
to Ansible Project

On my (winrm) target host, I am able to run the following:

PS C:\Users\agenerette> c:/Python27/python.exe c:/xx/run/yy/tests/confluence_api_test.py

When I try to incorporate that into a playbook, however, I get a great block of mostly encoded error output that I'm not able to make sense of.  I've attached a couple of screen-shots to give a more complete picture of what I'm seeing, but here's an excerpt:

PLAY [873320-wrk04-ia] *********************************************************

TASK [Do a test run of Sharefiles and Confluence API scripts...] ***************
Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/ansible/plugins/connection/winrm.py", line 274, in exec_command
    result = self._winrm_exec(cmd_parts[0], cmd_parts[1:], from_exec=True)
  File "/usr/lib/python2.6/site-packages/ansible/plugins/connection/winrm.py", line 219, in _winrm_exec
    response = Response(self.protocol.get_command_output(self.shell_id, command_id))
  File "/home/agenerette/.local/lib/python2.6/site-packages/winrm/protocol.py", line 398, in get_command_output
    self._raw_get_command_output(shell_id, command_id)
  File "/home/agenerette/.local/lib/python2.6/site-packages/winrm/protocol.py", line 417, in _raw_get_command_output
    res = self.send_message(xmltodict.unparse(req))
  File "/home/agenerette/.local/lib/python2.6/site-packages/winrm/protocol.py", line 244, in send_message
    fault = root.find('soapenv:Body/soapenv:Fault', xmlns)
TypeError: find() takes exactly 2 arguments (3 given)
failed: [873320-wrk04-ia] (item=c:/t3/run/ae/tests/sharefile_api_test.py) => {"item": "c:/t3/run/ae/tests/sharefile_api_test.py", "msg": "failed to exec cmd PowerShell -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -EncodedCommand

And here's a copy of the playbook:

---
- hosts: "{{ targets }}"
  user: "{{ admin }}"
  gather_facts: yes

  vars:
    python_exec: c:/Python27/python.exe
    working_dir: c:/xx/run/yy

    sharefile_script: c:/xx/run/yy/tests/sharefile_api_test.py
    confl_script: c:/xx/run/yy/tests/confluence_api_test.py

  tasks:
    - name: Do a test run of Sharefiles and Confluence API scripts...
      win_shell: "'{{ python_exec }} {{ item }}'"
#     win_shell: "Get-Item Env:ldap_user"
      with_items:
        - "{{ sharefile_script }}"
        - "{{ confl_script }}"
      environment:
        LDAP_USER: "{{ my_ldap_user }}"
        LDAP_REG_CODE: "null"
        LDAP_PASS: "{{ my_ldap_pass }}"
        GDOC_PASS: "{{ my_gdoc_pass }}"
        SHARE_USER: "{{ my_sharefiles_user }}"
        SHARE_PASS: "{{ my_sharefiles_pass }}"
        WIKI_USERNAME: "{{ my_ldap_user }}"
        WIKI_PASSWORD: "{{ my_ldap_pass }}"
      ignore_errors: yes
      register: result
    - debug: var=result





Jordan Borean

unread,
May 15, 2018, 3:42:29 PM5/15/18
to Ansible Project
The main issue you have is that there is a compatibility issue with pywinrm 0.3.0 and Pythoon 2.6 as shown here https://github.com/diyan/pywinrm/issues/204. This is a controller side issue and you either need to downgrade pywinrm to 0.2.2 or use Python 2.7, 3.5+.

Also what version of Ansible are you running, it doesn't look like it is pipelining the modules which was added in 2.3?

Thanks

Jordan

Ab Generette

unread,
May 15, 2018, 4:27:02 PM5/15/18
to Ansible Project
Ok, I think I'm going to pull back and work on upgrading both Python (to 2.7.5) and Ansible.  We're 2.2.1.0, where the latter is concerned.

Thanks for your help, Jordan.

-Anthony

Jordan Borean

unread,
May 15, 2018, 5:42:21 PM5/15/18
to Ansible Project
No worries, I was just curious as to why it wasn't pipelining. The syntax you had there seemed good to me, the only thing I would recommend is to use win_command instead of win_shell when calling the Python executable. The win_shell module should be used to execute things in a shell interpreter like PowerShell commands while win_command is used to execute executables.

Thanks

Jordan

Ab Generette

unread,
May 15, 2018, 6:13:53 PM5/15/18
to Ansible Project
Ah, ok.  I thought I needed to use win_shell, because of the need to set environment variables.  I see that's not the case, though.  Thx.

Jordan Borean

unread,
May 15, 2018, 6:30:48 PM5/15/18
to Ansible Project
Environment variables are set for all modules, except raw (and maybe script I can't remember). It is a mechanism done by Ansible before running the module code.

Thanks

Jordan
Reply all
Reply to author
Forward
0 new messages