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

Support Terraform 0.12 state format #114

Merged
merged 3 commits into from
Aug 1, 2019
Merged

Conversation

AndiDog
Copy link
Contributor

@AndiDog AndiDog commented May 28, 2019

Solves #113 by implementing basic support for the output of Terraform 0.12's terraform show -json. I introduced new JSON-to-struct mappings for 0.12, but kept all old code to remain compatible with old Terraform versions (99% of people probably still use 0.11 right now). Internally, new-style resources are mapped to the existing struct's so almost no code apart from the parser needed any change.

@adammck
Copy link
Owner

adammck commented May 29, 2019

Wow, thanks for delivering this so quickly! I will try to review it this weekend. Please fix the build/tests? Positive/negative feedback from anyone else trying this out is very welcome.

@AndiDog
Copy link
Contributor Author

AndiDog commented May 29, 2019

[...] Please fix the build/tests? [...]

The build is missing the sort.Slice function which was introduced with Go 1.8 (release date 2017-02-16). I've added a commit to update the used Go versions.

@ctrlok
Copy link
Contributor

ctrlok commented Jun 4, 2019

Right now there is a problem with latest version

$ terraform-inventory --list ./
overwriting already existing individual key main.0, old: [18.234.233.11], new: 3.91.252.244"all":{
"hosts":["18.234.233.11","3.91.252.244"] ... "main":["18.234.233.11","3.91.252.244"
],"main.0":["3.91.252.244"]..

I have ec2_instance with count=2

Also I have a problem with parsing existing terraform state file

$ terraform-inventory --list ./terraform.tfstate
Error reading tfstate file: 0.12 format error: <nil>; pre-0.12 format error: <nil> (nil error means no content/modules found in the respective format)

Problem with parsing local tfstate is probably because of different formats of tfstate and terraform show --json

@ctrlok
Copy link
Contributor

ctrlok commented Jun 4, 2019

I'm not sure, but looks like we need to rewrite func NewResource(keyName string, state resourceState) (*Resource, error) function. Because in terraform show --json command resource name is the same for resources with the same name, but those resources have additional parameter index

          "address": "aws_instance.main",
          "mode": "managed",
          "type": "aws_instance",
          "name": "main",
          "index": 0,

@AndiDog
Copy link
Contributor Author

AndiDog commented Jun 4, 2019

Saw that coming since I tested simple count-less cases. I can check next week. Any sample snippets and more testing helps, thank you!!

@AndiDog
Copy link
Contributor Author

AndiDog commented Jun 6, 2019

Please try again now. The pushed patch works in my Terraform project for count=2.

@ctrlok
Copy link
Contributor

ctrlok commented Jun 6, 2019

Works fine, but I got this error message:

tags overwriting already existing output key main, old: [54.164.36.15 3.89.164.2], new: [3.89.164.2 54.164.36.15]

main is a name of resource. I.E. aws_ec2_instance.main

@AndiDog
Copy link
Contributor Author

AndiDog commented Jun 11, 2019

Works fine, but I got this error message:

tags overwriting already existing output key main, old: [54.164.36.15 3.89.164.2], new: [3.89.164.2 54.164.36.15]

main is a name of resource. I.E. aws_ec2_instance.main

Cannot reproduce that. Do you have a minimal example?

@wegerhoff
Copy link

The patch is working fine when working with terraform modules to group hosts. Unfortunately we are using vpshere-tags to group hosts. This was supported by terraform-inventory prior to 0.12.*. Is there a chance to get this feature back without me having to learn go first? :)

Current output of terraform-inventory -inventory:

[module_consul_vms_vm]
10.27.101.38
10.27.101.36
10.27.101.40

...

Expected additional output:

[vsphere_consul_tag]
10.27.101.38
10.27.101.36
10.27.101.40

[vsphere_solr_tag]
10.27.101.4
10.27.101.2
10.27.101.3

@AndiDog
Copy link
Contributor Author

AndiDog commented Jun 21, 2019

As said, please provide a full working example (input + expected output + if possible Terraform 0.12 state file) and it can be tested and fixed hopefully by covering 0.12 with more of the previously existing unit tests.

@mkjmdski
Copy link

mkjmdski commented Jul 1, 2019

Hey, what is the status of this PR? I'm really looking forward to use new dynamic inventory for terraform 0.12 ;)

@adammck
Copy link
Owner

adammck commented Jul 1, 2019

It seems like some people are still having trouble with this PR, but it seems useful enough that we ought to merge and release it anyway, perhaps as a pre-release. You're done with this, @AndiDog?

@AndiDog
Copy link
Contributor Author

AndiDog commented Jul 2, 2019

I have been using the version from this PR for weeks without issue (on AWS). Once the above commenters can provide a reproducible case, they can create separate PRs, since the changes are simple to develop thanks to the unit tests. Please go ahead and merge. I agree that it's already useful now.

@ctrlok
Copy link
Contributor

ctrlok commented Jul 2, 2019

It works for me, but I still have this message even with inventory with a single host. I get this message every time, so I didn't understand how any of my state files can show anything.

@jury89
Copy link

jury89 commented Jul 15, 2019

Hi guys, i'm having the same problem with the new Terraform version. Any idea when this PR will be merged? 🙏

@astubbs
Copy link

astubbs commented Jul 16, 2019

PR works for me - thanks @AndiDog !

@dpisano
Copy link

dpisano commented Jul 23, 2019

I'm still getting the 0.12 format error: <nil>

$ terraform-inventory --list ./terraform.tfstate
Error reading tfstate file: 0.12 format error: <nil>; pre-0.12 format error: <nil> (nil error means no content/modules found in the respective format)

If you like I can share my .tfstate file.

@ctrlok
Copy link
Contributor

ctrlok commented Jul 23, 2019

@dpisano please, read the first message. That version of the terraform works with terraform show --json output

@dpisano
Copy link

dpisano commented Jul 23, 2019

@ctrlok sorry it looks like I was misunderstanding how how to run it. Running just terraform-inventory --list ./ works for me.

@dpisano
Copy link

dpisano commented Jul 26, 2019

As a node this only works when I use it with Ansible when I set TF_STATE=./ when I run the playbook. The full command is something like this: TF_STATE=./ ansible-playbook --inventory-file=/usr/local/bin/terraform-inventory ../site.yml

@AndiDog
Copy link
Contributor Author

AndiDog commented Jul 31, 2019

Can we get this merged and released? Is the PR waiting for anything?

@danihodovic
Copy link

https://github.com/nbering/terraform-inventory is similar to this project, but is compatible with both Terraform 0.12 and 0.11.

https://github.com/nbering/terraform-inventory/blob/master/CHANGELOG.md#200---2019-05-05

@adammck
Copy link
Owner

adammck commented Aug 1, 2019

Nope, this is good to go, I just haven't had time to test it properly. Merging anyway, since it seems to work for plenty of people on this thread. Thanks, @AndiDog!

@adammck adammck merged commit 94a66e3 into adammck:master Aug 1, 2019
@adammck
Copy link
Owner

adammck commented Aug 8, 2019

FYI this was just released as v0.9. Thanks for your patience!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants