Jakim
Jakim

Reputation: 1813

Why Python script works differently when using ansible?

I have a simple ansible playbook which will call a shell script on a remote server, the shell script will call another python script, which will do something, when I run the ansible playbook, the script is not working, but when I ssh to the server and run the same command manually, it worked. I've done some debugging, seems when calling the python script, if I delete all the import statements from the python script, it works from ansible, but I don't understand why it works when I ssh to the server and would like to have some suggestion on how to resolve this issue.

the python script:

#!/usr/bin/python
import socket
import argparse
import logging
import subprocess
import time
import imp

def main():
    f = open('/afile', 'w')
    f.write('a test line')
    f.close()
if __name__ == '__main__':
    main()

those imports are not using here, it will be used in my real script, here I just write a line into a file for debugging.

The ansible playbooks are just simply like:

---
- hosts: servers

tasks:
  - name: trigger the script
    shell: /start.sh

The start.sh then simply invoke the python script:

#!/bin/sh
/start.py    

Upvotes: 4

Views: 535

Answers (1)

Jakim
Jakim

Reputation: 1813

sorry, it's my bad, I didn't put all the scripts here, seems that there is another script which has things like

#!/bin/sh
/start & >> stdout.log

this caused the problem, I guess the first three modules imported have things related to standard io, so the solution is using nohup.

again, very sorry for the incomplete question.

Upvotes: 1

Related Questions