Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inventory terraform_state - add support for backend_config_files and update backend_config #112

Conversation

abikouo
Copy link
Contributor

@abikouo abikouo commented Jan 26, 2024

This pull requests add support for the parameter backend_config_files to provide Terraform configuration into files, this could be useful when the backend configuration contains some secrets.
To be aligned with the terraform init command, the backend configuration files should contain only key/values pair defining the configuration, here is an example of a s3 backend configuration

region = "us-east-1"
bucket = "tf-state-storage"
key = "terraform/tfstate"

However, as we are trying to read an existing Terraform state file from the provided backend, we need to generate a simple Terraform with the backend definition, this requires a new parameter backend_type
The generated configuration will look like

terraform {
   backend "<backend_type>" {}
}

We will also update the current backend_config parameter, which takes the full backend configuration by accepting only a dictionnary of key/values pairs to push as configuration to the backend when running terraform init --backend-config command.
Here is an example of an inventory file with the new modifications

inventory_state.yaml

plugin: cloud.terraform.terraform_state
backend_type: s3
backend_config:
   region: "us-east-1"
   key: "terraform/tfstate"
   bucket: "tf-state-storage"
backend_config_files: "aws_secrets.hcl"

With aws_secrets.hcl

access_key = "xxxxxxxxxxxxxxxxx"
secret_key = "xxxxxxxxxxxxxxxxx"

Copy link

codecov bot commented Jan 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

❗ No coverage uploaded for pull request base (stable-statefile-inventory@5e080e6). Click here to learn what that means.

❗ Current head 5cc7acc differs from pull request most recent head bb900ef. Consider uploading reports for the commit bb900ef to get more accurate results

Additional details and impacted files
@@                      Coverage Diff                      @@
##             stable-statefile-inventory     #112   +/-   ##
=============================================================
  Coverage                              ?   79.44%           
=============================================================
  Files                                 ?       23           
  Lines                                 ?     1430           
  Branches                              ?      249           
=============================================================
  Hits                                  ?     1136           
  Misses                                ?      252           
  Partials                              ?       42           
Flag Coverage Δ
units 79.44% <0.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

Copy link

Copy link
Contributor

@hakbailey hakbailey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple small comments but this looks good and works as expected!

plugins/inventory/terraform_state.py Outdated Show resolved Hide resolved
plugins/inventory/terraform_state.py Outdated Show resolved Hide resolved
Copy link
Contributor

@mandar242 mandar242 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested changes to backend_config works as expected.

plugin: cloud.terraform.terraform_state
backend_type: s3
backend_config:
  region: us-west-1
  key: terraform/my.tfstate
  bucket: mandkulk-tf-test

ansible-inventory -i terraform_state.yaml --graph --vars
returns

@all:
  |--@ungrouped:
  |  |--aws_instance_example_server
  |  |  |--{ami = ami-0353faff0d421c70e}
  |  |  |--{arn = arn:aws:ec2:us-west-1:721012345678:instance/i-0bd1ca4a412345678}
  |  |  |--{associate_public_ip_address = False}
  |  |  |--{availability_zone = us-west-1a}
  |  |  |--{capacity_reservation_specification = [{'capacity_reservation_preference': 'open', 'capacity_reservation_target': []}]}
  |  |  |--{cpu_core_count = 1}
  |  |  |--{cpu_options = [{'amd_sev_snp': '', 'core_count': 1, 'threads_per_core': 1}]}
  |  |  |--{cpu_threads_per_core = 1}
...
...

Copy link

@abikouo abikouo requested a review from gravesm January 31, 2024 14:44
Copy link

@abikouo abikouo merged commit 5c5fb11 into ansible-collections:stable-statefile-inventory Jan 31, 2024
36 checks passed
abikouo added a commit that referenced this pull request Feb 26, 2024
…update backend_config (#112)

* add support for backend_config_files
abikouo added a commit that referenced this pull request Feb 28, 2024
…update backend_config (#112)

* add support for backend_config_files
abikouo added a commit that referenced this pull request Feb 28, 2024
* terraform_state - New Inventory plugin (#107)

* initial commit for terraform state inventory

* code before unit and integration tests

* adding extended unit tests

* add integration tests

* fix ansible-lint errors

* fix CI issues

* add changelog fragment

* fix ansible-lint

* fix another ansible-lint issue

* code review

* fix unit tests

* fix ansible-lint indentation

* Setting option backend_config as required

* Inventory terraform_state - add support for backend_config_files and update backend_config (#112)

* add support for backend_config_files

* Support AzureRM and GCP instances types into Terraform state inventory (#110)

---------

Co-authored-by: Bikouo Aubin <[email protected]>
alinabuzachis added a commit to alinabuzachis/cloud.terraform that referenced this pull request May 14, 2024
…ections#116)

* terraform_state - New Inventory plugin (ansible-collections#107)

* initial commit for terraform state inventory

* code before unit and integration tests

* adding extended unit tests

* add integration tests

* fix ansible-lint errors

* fix CI issues

* add changelog fragment

* fix ansible-lint

* fix another ansible-lint issue

* code review

* fix unit tests

* fix ansible-lint indentation

* Setting option backend_config as required

* Inventory terraform_state - add support for backend_config_files and update backend_config (ansible-collections#112)

* add support for backend_config_files

* Support AzureRM and GCP instances types into Terraform state inventory (ansible-collections#110)

---------

Co-authored-by: Bikouo Aubin <[email protected]>
(cherry picked from commit ad19e5b)
alinabuzachis added a commit to alinabuzachis/cloud.terraform that referenced this pull request May 15, 2024
…ections#116)

* terraform_state - New Inventory plugin (ansible-collections#107)

* initial commit for terraform state inventory

* code before unit and integration tests

* adding extended unit tests

* add integration tests

* fix ansible-lint errors

* fix CI issues

* add changelog fragment

* fix ansible-lint

* fix another ansible-lint issue

* code review

* fix unit tests

* fix ansible-lint indentation

* Setting option backend_config as required

* Inventory terraform_state - add support for backend_config_files and update backend_config (ansible-collections#112)

* add support for backend_config_files

* Support AzureRM and GCP instances types into Terraform state inventory (ansible-collections#110)

---------

Co-authored-by: Bikouo Aubin <[email protected]>
(cherry picked from commit ad19e5b)
softwarefactory-project-zuul bot pushed a commit that referenced this pull request May 15, 2024
[Manual backport stable-2] Patchback/backports/stable 2/ad19e5b33b15d566e2979aa471c71902bb9b588d/pr 116

terraform_state - New Inventory plugin (#107)


initial commit for terraform state inventory


code before unit and integration tests


adding extended unit tests


add integration tests


fix ansible-lint errors


fix CI issues


add changelog fragment


fix ansible-lint


fix another ansible-lint issue


code review


fix unit tests


fix ansible-lint indentation


Setting option backend_config as required


Inventory terraform_state - add support for backend_config_files and update backend_config (#112)


add support for backend_config_files


Support AzureRM and GCP instances types into Terraform state inventory (#110)



Co-authored-by: Bikouo Aubin [email protected]
(cherry picked from commit ad19e5b)
SUMMARY


ISSUE TYPE


Bugfix Pull Request
Docs Pull Request
Feature Pull Request
New Module Pull Request

COMPONENT NAME

ADDITIONAL INFORMATION

Reviewed-by: Mike Graves <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants