简体   繁体   中英

Convert Command To Subprocess

I currently have the following:

tf = Terraform()
tf.init()
tf.plan(capture_output=False)
approve = {"auto-approve": True}
print(tf.apply(capture_output=False, auto_approve=True, skip_plan=True,**approve))

This works quite well, but I need to put it into a subprocess. I've tried the following:

tf = Terraform()
tf.init()
tf.plan(capture_output=False)
approve = {"auto-approve": True}
subprocess.check_output(['tf.apply','capture_output','0', 'auto_approve','1', 'skip_plan','1','**approve'])

However, I receive the error: "'tf.apply' is not recognised as an internal or external command, operable program or batch file."

Can I convert the above into a subprocess? And if so, how?

subprocess executes external commands. By contrast, you are trying to execute something like (but not quite) Python code. This fundamentally can't work.

What you can do is create a Python script that contains your Terraform code and execute that via subprocess.check_output . However, this only works if the Python script is complete . You can't jus execute a fragment as in your case.

For a minimal example of what you can do, take your original script, save it as tf.py and execute it like this in Python:

subprocess.check_output(['python', 'tf.py'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM