Why Python script works differently when using ansible? - python

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

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.

Related

Python/Linux/Streamlit: executing subprocess for linux script

I'm a bit turned around on how to execute a shell script file in a Linux environment via Python's subprocess command in Streamlit. Any assistance on what I'm missing is appreciated.
I'm using a shell script called 0_texts.sh to run Pylanguagetool for a grammar check of one text file and return corrections in another text file, like so:
cd /home/user/dir/TXTs/
pylanguagetool text_0.txt > comments_0.txt
This script runs correctly in the Linux terminal, writing a comments_0.txt with appropriate grammar checks from text_0.txt.
I need to create a Python/Streamlit app that runs these shell scripts. In attempting to run this shell script, I've written script.py below:
import os
import subprocess
import sys
subprocess.run(['bash','/home/user/dir/Scripts/0_texts.sh'])
I then run script.py in Streamlit via the code below, keeping with Streamlit's documentation on using subprocess here.
import streamlit as st
import os
import subprocess
import sys
def app():
button1 = st.button("Click me")
if button1:
p = subprocess.run([f"{sys.executable}", "/home/user/dir/pages/script.py"])
st.write(p)
When I execute the script.py via Streamlit, the 0_txts.sh script executes, writing comments_0.txt in the correct directory and providing the following traceback: CompletedProcess(args=['/usr/bin/python3', '/home/user/dir/pages/script.py'], returncode=0). However, the comments_0.txt output contains the error input file is required, as if it can't properly access or read text_0.txt. I've tinkered around trying to find the problem, but have hit a brick wall.
Any suggestions on what I'm missing, or paths forward? Any help greatly appreciated.

Subprocess not run in python cgi script

I have issue while running subprocess in Python CGI script.
I am going to run python file as subprocess in python CGI script.
script.py
#!enable debugging
import cgitb
cgitb.enable()
print("Content-Type: text/html;charset=utf-8")
print()
import subprocess
p = subprocess.Popen(["sudo", "/usr/bin/python3", "test.py"], stdout=subprocess.PIPE)
test.py
f.open("test.txt", "a")
f.write("This is test")
f.close()
If I run script.py in console, it creates test.txt file successfully.
But If I run it on browser with Python CGI, it cannot create test.txt.
I thought, it can be caused by permission, so I tried to create test.txt in script.py directly, not on 'test.py', it is created successfully.
So, main issue is Python CGI script cannot run subprocess.
I cannot get any error while running on browser as Python CGI script.
How can I fix this issue?
Please if sudo does not work, look in the system log. There can be messages that help you with the debugging. The files are in /var/log and if you list by time ls -t you will see which ones changed just now.
First try without sudo. Make your file in a place where it does not need sudo permission like /tmp/test.txt. Then you know if the problem is sudo or something else.

How do I export environment variables using a script in the same shell in python?

My django project fetches credentials from environment variables, now I want to automate this process and store the credentials in the vault(hashivcorp).
I have a python and shell script which fetches data from an API and exports it as environment variables, when I run it using os.system command it runs the shell script but as it runs it in a subprocess, I can't access the variables in the main(parent) process/shell. Only way of doing it by inserting the shell script in the settings.py file.
Is there any way I can do it so that I get those in the main process?
P.s: I did try sourcing, os.system didn't recognise it as a command.
Here's the code I'm running:
import os
os.environ['ENV'] = 'Demo'
os.system('python3 /home/rishabh/export.py')
print(os.environ.get('RDS_DB_NAME'))
output:
None
the python file, shell script works just fine.
One way to do it is to run export.py in the same process, as user1934428 suggested:
import os
import sys
os.environ['ENV'] = 'Demo'
sys.path.append('/home/rishabh/')
import export # runs export.py in the same process
print(os.environ.get('RDS_DB_NAME'))
This assumes there are no __name__ == '__main__' checks inside export.py.
You only need the sys.path line if export.py is in a different directory than your current script.

python script not starting with etc/rc.local

I created a python script that I would like to execute at each startup. I modified the etc/rc.local, but I don't get the script to run.
etc/rc.local addition (I added the sleep thinking it may help):
(sleep 10; /usr/bin/python3 /home/pi/mower-gps-tracking/app/gps_logger.py)&
imports in the different python scripts (I don't know if it matters):
from ftplib import FTP
import os
import serial
import time
import threading
from gpiozero import LED, Button
When I start the etc/rc.local manually via a ssh command, it runs fine.
Any idea what I'm missing ?
Check if the script (rc.local) is executable ie. has the 'x' attribute set.
And you will probably need a
#!/bin/bash
as line #1 of your script.
Hope this helps.
And as per Barmar above, probably more approp in the Unix forums.
I tried this on my linux box no issues .. As a test create a script that will wrap your script, have it write to a tmp file so you can see if it actually runs. Use nohup and & in your scripts.
/etc/rc.local:
nohup /root/script.sh &
/root/script.sh:
#!/bin/bash
echo "I'm starting something .." > /tmp/startup_thing.log
/root/gps_logger.py
note: all scripts need to be chmod a+x file.ext. if it's not running check for the file /tmp/startup_thing.log . If it's there then you have some other issue. If it isn't then rc.local isn't working.

Why different behaviors between running Python module as an executable vs. Python [filename]

So, I created a simple python module, test.py
import commands
def main():
cmd = 'ls -l'
(status, output) = commands.getstatusoutput(cmd)
print status, output
if __name__ == '__main__':
main()
When I ran it using "Python test.py", I got the result that I expected. But when I ran it as an executable (yes, it has the 'x' permission), the program didn't respond at all and I had to Ctrl+C to quit it. Why is that? Shouldn't both ways give the same result?
Add a hash-bang line to the top:
#!/usr/bin/env python
import commands
...
This tells your system what interpreter to use to execute the script. Without it it doesn't know if it's a shell script, Perl script, Python script, what.
You need the hashbang to be the first line of your script, referencing the path of the Python interpreter. Otherwise, all the OS knows is that you're trying to execute a script, and it has no idea how to go about doing that.

Categories