ansible-collections / cloud.terraform

The collection automates the management and provisioning of infrastructure as code (IaC) using the Terraform CLI tool within Ansible playbooks and Execution Environment runtimes.
GNU General Public License v3.0
100 stars 36 forks source link

Support Terraform Cloud with different workspaces #150

Open raspbeguy opened 5 months ago

raspbeguy commented 5 months ago
SUMMARY

As I understand there is two way to build an inventory using terraform states:

I didn't manage to use terraform_state, with the following configuration:

plugin: cloud.terraform.terraform_state
hostname: terraform.mydomain.example
backend_type: remote
backend_config:
  organization: my-org
  workspaces:
    name: my-workspace

For that reason I'm using Terraform Cloud with multiple workspaces. For now I'm successfully using terraform_provider. My terraform configuration looks like this:

terraform {
  cloud {
    hostname = "terraform.mydomain.examples"
    organization = "my-org"

    workspaces {
      tags = ["tag1", "tag2"]
    }
  }
  ...
}

I use tags instead of workspace parameter to enable using several workspaces.

The problem with terraform_provider is that I have to manually switch to the desired workspace by typing:

terraform workspace select myworkspace

I would rather have an option in terraform_provider to specify the workspace.

Another option would be to make terraform_state work with the remote option.

ISSUE TYPE
COMPONENT NAME
gravesm commented 5 months ago

What is the error message you are receiving when using the terraform_state plugin? Also, what version of the cloud.terraform collection and Ansible are you using?

raspbeguy commented 5 months ago

I'm using master version on the plugins.

I have got this error:

[WARNING]:  * Failed to parse /home/ggodfroy/repo/metrics-infra/ansible/environments/preprod-test/terraform_state.yml with auto plugin: Command '['tofu', 'init', '-input=false', '-no-color', '-backend-config',
'hostname=terraform.mydomain.examples', '-backend-config', 'organization=my-org', '-backend-config', "workspaces={'name': 'my-workspace'}"]' returned non-zero exit status 1.

When I execute this tofu command myself I get this error:

OpenTofu initialized in an empty directory!

The directory has no OpenTofu configuration files. You may begin working
with OpenTofu immediately by creating OpenTofu configuration files.

I guess there is no way to change directory in terraform_state. Also I don't know why I functionally would need some terraform/tofu config because I normally provided everything the plugin needs to fetch states.

Same error with tofu and terraform btw.