Outputting Output to File?

3,269 views
Skip to first unread message

Jason Champion

unread,
Nov 23, 2015, 8:05:53 PM11/23/15
to Terraform
Is it possible to use outputs to generate a file?

My use case is that I'm spinning up a group of servers and want to generate a hosts file containing their names and IP addresses after creation.

I have outputs that look like this:

output "greenplum9 ip" {
    value = "${aws_instance.greenplum9.public_ip}"
}
output "greenplum8 ip" {
    value = "${aws_instance.greenplum8.public_ip}"
}

And that prints output like:

greenplum9 ip = 42.42.123.123
greenplum8 ip = 42.42.123.124

I'd like to be able to write (or append to) a text file rather than just outputting to screen. Is this possible/sensible with terraform, and if so, how would I do it?

Mitchell Hashimoto

unread,
Nov 23, 2015, 8:09:47 PM11/23/15
to <terraform-tool@googlegroups.com>
Hey Jason,

Take a look at the `terraform output` command. This might be just what you're looking for.

In the future, we actually have plans to provide a more machine-readable way to get all the outputs together.

Best,
Mitchell

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/8a0d4075-e388-459e-a76f-81e3318ae565%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jason Champion

unread,
Nov 23, 2015, 8:19:00 PM11/23/15
to terrafo...@googlegroups.com
Thank you, that'll work well enough for now. :)

You received this message because you are subscribed to a topic in the Google Groups "Terraform" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/terraform-tool/f2RpFeuW5tg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/CACOXMX7rt2%2B4ax%3D6Cf84iLEoz9VQ26ovc8QEHez%2BNnH9kV5a-Q%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.



--
Jason Champion
Chief Technology Officer
Theme Dragon
503-840-4012
http://www.themedragon.com

George Hartzell

unread,
Nov 24, 2015, 6:03:44 PM11/24/15
to terrafo...@googlegroups.com, Jason Champion
[Roster][roster] might be useful, particularly:

roster hosts

which will dump out an /etc/hosts formatted file based on the info in
the state file.

Or

roster execute-template ...

which will let you run the name/addr info through your own template.

I've used it to provide hosts files when I was working through the
[provisioning taste test][taste] and as a dynamic index for Ansible
runs.

Roster is in its very early days, please let me know if there's
something else that you'd like it to do or if you can provide info
about another provider you'd like it to support.

g.

[roster]: https://github.com/hartzell/roster
[taste]: https://valdhaus.co/books/taste-test-puppet-chef-salt-stack-ansible.html

Jason Champion

unread,
Nov 24, 2015, 6:53:42 PM11/24/15
to terrafo...@googlegroups.com
George,

That looks pretty neat.

Pardon my ignorance, but is there something I need to do in order to build? I checked out the code and didn't see anything in the README, or a prebuilt executable.

I don't really know golang (know C++ and Python tho), so please forgive me if there's some dumb obvious thing that all go developers know to do...

Regards,
Jason

George Hartzell

unread,
Nov 24, 2015, 7:04:35 PM11/24/15
to terrafo...@googlegroups.com, Jason Champion
Jason Champion writes:
> George,
>
> That looks pretty neat.
>
> Pardon my ignorance, but is there something I need to do in order to build?
> I checked out the code and didn't see anything in the README, or a prebuilt
> executable.
>
> I don't really know golang (know C++ and Python tho), so please forgive me
> if there's some dumb obvious thing that all go developers know to do...

Nice to hear it might be helpful.

I'll add some info to the README, but if you're in a hurry, it should
just build as a bog standard go application.

What platform are you working on? Would you trust a precompiled
binary?

Working from memory here, can't test run this at the moment, but...

Assuming you have go installed on your machine and that you set the
GOPATH environment variable to some directory where you'd like all of
your go stuff to go, you should just be able to

go get https://github.com/hartzell/roster
cd $GOPATH/src/github.com/hartzell/roster
go build

and you should end up with the roster executable.

I'll write up more (and test run it myself) after thanksgiving.

g.
Reply all
Reply to author
Forward
0 new messages