I'm encountering an issue while using python_terraform to apply changes to my Terraform configuration automatically. Specifically, even when I include the auto_approve=True flag in the terraform.apply() function call, it still prompts me for confirmation. Below is my code snippet:
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")
However, when I run this script, it still prompts me for confirmation with the message:
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
I figured it out. There is a small bug in
terraform_pythonscript. To solve it, just locate whereterraform_pythonis installed. In Ubuntu you can use the command:after that, locate this line inside __init__.py:
replace with this:
Problem solved. Hope someone will find this useful.