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

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.

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
         Racke

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.

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.

    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

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).

Regards
        Racke

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.

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:
  chdir: "/home/{{ username}}/project1/reports"

And even with delegate_to it will run on all hosts.

Regards
           Racke

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:

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
     Racke

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?