Skip to content

Commit

Permalink
Add ENV TF_HOSTNAME_KEY_NAME to specify which hostname to return
Browse files Browse the repository at this point in the history
Overwrite with the environment variable TF_HOSTNAME_KEY_NAME which attribute is to be used for the ansible inventory name.
By default, the IP address is used. The Ansible variable "ansible_host" is used to connect to the host.
  • Loading branch information
cgroschupp committed Jun 19, 2018
1 parent 1046853 commit ba8e3b7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ to `private_ip` before running the playbook, like:

TF_KEY_NAME=private_ip ansible-playbook --inventory-file=/path/to/terraform-inventory deploy/playbook.yml

By default, the ip address is the ansible inventory name. The `TF_HOSTNAME_KEY_NAME` environment variable allows
you to overwrite the source of the ansible inventory name.

TF_HOSTNAME_KEY_NAME=name ansible-playbook --inventory-file=/path/to/terraform-inventory deploy/playbook.yml

## Development

It's just a Go app, so the usual:
Expand Down
18 changes: 10 additions & 8 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,20 @@ func gatherResources(s *state) map[string]interface{} {

for _, res := range s.resources() {
// place in list of all resources
all.Hosts = appendUniq(all.Hosts, res.Address())
all.Hosts = appendUniq(all.Hosts, res.Hostname())

// place in list of resource types
tp := fmt.Sprintf("type_%s", res.resourceType)
types[tp] = appendUniq(types[tp], res.Address())
types[tp] = appendUniq(types[tp], res.Hostname())

unsortedOrdered[res.baseName] = append(unsortedOrdered[res.baseName], res)

// store as invdividual host (eg. <name>.<count>)
invdName := fmt.Sprintf("%s.%d", res.baseName, res.counter)
if old, exists := individual[invdName]; exists {
fmt.Fprintf(os.Stderr, "overwriting already existing individual key %s, old: %v, new: %v", invdName, old, res.Address())
fmt.Fprintf(os.Stderr, "overwriting already existing individual key %s, old: %v, new: %v", invdName, old, res.Hostname())
}
individual[invdName] = []string{res.Address()}
individual[invdName] = []string{res.Hostname()}

// inventorize tags
for k, v := range res.Tags() {
Expand All @@ -77,7 +77,7 @@ func gatherResources(s *state) map[string]interface{} {
if v != "" {
tag = fmt.Sprintf("%s_%s", k, v)
}
tags[tag] = appendUniq(tags[tag], res.Address())
tags[tag] = appendUniq(tags[tag], res.Hostname())
}
}

Expand All @@ -94,7 +94,7 @@ func gatherResources(s *state) map[string]interface{} {
sort.Sort(cs)

for i := range resources {
ordered[basename] = append(ordered[basename], resources[i].Address())
ordered[basename] = append(ordered[basename], resources[i].Hostname())
}
}

Expand Down Expand Up @@ -187,8 +187,10 @@ func checkErr(err error, stderr io.Writer) int {

func cmdHost(stdout io.Writer, stderr io.Writer, s *state, hostname string) int {
for _, res := range s.resources() {
if hostname == res.Address() {
return output(stdout, stderr, res.Attributes())
if hostname == res.Hostname() {
attributes := res.Attributes()
attributes["ansible_host"] = res.Address()
return output(stdout, stderr, attributes)
}
}

Expand Down
1 change: 1 addition & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ olddatacenter="\u003c0.7_format"

const expectedHostOneOutput = `
{
"ansible_host": "10.0.0.1",
"id":"i-aaaaaaaa",
"private_ip":"10.0.0.1",
"tags.#": "1",
Expand Down
11 changes: 11 additions & 0 deletions resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,17 @@ func (r Resource) NameWithCounter() string {
return fmt.Sprintf("%s.%d", r.baseName, r.counter)
}

// Hostname returns the hostname of this resource.
func (r Resource) Hostname() string {
if keyName := os.Getenv("TF_HOSTNAME_KEY_NAME"); keyName != "" {
if ip := r.State.Primary.Attributes[keyName]; ip != "" {
return ip
}
}

return r.Address()
}

// Address returns the IP address of this resource.
func (r Resource) Address() string {
if keyName := os.Getenv("TF_KEY_NAME"); keyName != "" {
Expand Down

0 comments on commit ba8e3b7

Please sign in to comment.