python_terraform : can't disable apply confirmation

30 Views Asked by At

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
1

There are 1 best solutions below

0
sofiane benhamza On

I figured it out. There is a small bug in terraform_python script. To solve it, just locate where terraform_python is installed. In Ubuntu you can use the command:

pip show python_terraform

after that, locate this line inside __init__.py:

default['auto-approve'] = (skip_plan == True)

replace with this:

default['auto_approve'] = (skip_plan == True)

Problem solved. Hope someone will find this useful.