# how to execute python script using ansible, python script running on different sub-directory under same project folder

**URL:** <https://forum.ansible.com/t/how-to-execute-python-script-using-ansible-python-script-running-on-different-sub-directory-under-same-project-folder/33030>\
**Category:** Ansible Project\
**Created:** [May 27, 2020, 10:31am UTC](https://forum.ansible.com/t/how-to-execute-python-script-using-ansible-python-script-running-on-different-sub-directory-under-same-project-folder/33030 "2020-05-27T10:31:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![aditya\_govindaraju](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@aditya\_govindaraju](https://forum.ansible.com/u/aditya_govindaraju)\
**Post date:** [May 27, 2020, 10:31am UTC](https://forum.ansible.com/t/how-to-execute-python-script-using-ansible-python-script-running-on-different-sub-directory-under-same-project-folder/33030/1 "2020-05-27T10:31:40Z")

</div>

Am trying to write a playbook in order to execute a python script running on a different subdirectory under the same project folder

A python script running on - /home/{username}/project1/reports/test1.py

And playbook running on - /home/{username}/project1/test1.yml

I need to execute a python script using the playbook. And playbook looks like this.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [May 27, 2020, 11:07am UTC](https://forum.ansible.com/t/how-to-execute-python-script-using-ansible-python-script-running-on-different-sub-directory-under-same-project-folder/33030/2 "2020-05-27T11:07:20Z")

</div>

> Am trying to write a playbook in order to execute a python script running on a different subdirectory under the same  
> project folder
> 
> A python script running on - /home/{username}/project1/reports/test1.py
> 
> And playbook running on - /home/{username}/project1/test1.yml
> 
> I need to execute a python script using the playbook. And playbook looks like this.
> 
> ---  
> - hosts: switch1  
> vars:  
> ansible\_connection: network\_cli  
> ansible\_network\_os: nxos  
> ansible\_ssh\_user: '{{ username }}'  
> ansible\_ssh\_pass: '{{ password }}'  
> tasks:  
> - name: Execute the python script  
> command: python test1.py  
> args:  
> chdir: /home/{username}/project1/reports
> 
> But it's failing to execute the script. Please suggest me if am missing anything

Mind to share the error message? Is Python installed on switch1?

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![aditya\_govindaraju](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@aditya\_govindaraju](https://forum.ansible.com/u/aditya_govindaraju)\
**Post date:** [May 27, 2020, 11:20am UTC](https://forum.ansible.com/t/how-to-execute-python-script-using-ansible-python-script-running-on-different-sub-directory-under-same-project-folder/33030/3 "2020-05-27T11:20:23Z")

</div>

Please find the below error message

The full traceback is:  
WARNING: The below traceback may _not_ be related to the actual failure.  
File “/tmp/ansible\_command\_payload\_4s7z5o/ansible\_command\_payload.zip/ansible/modules/commands/command.py”, line 278, in main  
fatal: [switch1]: FAILED! =\> {  
“changed”: false,  
“invocation”: {  
“module\_args”: {  
“\_raw\_params”: “python test1.py”,  
“\_uses\_shell”: false,  
“argv”: null,  
“chdir”: “/home/{username}/project1/reports/”,  
“creates”: null,  
“executable”: null,  
“removes”: null,  
“stdin”: null,  
“stdin\_add\_newline”: true,  
“strip\_empty\_ends”: true,  
“warn”: true

Python is running on ansible control node and it is trying to fetch the compliance report of switch1 and the python script work individually. But unable to execute the same using playbook.

Please suggest.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [May 27, 2020, 11:26am UTC](https://forum.ansible.com/t/how-to-execute-python-script-using-ansible-python-script-running-on-different-sub-directory-under-same-project-folder/33030/4 "2020-05-27T11:26:31Z")

</div>

> Please find the below error message
> 
> The full traceback is:  
> WARNING: The below traceback may \*not\* be related to the actual failure.  
> File "/tmp/ansible\_command\_payload\_4s7z5o/ansible\_command\_payload.zip/ansible/modules/commands/command.py", line 278,  
> in main  
> fatal: [switch1]: FAILED! =\> {  
> "changed": false,  
> "invocation": {  
> "module\_args": {  
> "\_raw\_params": "python test1.py",  
> "\_uses\_shell": false,  
> "argv": null,  
> "chdir": "/home/{username}/project1/reports/",  
> "creates": null,  
> "executable": null,  
> "removes": null,  
> "stdin": null,  
> "stdin\_add\_newline": true,  
> "strip\_empty\_ends": true,  
> "warn": true
> 
> Python is running on ansible control node and it is trying to fetch the compliance report of switch1 and the python  
> script work individually. But unable to execute the same using playbook.
> 
> Please suggest.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;Am trying to write a playbook in order to execute a python script running on a different subdirectory under the same  
> &nbsp;&nbsp;&nbsp;&nbsp;project folder
> 
> &nbsp;&nbsp;&nbsp;&nbsp;A python script running on - /home/{username}/project1/reports/test1.py
> 
> &nbsp;&nbsp;&nbsp;&nbsp;And playbook running on - /home/{username}/project1/test1.yml
> 
> &nbsp;&nbsp;&nbsp;&nbsp;I need to execute a python script using the playbook. And playbook looks like this.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;---  
> &nbsp;&nbsp;&nbsp;&nbsp;- hosts: switch1  
> &nbsp;&nbsp;&nbsp;&nbsp; vars:  
> &nbsp;&nbsp;&nbsp;&nbsp; ansible\_connection: network\_cli  
> &nbsp;&nbsp;&nbsp;&nbsp; ansible\_network\_os: nxos  
> &nbsp;&nbsp;&nbsp;&nbsp; ansible\_ssh\_user: '{{ username }}'  
> &nbsp;&nbsp;&nbsp;&nbsp; ansible\_ssh\_pass: '{{ password }}'  
> &nbsp;&nbsp;&nbsp;&nbsp; tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp; - name: Execute the python script  
> &nbsp;&nbsp;&nbsp;&nbsp; command: python test1.py  
> &nbsp;&nbsp;&nbsp;&nbsp; args:  
> &nbsp;&nbsp;&nbsp;&nbsp; chdir: /home/{username}/project1/reports
> 
> &nbsp;&nbsp;&nbsp;&nbsp;But it's failing to execute the script. Please suggest me if am missing anything

Well, you need to tell Ansible that the script should be running on the controller, e.g. with delegate\_to: localhost  
(see [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_delegation.html#delegation](https://docs.ansible.com/ansible/latest/user_guide/playbooks_delegation.html#delegation)).

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![aditya\_govindaraju](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@aditya\_govindaraju](https://forum.ansible.com/u/aditya_govindaraju)\
**Post date:** [May 27, 2020, 12:43pm UTC](https://forum.ansible.com/t/how-to-execute-python-script-using-ansible-python-script-running-on-different-sub-directory-under-same-project-folder/33030/5 "2020-05-27T12:43:59Z")

</div>

After i added delegate\_to:localhost, playbook ran successful but ‘ignored’ with no output.

Output error:

fatal: [switch1 → localhost]: FAILED! =\> {  
“changed”: false,  
“invocation”: {  
“module\_args”: {  
“\_raw\_params”: “python test1.py”,  
“\_uses\_shell”: false,  
“argv”: null,  
“chdir”: “/home/{username}/project1/reports/”,  
“creates”: null,  
“executable”: null,  
“removes”: null,  
“stdin”: null,  
“stdin\_add\_newline”: true,  
“strip\_empty\_ends”: true,  
“warn”: true  
}…

Output status:  
“status”: “successful”,  
“traceback”: “”,  
“inventory”: “switch1”,  
“project”: “project1”,  
“playbook”: “test1.yml”,  
“credential”: null,  
“limit”: “”,  
“extra\_vars”: “{"username": "user1", "password": "$encrypted$"}”,  
“hosts”: {  
“switch1”: {  
“failed”: false,  
“changed”: 0,  
“dark”: 0,  
“failures”: 0,  
“ok”: 2,  
“processed”: 1,  
“skipped”: 0,  
“rescued”: 0,  
“ignored”: 1

I think we can not use localhost, become switch1 is a remote device and once this playbook is successful, Similarly i need to run on more than 30 devices. Please suggest.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [May 27, 2020, 12:50pm UTC](https://forum.ansible.com/t/how-to-execute-python-script-using-ansible-python-script-running-on-different-sub-directory-under-same-project-folder/33030/6 "2020-05-27T12:50:32Z")

</div>

> After i added delegate\_to:localhost, playbook ran successful but 'ignored' with no output.
> 
> Output error:
> 
> fatal: [switch1 -\> localhost]: FAILED! =\> {  
> "changed": false,  
> "invocation": {  
> "module\_args": {  
> "\_raw\_params": "python test1.py",  
> "\_uses\_shell": false,  
> "argv": null,  
> "chdir": "/home/{username}/project1/reports/",  
> "creates": null,  
> "executable": null,  
> "removes": null,  
> "stdin": null,  
> "stdin\_add\_newline": true,  
> "strip\_empty\_ends": true,  
> "warn": true  
> }…

The username isn't interpolated, try

args:  
&nbsp;&nbsp;chdir: "/home/{{ username}}/project1/reports"

And even with delegate\_to it will run on all hosts.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![aditya\_govindaraju](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@aditya\_govindaraju](https://forum.ansible.com/u/aditya_govindaraju)\
**Post date:** [May 28, 2020, 7:37pm UTC](https://forum.ansible.com/t/how-to-execute-python-script-using-ansible-python-script-running-on-different-sub-directory-under-same-project-folder/33030/7 "2020-05-28T19:37:54Z")

</div>

The issue still there, so I have moved the playbook file to the same directory where python script is running ex: /reports/

So both playbook and python script is in the same folder. But still, I get a couple of errors.

Below is my playbook:

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [May 28, 2020, 7:45pm UTC](https://forum.ansible.com/t/how-to-execute-python-script-using-ansible-python-script-running-on-different-sub-directory-under-same-project-folder/33030/8 "2020-05-28T19:45:44Z")

</div>

> The issue still there, so I have moved the playbook file to the same directory where python script is running ex: /reports/
> 
> So both playbook and python script is in the same folder. But still, I get a couple of errors.
> 
> Below is my playbook:  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> ---  
> - hosts: switch1  
> vars:  
> ansible\_connection: network\_cli  
> ansible\_network\_os: nxos  
> ansible\_ssh\_user: '{{ username }}'  
> ansible\_ssh\_pass: '{{ password }}'  
> tasks:  
> - name: Execute the test1 script  
> command: python /home/{{ username }}/project1/reports/test1.py --- python script running on same folder as yaml  
> playbook  
> delegate\_to:  
> args:  
> chdir: /home/{{ username }}/project1/reports/  
> ignore\_errors: False
> 
> I get a couple of error if I run the playbook with commenting (#) args: chdir: then i get.
> 
> fatal: [switch1]: FAILED! =\> {"changed": true, "cmd": ["python", "/home/{{ username }}/project1/reports/test1.py"],  
> "delta": "0:00:00.035644", "end": "2020-05-28 19:04:00.583497", "msg": "non-zero return code", "rc": 2, "start":  
> "2020-05-28 19:04:00.547853", "stderr": "python: can't open file
> 
> If i uncomment(remove # on those lines) the args: chdir: then i get.
> 
> fatal: [switch1]: FAILED! =\> {"changed": false, "msg": "Unable to change directory before execution: [Errno 2] No such  
> file or directory: '/home/{{ username }}/project1/reports/'"}
> 
> Python - unable to identify the path to execute the test1.py that i finally understood. so any permission to the user to  
> execute the same or do i need to move the playbook to back to original directory, so the python script is under  
> subdirectory.
> 
> Please suggest. if am missing any logic here.

Again, {{ username }} is not evaluated. Put it into "":

command: "python /home/{{ username }}/project1/reports/test1.py"  
chdir: "/home/{{ username }}/project1/reports/"

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![aditya\_govindaraju](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@aditya\_govindaraju](https://forum.ansible.com/u/aditya_govindaraju)\
**Post date:** [May 28, 2020, 8:20pm UTC](https://forum.ansible.com/t/how-to-execute-python-script-using-ansible-python-script-running-on-different-sub-directory-under-same-project-folder/33030/9 "2020-05-28T20:20:59Z")

</div>

Am still getting

fatal: [switch1]: FAILED! =\> {“changed”: false, “msg”: “Unable to change directory before execution: [Errno 2] No such file or directory: ‘/home/{{ username }}/project1/reports/’'”}

Any permission issue to access folder to be checked?
