我在使用
python_terraform
自动应用对 Terraform 配置的更改时遇到问题。具体来说,即使我在 auto_approve=True
函数调用中包含 terraform.apply()
标志,它仍然会提示我进行确认。下面是我的代码片段:
python
from python_terraform import Terraform
# Initialize Terraform
t = Terraform()
variables = {'a': 'b'}
print("Initializing Terraform...")
return_code, stdout, stderr = terraform.init()
if return_code == 0:
print("Terraform initialized successfully")
# Show state
terraform.show()
# Apply Terraform changes
print("Applying Terraform changes...")
return_code, stdout, stderr = terraform.apply(
auto_approve=True, # supposed to auto-approve but doesn't seem to work
var=variables,
lock=False,
capture_output=False,
input=False
)
if stdout and stderr:
print(stdout.decode('utf-8')) # Print stdout
print(stderr.decode('utf-8')) # Print stderr
print("done successfully")
但是,当我运行此脚本时,它仍然提示我确认以下消息:
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: #if i tap "yes" it works
我发现了,terraform_python 脚本中有一个小错误, 要解决这个问题,只需找到 terraform_python 的安装位置即可 在 ubuntu 中你可以使用以下命令:
pip show python_terraform
之后,在 init.py 中找到这一行: 默认['自动批准'] = (skip_plan == True) 替换为: 默认['auto_approve'] = (skip_plan == True) 问题解决了。希望有人会觉得这很有用