python-terraform icon indicating copy to clipboard operation
python-terraform copied to clipboard

Problem when using inside IDE

Open mateuszzz88 opened this issue 4 years ago • 3 comments

Hello, I have python script that successfully runs from console

  • I did az login
  • I have main.tf
  • scripts does create resources and then destroys them.
  • and it all works.

However when run from IDE (in my case JetBrains clion) I get errors like this:

Error: Error building account: Error getting authenticated object ID: Error parsing json result from the Azure CLI: Error unmarshaling the result of Azure CLI: invalid character '\x1b' after top-level value

  on main.tf line 1, in provider "azurerm":
   1: provider "azurerm" {
(...)

WARNING:python_terraform:error: b"\x1b[31mThe output variable requested could not be found in the state\nfile. If you recently added this to your configuration, be\nsure to run `terraform apply`, since the state won't be updated\nwith new output variables until that command is run.\x1b[0m\x1b[0m\n"

Do you have an idea what am I doing wrong?

mateuszzz88 avatar Dec 23 '20 14:12 mateuszzz88

more concretely first part of error stems from

    t = Terraform(working_dir=working_dir)
    t.init('./')

    applyVariables = {
        'system': system,
        'disk_size': disk_size,
        'disk_type': disk_type,
        'tag': tag,
        'vm_count': vm_count,
        'vm_size': vm_size,
        'username': username,
        'allowed_connection_ip': allowed_connection_ip,
        'enable_firewall': enable_firewall,
        'ssh_pubkey': ssh_pubkey
    }
    print(applyVariables)

    print('############ Applying... ############')
    _long_procedure_message()
    ret_code, stdout, stderr = t.apply('./', no_color=IsFlagged, skip_plan=True, var=applyVariables, capture_output=False)

The warning comes from

returnValue = t.output('vm_network_values', full_value=True)

mateuszzz88 avatar Dec 23 '20 14:12 mateuszzz88

I still have this problem, I reproduce it very reliably on pycharm and clion both, on two different computers.

  • enabling/disabling capture_output doesn't help
  • enabling/disabling no_color doesn't help either
  • executing inside my script (in IDE) subprocess.run(['terraform','plan']) has the same problem, so probably this is not library's bug.

I think it "should" be caused by some env flag set in IDE, but I couldn't reproduce it in console by replicating environment.

mateuszzz88 avatar Jan 19 '21 18:01 mateuszzz88

I had the same problem running terraform plan under the Azure CLI. I found it can be resolved by setting an environment variable in the PyCharm Run Configuration as follows: AZURE_CORE_NO_COLOR=1

jon-dennis avatar Mar 18 '21 11:03 jon-dennis