Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python terraform apply auto approve not working

I have a python code that performs terraform plan and apply.

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

The PLAN part works correctly however the apply part is waiting for a 'yes', even though I have specified {auto-approve: True}. For information am using python 3.7 and python-terraform module 0.10.1

The apply part hangs and if i exit the script i get the following output:

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

Thank you

like image 841
Kheshav Sewnundun Avatar asked Jul 03 '19 10:07

Kheshav Sewnundun


People also ask

What happens if terraform apply fails?

After applying, the run proceeds automatically to completion. If the apply succeeded, the run ends in the Applied state. If the apply failed, the run ends in the Apply Errored state.

What is auto approve in terraform?

-auto-approve - Skips interactive approval of plan before applying. This option is ignored when you pass a previously-saved plan file, because Terraform considers you passing the plan file as the approval and so will never prompt in that case.

What action does the terraform apply command perform?

The terraform apply command performs a plan just like terraform plan does, but then actually carries out the planned changes to each resource using the relevant infrastructure provider's API. It asks for confirmation from the user before making any changes, unless it was explicitly told to skip approval.


1 Answers

Figured out how to make that work without the prompt to type in yes. using the latest 0.10.1 version of python-terraform.

tf = Terraform(working_dir='my/path')

tf.init()
tf.plan()
tf.apply(skip_plan=True)

# then later
tf.destroy()

seems like the auto_approve variable is tied to skip_plan (see here).

like image 71
Jawad Avatar answered Oct 13 '22 19:10

Jawad