ansible / terraform-provider-ansible

community terraform provider for ansible
https://registry.terraform.io/providers/ansible/ansible/latest
GNU General Public License v3.0
183 stars 42 forks source link

Support for Terragrunt #95

Closed EdanBrooke closed 4 months ago

EdanBrooke commented 4 months ago

Hi,

I would like to use this plugin to dynamically build our Ansible inventories. I got this working with Terraform on its own, but we're now looking to use Terragrunt with HTTP (GitLab) remote state. This doesn't seem to be possible at the moment as specifying the path to the a Terragrunt directory doesn't yield a backend configuration or a Terraform state file:

.
├── common
│   └── azurerm
│       └── core.hcl
├── lab
│   ├── azurerm
│   │   └── core
│   │       └── terragrunt.hcl
│   └── env.hcl
└── tst
    └── env.hcl
---
plugin: cloud.terraform.terraform_provider
state_file: "" # Use remote Terraform state by specifying empty string, issue #52 
project_path: ../../lab/azurerm/core
search_child_modules: True

Is there a particular place I should post in the community to discuss this?

EdanBrooke commented 4 months ago

Hello,

The below works, specifying the Terraform binary as Terragrunt.

---
plugin: cloud.terraform.terraform_provider
binary_path: "/home/linuxbrew/.linuxbrew/bin/terragrunt" # Use Terragrunt wrapper
project_path: "../terraform/environments/lab/azurerm/core"
search_child_modules: True

Of course, for this to work you'd have to create a separate YAML file inside your inventory directory for each Terragrunt directory that yields Ansible Terraform provider resources. I'm OK with this personally.

However, specifying only terragrunt fails. I believe that adding support for binary_path to be either absolute, or relative to any path within $PATH may be an effective way to resolve this?

Thanks.

EdanBrooke commented 4 months ago

Because I'm using Terragrunt, I realised I can actually use a generate block to build the inventory YAML files on the fly. I've added the following to my root terragrunt.hcl:

generate "inventory" {
  path      = "${get_repo_root()}/ansible/inventory/terraform-${local.state_name).yml"
  if_exists = "overwrite"
  contents  = <<EOF
---
plugin: cloud.terraform.terraform_provider
binary_path: "${run_cmd("--terragrunt-quiet", "/usr/bin/which", "terragrunt")}" # Use Terragrunt wrapper
project_path: "${get_terragrunt_dir()}"
EOF
}

Consider this resolved, thanks.