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

Terraform Resources of "mode" "data" Are Included by Default #167

Open timway opened 1 month ago

timway commented 1 month ago
SUMMARY

The inventory plugin adds any matching resource to the inventory. A number of resource types have "lookup" (aka data) and "concrete" (aka managed) resources. It's not an uncommon process to "lookup" a similar resource to apply some or all of it's settings to a managed resource. This can be seen in AWS (a supported cloud) with the aws_instance data source and resource.

My opinion is the data resources should be excluded by default (breaking change). A sub-option should be added to provider-mapping to specify the mode. It could be restricted to 2 values, "data", and "managed", or 3 including - "any". I'd advocate for the breaking change as it is closer to my expectation as a user of the collection. Being able to add an additional provider mapping to select data or catch both via an "any" value would ensure no existing use cases are left uncovered.

ISSUE TYPE
COMPONENT NAME

plugins/inventory/terraform_state.py

ANSIBLE VERSION
ansible [core 2.16.11]
  config file = /tmp/tmp.ft7auWhZ7g/ansible.cfg
  configured module search path = ['/home/xyz/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.12/site-packages/ansible
  ansible collection location = /tmp/tmp.ft7auWhZ7g
  executable location = /usr/bin/ansible
  python version = 3.12.6 (main, Sep  9 2024, 00:00:00) [GCC 14.2.1 20240801 (Red Hat 14.2.1-1)] (/usr/bin/python3)
  jinja version = 3.1.4
  libyaml = True
COLLECTION VERSION

At the time of testing I was using this version from git dcb5408.

# /tmp/tmp.ft7auWhZ7g/ansible_collections
Collection      Version   
--------------- ----------
cloud.terraform 4.0.0-dev0
CONFIGURATION
COLLECTIONS_PATHS(/tmp/tmp.ft7auWhZ7g/ansible.cfg) = ['/tmp/tmp.ft7auWhZ7g']
CONFIG_FILE() = /tmp/tmp.ft7auWhZ7g/ansible.cfg
EDITOR(env: EDITOR) = /usr/bin/nano
OS / ENVIRONMENT

Fedora Workstation 40, ansible-core and tofu from packages

STEPS TO REPRODUCE

main.tf

terraform {
  required_providers {
    local = {
      source = "hashicorp/local"
      version = "2.5.2"
    }
  }
}

data "local_file" "foo" {
  filename = "${path.module}/foo"
}

resource "local_file" "bar" {
  content  = data.local_file.foo.content
  filename = "${path.module}/bar"
}

hosts.terraform_state.yml

---
plugin: cloud.terraform.terraform_state
backend_type: local
backend_config:
  path: /tmp/tmp.ft7auWhZ7g/terraform.tfstate
binary_path: /usr/bin/tofu
hostnames:
  - filename
provider_mapping:
  - provider_name: registry.opentofu.org/hashicorp/local
    types:
      - local_file
EXPECTED RESULTS
$ ansible-inventory --graph --inventory hosts.terraform_state.yml 
@all:
  |--@ungrouped:
  |  |--./bar
ACTUAL RESULTS
$ ansible-inventory --graph --inventory hosts.terraform_state.yml 
@all:
  |--@ungrouped:
  |  |--./foo
  |  |--./bar