我有执行terraform计划并应用的python代码。
from python_terraform import *
class terraform(object):
@staticmethod
def execute(action):
print(action)
tf = Terraform(
working_dir='/Users/kheshav/PROJECTS/terraform/demo_supinfo/tf_files')
approve = {"auto-approve": True}
if action is "PLAN":
"""
return_code, stdout, stderr = tf.plan(
capture_output=True)
"""
tf.plan(no_color=IsFlagged, refresh=False, capture_output=True)
return_code, stdout, stderr = tf.plan()
print(stdout)
elif action == "APPLY":
return_code, stdout, stderr = tf.apply(
capture_output=True, auto_approve=True, **approve)
elif action == "DESTROY":
return_code, stdout, stderr = tf.destroy(
capture_output=True, auto_approve=True, **approve)
elif action == "OUTPUT":
stdout = tf.output(
capture_output=True)
return stdout
尽管我指定了{auto-approve: True}
,但是PLAN部分正常工作,但是应用部分正在等待“是”。有关信息,请使用python 3.7和python-terraform模块0.10.1]
应用部分挂起,如果我退出脚本,则会得到以下输出:
Traceback (most recent call last):
File "main.py", line 85, in <module>
handle_command(command, channel)
File "main.py", line 23, in handle_command
response = terraform.execute("APPLY")
File "/Users/kheshav/PROJECTS/terraform/demo_supinfo/chatbot/terraform.py", line 26, in execute
print("APPLYING")
File "/Users/kheshav/.pyenv/versions/demo_supinfo/lib/python3.7/site-packages/python_terraform/__init__.py", line 113, in apply
return self.cmd('apply', *args, **option_dict)
File "/Users/kheshav/.pyenv/versions/demo_supinfo/lib/python3.7/site-packages/python_terraform/__init__.py", line 299, in cmd
out, err = p.communicate()
File "/Users/kheshav/.pyenv/versions/3.7.3/lib/python3.7/subprocess.py", line 939, in communicate
stdout, stderr = self._communicate(input, endtime, timeout)
File "/Users/kheshav/.pyenv/versions/3.7.3/lib/python3.7/subprocess.py", line 1681, in _communicate
ready = selector.select(timeout)
File "/Users/kheshav/.pyenv/versions/3.7.3/lib/python3.7/selectors.py", line 415, in select
fd_event_list = self._selector.poll(timeout)
KeyboardInterrupt
谢谢
弄清楚了如何在不提示输入yes的情况下使它起作用。使用最新的0.10.1版本的python-terraform。
tf = Terraform(working_dir='my/path')
tf.init()
tf.plan()
tf.apply(skip_plan=True)
# then later
tf.destroy()
似乎好像auto_approve变量绑定到skip_plan(see here)。