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

Create all group as per Ansible inventory script conventions #46

Merged
merged 8 commits into from
Sep 22, 2016

Conversation

Jonnymcc
Copy link
Contributor

I first noticed that I was unable to run terraform-inventory --list without first exporting TF_STATE=.terraform/terraform.tfstate and as per the README it appears as though having to export TF_STATE was optional. Second, I noticed that when running ansible-playbook with the --list-hosts flag ansible-playbook would say there were no hosts found. When I removed --list-hosts it detected the host that the playbook should run against.

I then ran git bisect to track down the problem. And I was led to this commit 6ead28e. I noticed that in cli.go this change added an "all" group to the Ansible inventory, so I went to check the Ansible docs regarding the format expected from a script.

http://docs.ansible.com/ansible/developing_inventory.html#script-conventions
The JSON generated from the master branch creates an "all" group containing a mapping of variable keys and values. According to the Ansible docs that mapping should be under the "vars" key of the "all" group.

After making these changes it has fixed both of the problems I was encountering.

@Jonnymcc
Copy link
Contributor Author

Not sure why the tests are failing. I'm a go beginner.

Jonnymcc pushed a commit to Jonnymcc/terraform-inventory that referenced this pull request Sep 16, 2016
Copy link
Contributor

@lolchocotaco lolchocotaco left a comment

Choose a reason for hiding this comment

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

Minor change that cause hosts to be missed.

}
sort.Strings(strs)
i := sort.SearchStrings(strs, item)
if i < len(strs) && strs[i] != item {
Copy link
Contributor

Choose a reason for hiding this comment

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

According to the goDocs: https://golang.org/pkg/sort/#SearchStrings, this conditional excludes IP ranges that would go at the end of the list. Consider something like:
if i == len(strs) || (i < len(strs) && strs[i] != item) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done here c735e9f

@@ -139,7 +139,15 @@ const exampleStateFile = `

const expectedListOutput = `
{
"all": {"datacenter": "mydc", "olddatacenter": "<0.7_format", "ids": [1, 2, 3, 4], "map": {"key": "value"}},
"all": {
"hosts": ["10.0.0.1"],
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice to update the test cases to test the unique host filtering functionality.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done here 082d2ec

@adammck
Copy link
Owner

adammck commented Sep 22, 2016

Thank you very much for the patch, and especially for the comprehensive explanation of the issue. Looks good to me. Merging.

@adammck adammck merged commit 0e941ce into adammck:master Sep 22, 2016
@adammck
Copy link
Owner

adammck commented Sep 22, 2016

FYI, this PR is included in the v0.7-pre release. I'd greatly appreciate it if you could give it a try.

@Jonnymcc
Copy link
Contributor Author

Tested, looks good!

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.

None yet

3 participants