mirror of
https://github.com/adammck/terraform-inventory
synced 2024-11-26 07:43:46 +01:00
Showing Terraform outputs as "global" variables in the Ansible dynamic inventory json.
This commit is contained in:
parent
229202334c
commit
6ead28eee6
15
cli.go
15
cli.go
@ -7,19 +7,26 @@ import (
|
||||
)
|
||||
|
||||
func cmdList(stdout io.Writer, stderr io.Writer, s *state) int {
|
||||
groups := make(map[string][]string, 0)
|
||||
groups := make(map[string]interface{}, 0)
|
||||
for _, res := range s.resources() {
|
||||
for _, grp := range res.Groups() {
|
||||
tmpGroup := []string{}
|
||||
|
||||
_, ok := groups[grp]
|
||||
if !ok {
|
||||
groups[grp] = []string{}
|
||||
if ok {
|
||||
tmpGroup = groups[grp].([]string)
|
||||
}
|
||||
|
||||
groups[grp] = append(groups[grp], res.Address())
|
||||
tmpGroup = append(tmpGroup, res.Address())
|
||||
groups[grp] = tmpGroup
|
||||
}
|
||||
}
|
||||
|
||||
groups["all"] = make(map[string]string, 0)
|
||||
for _, out := range s.outputs() {
|
||||
groups["all"].(map[string]string)[out.keyName] = out.value
|
||||
}
|
||||
|
||||
return output(stdout, stderr, groups)
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,9 @@
|
||||
"path": [
|
||||
"root"
|
||||
],
|
||||
"outputs": {},
|
||||
"outputs": {
|
||||
"datacenter": "mydc"
|
||||
},
|
||||
"resources": {
|
||||
"aws_instance.alpha.0": {
|
||||
"type": "aws_instance",
|
||||
|
26
output.go
Normal file
26
output.go
Normal file
@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Output struct {
|
||||
|
||||
// The keyName and value of the output
|
||||
keyName string
|
||||
value string
|
||||
}
|
||||
|
||||
func NewOutput(keyName string, value string) (*Output, error) {
|
||||
|
||||
// TODO: Warn instead of silently ignore error?
|
||||
if len(keyName) == 0 {
|
||||
return nil, fmt.Errorf("couldn't parse keyName: %s", keyName)
|
||||
}
|
||||
|
||||
return &Output{
|
||||
keyName: keyName,
|
||||
value: value,
|
||||
}, nil
|
||||
}
|
||||
|
16
parser.go
16
parser.go
@ -29,6 +29,20 @@ func (s *state) read(stateFile io.Reader) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// outputs returns a slice of the Outputs found in the statefile.
|
||||
func (s *state) outputs() []*Output {
|
||||
inst := make([]*Output, 0)
|
||||
|
||||
for _, m := range s.Modules {
|
||||
for k, v := range m.Outputs {
|
||||
o, _ := NewOutput(k, v)
|
||||
inst = append(inst, o)
|
||||
}
|
||||
}
|
||||
|
||||
return inst
|
||||
}
|
||||
|
||||
// resources returns a slice of the Resources found in the statefile.
|
||||
func (s *state) resources() []*Resource {
|
||||
inst := make([]*Resource, 0)
|
||||
@ -43,7 +57,6 @@ func (s *state) resources() []*Resource {
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if r.IsSupported() {
|
||||
inst = append(inst, r)
|
||||
}
|
||||
@ -55,6 +68,7 @@ func (s *state) resources() []*Resource {
|
||||
|
||||
type moduleState struct {
|
||||
ResourceStates map[string]resourceState `json:"resources"`
|
||||
Outputs map[string]string `json:"outputs"`
|
||||
}
|
||||
|
||||
// resourceKeys returns a sorted slice of the key names of the resources in this
|
||||
|
Loading…
Reference in New Issue
Block a user