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

cloud.terraform.terraform_state Plugin should accept required_providers source and version as input #151

Closed oshankkumar closed 4 months ago

oshankkumar commented 5 months ago
SUMMARY

The inventory plugin cloud.terraform.terraform_state should accept required_providers source and version as input. I am using the azurerm backend provider with version 2.99.0. However, this plugin by default uses the latest azurerm version while building the inventory, resulting in an error when running terraform show while building the inventory.

ISSUE TYPE
COMPONENT NAME

plugin

ANSIBLE VERSION
ansible [core 2.17.0]
  config file = None
  configured module search path = ['/home/XXXXXX/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/XXXXX/python3.11/site-packages/ansible
  ansible collection location = /home/XXXXX/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/XXXXXXX/bin/ansible
  python version = 3.11.9 (main, Apr  6 2024, 17:59:24) [GCC 9.4.0] (/XXXXX/bin/python3.11)
  jinja version = 3.1.4
  libyaml = True
COLLECTION VERSION
Collection                               Version
---------------------------------------- -------
ansible.posix                            1.5.4
azure.azcollection                       2.4.0
cloud.terraform                          3.0.0
community.docker                         3.10.3
community.library_inventory_filtering_v1 1.0.1
CONFIGURATION
OS / ENVIRONMENT
Distributor ID: Ubuntu
Description: Ubuntu 20.04.6 LTS
Release: 20.04
Codename: focal
STEPS TO REPRODUCE

Create an inventory plugin inventory.yml

plugin: cloud.terraform.terraform_state
backend_type: azurerm
backend_config:
  resource_group_name: rg
  storage_account_name: sc
  container_name: terraform
  key: terraform.tfstate

Run the following command:

ansible-inventory -i inventory.yml --graph

An error is thrown:

[WARNING]:  * Failed to parse inventory.yml with auto plugin: Could not get Terraform show from path: /tmp/tmp11q0tkqc.  stdout:
stderr: Failed to marshal state to json: unsupported attribute "availability_zone"

This happens because the Terraform state file was built using an older version while the cloud.terraform.terraform_state plugin uses the latest version during terraform init. The plugin should accept input for required_providers source and version and use this information to write the Terraform version config which will be used during terraform init.

vvarga007 commented 5 months ago

@abikouo, it appears that you've renamed 'ProvidersMapping' to 'PROVIDERS_CONFIG', which is causing a merge conflict with my pull request. Could you also take a look at my PR and make it happen? It would be great to be able to define custom providers.

ansible-collections/cloud.terraform#148 ansible-collections/cloud.terraform#146

Thanks,