# Best way to run Python scripts against Managed Cygwin nodes

**URL:** <https://forum.ansible.com/t/best-way-to-run-python-scripts-against-managed-cygwin-nodes/27525>\
**Category:** Ansible Project\
**Created:** [May 14, 2018, 6:52pm UTC](https://forum.ansible.com/t/best-way-to-run-python-scripts-against-managed-cygwin-nodes/27525 "2018-05-14T18:52:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ab\_Generette](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Ab\_Generette](https://forum.ansible.com/u/Ab_Generette)\
**Post date:** [May 14, 2018, 6:52pm UTC](https://forum.ansible.com/t/best-way-to-run-python-scripts-against-managed-cygwin-nodes/27525/1 "2018-05-14T18:52:55Z")

</div>

I have a CentOS7 control server from which I am trying to manage a few Server2012/Cygwin nodes. Those latter nodes have Cygwin setup such that its sshd component is installed and running (I’m able to successfully ssh to the targets).

My ~/.ansible.cfg is the active one and this file contains

> inventory = ~/playbooks/inv/hosts

The relevant entries in that inventory file for target that I’ve focused on are (note: the “abc\_\*” variables referenced in the inventory file are stored in a vault file that’s pulled in, of course, at run time):

> [abc\_windows]  
> wrk04 ansible\_host=\< target’s IP \>
> 
> [abc\_windows:vars]  
> ansible\_user = “{{ abc\_win\_ansible\_user }}”  
> ansible\_password = “{{ abc\_win\_ansible\_pwd }}”  
> ansible\_port = 5986  
> ansible\_winrm\_transport = ntlm  
> ansible\_connection = winrm  
> ansible\_winrm\_server\_cert\_validation = ignore

A GUI based installer for my company’s software drops in a bunch of files that include the ones referenced in the python\_exec, working\_dir, sharefile\_script, and confl\_script variables, below.

Now, among other things, I’ve been trying to automate the task of running a couple of Python scripts against each of the managed nodes. The playbook, template, and shell script, pasted below, seek to accomplish that end. At the very end of this posting, you can see the output that I get, when I run the playbook. The “/tmp/sharefiles\_confl.log” file is never created and the scripts never get run, as far as I can tell.

> ## [agenerette@build playbooks]$ cat sharefiles\_confluence.yml
> 
> - hosts: “{{ targets }}”  
> user: “{{ admin }}”  
> gather\_facts: yes
> 
> vars:  
> python\_exec: /cygdrive/c/Python27/python.exe  
> working\_dir: /cygdrive/c/abc/run/ab
> 
> sharefile\_script: /cygdrive/c/abc/run/ab/tests/sharefile\_api\_test.py  
> confl\_script: /cygdrive/c/abc/run/ab/tests/confluence\_api\_test.py
> 
> tasks:
> 
> - name: Place test script  
> win\_template:  
> src: templates/sharefiles\_confluence.template  
> dest: ~/sharefiles\_confluence.sh  
> owner: “{{ admin }}”  
> mode: u+rwx,g+rwx  
> ignore\_errors: yes
> 
> - name: Do a test run of Sharefiles and Confluence API scripts…  
> raw: ~/sharefiles\_confluence.sh  
> ignore\_errors: yes  
> register: result
> 
> - debug: var=result

NOTE: the “my\_\*” variables referenced in the following template are also stored in the vault file that’s pulled in, of course, at run time:

> [agenerette@build playbooks]$ cat templates/sharefiles\_confluence.template  
> #!/bin/bash
> 
> export ldap\_user=“{{ my\_ldap\_user }}”  
> export ldap\_pass=“{{ my\_ldap\_pass }}”  
> export ldap\_reg\_code=“null”  
> export gdoc\_pass=“{{ my\_gdoc\_pass }}”  
> export share\_user=“{{ my\_sharefiles\_user }}”  
> export share\_pass=“{{ my\_sharefiles\_pass }}”  
> export wiki\_user=“{{ my\_ldap\_user }}”  
> export wiki\_pass=“{{ my\_ldap\_pass }}”
> 
> echo $ldap\_user \> /tmp/sharefiles\_confl.log
> 
> “{{ python\_exec }} {{ sharefile\_script }} && {{ python\_exec }} {{ confl\_script }}” \>\> /tmp/sharefiles\_confl.log

And here’s a sample run, with output:

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [May 14, 2018, 7:54pm UTC](https://forum.ansible.com/t/best-way-to-run-python-scripts-against-managed-cygwin-nodes/27525/2 "2018-05-14T19:54:43Z")

</div>

I’m a bit confused, do you need to use Cygwin to run this script, everything would be so much simpler if you ignored Cygwin and used something like PowerShell or pure Python? For your current playbook there are a few issues I see;

- Windows modules, like win\_template, do not support the mode/owner options when it comes to files. Windows uses a more complex setup using ACLs which don’t map neatly with the traditional unix permissions
- You are connecting over WinRM (not SSH in cygwin), unix-isms like ~/ may work but not strictly Windows and may not work in all cases. The path to ~/ is not necessarily the same path when you are in cygwin, e.g. it’s probably going to be a path in the user directory
- The raw module invocation is expecting to be in a cygwin environment as you are calling a bash script, you are actually in a PowerShell environment and need to explicitly call C:\cygwin\bin\bash.exe (or wherever it is located) to execute the bash script in cygwin  
What I would do is get rid of the bash script and just execute the Python scripts you have directly in Windows.

Thanks

Jordan

---

<div class="post-metadata">

**Author:** ![Ab\_Generette](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Ab\_Generette](https://forum.ansible.com/u/Ab_Generette)\
**Post date:** [May 14, 2018, 8:50pm UTC](https://forum.ansible.com/t/best-way-to-run-python-scripts-against-managed-cygwin-nodes/27525/3 "2018-05-14T20:50:18Z")

</div>

Thanks, Jordan. Ok, I believe I see what you mean. I only went down this convoluted path involving the script and all, because of other problems that I ran into.

Changing that playbook to:

> * * *
> 
> - hosts: “{{ targets }}”
> 
> user: “{{ admin }}”
> 
> gather\_facts: yes
> 
> vars:
> 
> python\_exec: /cygdrive/c/Python27/python.exe
> 
> working\_dir: /cygdrive/c/t3/run/ae
> 
> sharefile\_script: /cygdrive/c/t3/run/ae/tests/sharefile\_api\_test.py
> 
> confl\_script: /cygdrive/c/t3/run/ae/tests/confluence\_api\_test.py
> 
> tasks:
> 
> - name: Do a test run of Sharefiles and Confluence API scripts…
> 
> raw: “{{ item }}”
> 
> 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

I get:

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [May 14, 2018, 9:40pm UTC](https://forum.ansible.com/t/best-way-to-run-python-scripts-against-managed-cygwin-nodes/27525/4 "2018-05-14T21:40:10Z")

</div>

Hmm, looks like your host vars isn’t matching which what I expect, it is actually connecting over SSH and not WinRM when your original post said you defined ansible\_connection: winrm. Is there a reason why you are using SSH/Cygwin on Windows, we don’t support it currently as a valid configuration because of issues/complications with the whole setup.

Thanks

Jordan

---

<div class="post-metadata">

**Author:** ![Ab\_Generette](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Ab\_Generette](https://forum.ansible.com/u/Ab_Generette)\
**Post date:** [May 15, 2018, 1:10pm UTC](https://forum.ansible.com/t/best-way-to-run-python-scripts-against-managed-cygwin-nodes/27525/5 "2018-05-15T13:10:52Z")

</div>

I’ve done a bit of switching, ssh to winrm and back, again, while troubleshooting this one.

There are just a number of things that have been getting run on these hosts, from their consoles, in Cygwin: things like runs of those two Python scripts. I’m trying just trying to automate that stuff. If I can get things to work over winrm, I’ll just go with that.

I’m going to drop the whole SSH/Cygwin thing, now. 'Will see if I can get this to work over WinRM.

---

<div class="post-metadata">

**Author:** ![Ab\_Generette](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@Ab\_Generette](https://forum.ansible.com/u/Ab_Generette)\
**Post date:** [May 15, 2018, 2:28pm UTC](https://forum.ansible.com/t/best-way-to-run-python-scripts-against-managed-cygwin-nodes/27525/6 "2018-05-15T14:28:46Z")

</div>

Since I’m no longer going to try and run this via a script, I’d like to close this topic and start another one.
