Skip to content

Commit

Permalink
Merge pull request #6 from do-community/private-ips
Browse files Browse the repository at this point in the history
add an option to use private droplet IPs
  • Loading branch information
kamaln7 authored Dec 2, 2020
2 parents 8959101 + 0c56f26 commit fb91a7f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ The resulting inventory will be printed to the console. You can save it to a fil
* `--group-by-tag` - create groups for each Droplet tag. Default behavior.
* `--no-group-by-region` - Do not create groups for each DigitalOcean region.
* `--no-group-by-tag` - Do not create groups for each Droplet tag.
* `--out FILE` - write the inventory to the specified file
* `--out FILE` - write the ansible inventory to this file - if unset, print to stdout
* `--private-ips` - use private Droplet IPs instead of public IPs

## Example

Expand Down Expand Up @@ -166,5 +167,6 @@ Flags:
--ignore=IGNORE ... ignore a Droplet by name, can be specified multiple times
--group-by-region group hosts by region, defaults to true
--group-by-tag group hosts by their Droplet tags, defaults to true
--out=OUT write the ansible inventory to this file
--out=OUT write the ansible inventory to this file - if unset, print to stdout
--private-ips use private Droplet IPs instead of public IPs
```
24 changes: 19 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ var (
ignore = kingpin.Flag("ignore", "ignore a Droplet by name, can be specified multiple times").Strings()
groupByRegion = kingpin.Flag("group-by-region", "group hosts by region, defaults to true").Default("true").Bool()
groupByTag = kingpin.Flag("group-by-tag", "group hosts by their Droplet tags, defaults to true").Default("true").Bool()
out = kingpin.Flag("out", "write the ansible inventory to this file").String()
out = kingpin.Flag("out", "write the ansible inventory to this file - if unset, print to stdout").String()
privateIPs = kingpin.Flag("private-ips", "use private Droplet IPs instead of public IPs").Bool()
)

var doRegions = []string{"ams1", "ams2", "ams3", "blr1", "fra1", "lon1", "nyc1", "nyc2", "nyc3", "sfo1", "sfo2", "sfo3", "sgp1", "tor1"}
Expand Down Expand Up @@ -90,7 +91,8 @@ func main() {
var inventory bytes.Buffer

for _, d := range droplets {
log.WithField("droplet", d.Name).Info("processing")
ll := log.WithField("droplet", d.Name)
ll.Info("processing")
if *groupByRegion {
r := d.Region.Slug
dropletsByRegion[r] = append(dropletsByRegion[r], d.Name)
Expand All @@ -102,9 +104,17 @@ func main() {
}
}

ip, err := d.PublicIPv4()
var (
ip string
err error
)
if *privateIPs {
ip, err = d.PrivateIPv4()
} else {
ip, err = d.PublicIPv4()
}
if err != nil {
log.WithError(err).WithField("droplet", d.Name).Error("couldn't look up the Droplet's IP address, skipped")
ll.WithError(err).Error("couldn't look up the Droplet's IP address, skipped")
continue
}

Expand All @@ -116,7 +126,11 @@ func main() {
if *sshPort != 0 {
inventory.WriteString(fmt.Sprintf("ansible_port=%d ", *sshPort))
}
inventory.WriteString(fmt.Sprintf("ansible_host=%s", ip))
if ip != "" {
inventory.WriteString(fmt.Sprintf("ansible_host=%s", ip))
} else {
ll.Warn("could not get the Droplet's IP address, using hostname")
}
inventory.WriteRune('\n')
}
inventory.WriteRune('\n')
Expand Down

0 comments on commit fb91a7f

Please sign in to comment.