python terraform apply auto approve not working

Viewed 3064

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

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).

Related