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

2,254 views
Skip to first unread message

aditya govindaraju

unread,
May 27, 2020, 6:31:40 AM5/27/20
to Ansible Project
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



 

Stefan Hornburg (Racke)

unread,
May 27, 2020, 7:07:52 AM5/27/20
to ansible...@googlegroups.com
Mind to share the error message? Is Python installed on switch1?

Regards
Racke

>
>
>  
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/2940a20f-948b-4e21-8915-16112093d0f7%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/2940a20f-948b-4e21-8915-16112093d0f7%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration. Provisioning with Ansible.

signature.asc

aditya govindaraju

unread,
May 27, 2020, 7:20:23 AM5/27/20
to Ansible Project
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.

Stefan Hornburg (Racke)

unread,
May 27, 2020, 7:27:07 AM5/27/20
to ansible...@googlegroups.com
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

>
>
>  
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/17930f1c-c4b2-47f4-abe6-11a2135c404a%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/17930f1c-c4b2-47f4-abe6-11a2135c404a%40googlegroups.com?utm_medium=email&utm_source=footer>.
signature.asc

aditya govindaraju

unread,
May 27, 2020, 8:43:59 AM5/27/20
to Ansible Project
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.

On Wednesday, May 27, 2020 at 12:31:40 PM UTC+2, aditya govindaraju wrote:

Stefan Hornburg (Racke)

unread,
May 27, 2020, 8:51:05 AM5/27/20
to ansible...@googlegroups.com
On 5/27/20 2:43 PM, aditya govindaraju wrote:
> 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
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/0176c2ed-32aa-4c91-a788-67c608a7f173%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/0176c2ed-32aa-4c91-a788-67c608a7f173%40googlegroups.com?utm_medium=email&utm_source=footer>.
signature.asc

aditya govindaraju

unread,
May 28, 2020, 3:37:55 PM5/28/20
to Ansible Project
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.

On Wednesday, May 27, 2020 at 12:31:40 PM UTC+2, aditya govindaraju wrote:

Stefan Hornburg (Racke)

unread,
May 28, 2020, 3:46:49 PM5/28/20
to ansible...@googlegroups.com
Again, {{ username }} is not evaluated. Put it into "":

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

Regards
Racke

>
> On Wednesday, May 27, 2020 at 12:31:40 PM UTC+2, aditya govindaraju wrote:
>
> 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
>
>
>
>  
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/83c38126-78d3-44e7-96e6-dcc37fd66de4%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/83c38126-78d3-44e7-96e6-dcc37fd66de4%40googlegroups.com?utm_medium=email&utm_source=footer>.
signature.asc

aditya govindaraju

unread,
May 28, 2020, 4:21:00 PM5/28/20
to Ansible Project
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?

On Wednesday, May 27, 2020 at 12:31:40 PM UTC+2, aditya govindaraju wrote:
Reply all
Reply to author
Forward
0 new messages