# How to run a localhost python script in a play defined for a different host group?

**URL:** <https://forum.ansible.com/t/how-to-run-a-localhost-python-script-in-a-play-defined-for-a-different-host-group/29269>\
**Category:** Ansible Project\
**Created:** [December 15, 2018, 12:18pm UTC](https://forum.ansible.com/t/how-to-run-a-localhost-python-script-in-a-play-defined-for-a-different-host-group/29269 "2018-12-15T12:18:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ankit2](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@Ankit2](https://forum.ansible.com/u/Ankit2)\
**Post date:** [December 15, 2018, 12:18pm UTC](https://forum.ansible.com/t/how-to-run-a-localhost-python-script-in-a-play-defined-for-a-different-host-group/29269/1 "2018-12-15T12:18:49Z")

</div>

I have got the following playbook:

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 15, 2018, 12:23pm UTC](https://forum.ansible.com/t/how-to-run-a-localhost-python-script-in-a-play-defined-for-a-different-host-group/29269/2 "2018-12-15T12:23:17Z")

</div>

Add delegate\_to: localhost on the task(s) you need to run on localhost and not the remote host.

---

<div class="post-metadata">

**Author:** ![Ankit2](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@Ankit2](https://forum.ansible.com/u/Ankit2)\
**Post date:** [December 15, 2018, 12:34pm UTC](https://forum.ansible.com/t/how-to-run-a-localhost-python-script-in-a-play-defined-for-a-different-host-group/29269/3 "2018-12-15T12:34:09Z")

</div>

Thanks a lot Kai for the quick help. I will try this.

---

<div class="post-metadata">

**Author:** ![Ankit2](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@Ankit2](https://forum.ansible.com/u/Ankit2)\
**Post date:** [December 15, 2018, 12:37pm UTC](https://forum.ansible.com/t/how-to-run-a-localhost-python-script-in-a-play-defined-for-a-different-host-group/29269/4 "2018-12-15T12:37:17Z")

</div>

I have one more query though. What is the exact different between local\_action **and delegate\_to ?**

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 15, 2018, 1:18pm UTC](https://forum.ansible.com/t/how-to-run-a-localhost-python-script-in-a-play-defined-for-a-different-host-group/29269/5 "2018-12-15T13:18:36Z")

</div>

Nothing.

I prefer using delegate\_to since it's then easy to add or remove the delegation.  
If using local\_action more change need to be done to add or remove it.

---

<div class="post-metadata">

**Author:** ![Ankit2](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@Ankit2](https://forum.ansible.com/u/Ankit2)\
**Post date:** [December 15, 2018, 2:33pm UTC](https://forum.ansible.com/t/how-to-run-a-localhost-python-script-in-a-play-defined-for-a-different-host-group/29269/6 "2018-12-15T14:33:57Z")

</div>

I tried the following modified playbook:

**- hosts: Windows**  
**gather\_facts: false**  
**tasks:**  
**- name: Ensure Service status is running and startup mode set to Automatic**  
**win\_service:**  
**name: MSSQLSERVER**  
**state: started**  
**start\_mode: auto**

**- name: Send Job completion notification to MS Teams**  
**script: /tmp/ansible\_scripts/MSTeamsNotifications.pyc**  
**args:**  
**executable: /usr/bin/python**  
**delegate\_to: 127.0.0.1**  
**…**

but getting the following error:

TASK [Send Job completion notification to MS Teams] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
19:57:29

9  
fatal: [WinTest4143 → 127.0.0.1]: FAILED! =\> {“changed”: false, “msg”: “Could not find or access ‘/tmp/ansible\_scripts/MSTeamsNotifications.pyc’ on the Ansible Controller.\nIf you are using a module and expect the file to exist on the remote, see the remote\_src option”}

this file exists on Ansible Server and works fine. The file has full permissions too but it doesn’t work.

**$ ls -lrt /tmp/ansible\_scripts/MSTeamsNotifications.pyc**  
**-rwxrwxrwx. 1 anks users 1043 Dec 15 07:21 /tmp/ansible\_scripts/MSTeamsNotifications.pyc**

**$ /usr/bin/python /tmp/ansible\_scripts/MSTeamsNotifications.pyc**

**MSTeamsNotifications.py - Send Notifications to MS Teams Team**  
**1**

**Thanks and Regards,**

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 15, 2018, 3:16pm UTC](https://forum.ansible.com/t/how-to-run-a-localhost-python-script-in-a-play-defined-for-a-different-host-group/29269/7 "2018-12-15T15:16:48Z")

</div>

Recommend using localhost and not 127.0.0.1, localhost is special in Ansible and does the right ting.

The script module copies the file to the remote host and execute the script, so in this case you should use the command module instead and just run the script/program since it's already on the Ansible controller.

---

<div class="post-metadata">

**Author:** ![Ankit2](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@Ankit2](https://forum.ansible.com/u/Ankit2)\
**Post date:** [December 15, 2018, 5:18pm UTC](https://forum.ansible.com/t/how-to-run-a-localhost-python-script-in-a-play-defined-for-a-different-host-group/29269/8 "2018-12-15T17:18:56Z")

</div>

Hello Kai,

Unfortunately, I am still getting the error. The current Playbook:

**- hosts: Windows**  
**gather\_facts: false**  
**tasks:**

**- name: Ensure Service status is running and startup mode set to Automatic**  
**win\_service:**  
**name: MSSQLSERVER**  
**state: started**  
**start\_mode: auto**

**- name: Send Job completion notification to MS Teams**  
**command: ‘/usr/bin/python /tmp/ansiblescripts/MSTeamsNotifications.pyc’**  
**delegate\_to: localhost**

**Result:**

TASK [Send Job completion notification to MS Teams] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /var/lib/awx/projects/\_6\_\_innersource\_cio\_config\_mgmt/playbooks/mssql/SQLE\_Service\_Status.yml:15  
 ESTABLISH LOCAL CONNECTION FOR USER: awx  
 EXEC /bin/sh -c ‘echo ~awx && sleep 0’  
 EXEC /bin/sh -c ‘( umask 77 && mkdir -p “`echo /var/lib/awx/.ansible/tmp/ansible-tmp-1544892619.83-135217386386436`” && echo ansible-tmp-1544892619.83-135217386386436=“`echo /var/lib/awx/.ansible/tmp/ansible-tmp-1544892619.83-135217386386436`” ) && sleep 0’  
Using module file /var/lib/awx/venv/custom-venv/lib/python2.7/site-packages/ansible/modules/commands/command.py  
 PUT /var/lib/awx/.ansible/tmp/ansible-local-3LdqDbo/tmpLJ0CAV TO /var/lib/awx/.ansible/tmp/ansible-tmp-1544892619.83-135217386386436/AnsiballZ\_command.py  
 EXEC /bin/sh -c ‘chmod u+x /var/lib/awx/.ansible/tmp/ansible-tmp-1544892619.83-135217386386436/ /var/lib/awx/.ansible/tmp/ansible-tmp-1544892619.83-135217386386436/AnsiballZ\_command.py && sleep 0’  
 EXEC /bin/sh -c ‘/var/lib/awx/venv/custom-venv/bin/python /var/lib/awx/.ansible/tmp/ansible-tmp-1544892619.83-135217386386436/AnsiballZ\_command.py && sleep 0’  
 EXEC /bin/sh -c ‘rm -f -r /var/lib/awx/.ansible/tmp/ansible-tmp-1544892619.83-135217386386436/ \> /dev/null 2\>&1 && sleep 0’  
fatal: [WinTest4143-\> localhost]: FAILED! =\> {  
“changed”: true,  
“cmd”: [  
“/usr/bin/python”,  
“/tmp/ansiblescripts/MSTeamsNotifications.pyc”  
],  
“delta”: “0:00:00.022198”,  
“end”: “2018-12-15 16:50:20.371504”,  
“invocation”: {  
“module\_args”: {  
“\_raw\_params”: “/usr/bin/python /tmp/ansiblescripts/MSTeamsNotifications.pyc”,  
“\_uses\_shell”: false,  
“argv”: null,  
“chdir”: null,  
“creates”: null,  
“executable”: null,  
“removes”: null,  
“stdin”: null,  
“warn”: true  
}  
},  
“msg”: “non-zero return code”,  
“rc”: 2,  
“start”: “2018-12-15 16:50:20.349306”,  
“stderr”: “/usr/bin/python: can’t open file ‘/tmp/ansiblescripts/MSTeamsNotifications.pyc’: [Errno 2] No such file or directory”,  
“stderr\_lines”: [  
“/usr/bin/python: can’t open file ‘/tmp/ansiblescripts/MSTeamsNotifications.pyc’: [Errno 2] No such file or directory”  
],  
“stdout”: “”,  
“stdout\_lines”:   
}

**Another thing i noticed is, when i try to sudo to awx user, i get a expired password error. What is the default password for awx user. I don’t remember setting it up anywhere during installation of Tower.**

**# sudo su - awx**  
**sudo: Account or password is expired, reset your password and try again**  
**Changing password for root.**  
**(current) UNIX password:**

---

<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:** [December 15, 2018, 8:00pm UTC](https://forum.ansible.com/t/how-to-run-a-localhost-python-script-in-a-play-defined-for-a-different-host-group/29269/9 "2018-12-15T20:00:47Z")

</div>

If you are seeing this issue in AWX you are probably coming across bubble wrap [https://docs.ansible.com/ansible-tower/3.1.4/html/administration/proot\_func\_variables.html](https://docs.ansible.com/ansible-tower/3.1.4/html/administration/proot_func_variables.html). Bubblewrap limits what directories a playbook can run and IIRC it’s default is the project directory of the playbook itself. I recommend keeping that script as part of the playbook got repo in the files directory and referencing it by name.
