mirror of
https://github.com/adammck/terraform-inventory
synced 2024-11-22 20:01:58 +01:00
Rename ENV var to TF_STATE
The old one is still supported.
This commit is contained in:
parent
b5926ec984
commit
47c0c9f64c
@ -20,10 +20,10 @@ add it, if you think that would be useful.
|
||||
## Usage
|
||||
|
||||
Ansible doesn't (seem to) support calling the inventory script with parameters,
|
||||
so you can specify the path to the state file using the `TI_TFSTATE` environment
|
||||
so you can specify the path to the state file using the `TF_STATE` environment
|
||||
variable, like so:
|
||||
|
||||
TI_TFSTATE=deploy/terraform.tfstate ansible-playbook --inventory-file=terraform-inventory deploy/playbook.yml
|
||||
TF_STATE=deploy/terraform.tfstate ansible-playbook --inventory-file=terraform-inventory deploy/playbook.yml
|
||||
|
||||
Alternately, you can create a little shell script and call that. Something like:
|
||||
|
||||
|
33
main.go
33
main.go
@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
env "github.com/danryan/env"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
@ -12,52 +11,52 @@ var version = flag.Bool("version", false, "print version information and exit")
|
||||
var list = flag.Bool("list", false, "list mode")
|
||||
var host = flag.String("host", "", "host mode")
|
||||
|
||||
type Config struct {
|
||||
TfState string `env:"key=TI_TFSTATE"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
file := flag.Arg(0)
|
||||
cfg := &Config{}
|
||||
env.MustProcess(cfg)
|
||||
|
||||
if *version == true {
|
||||
fmt.Printf("%s version %s\n", os.Args[0], versionInfo())
|
||||
return
|
||||
}
|
||||
|
||||
// not given on the command line? try ENV.
|
||||
if file == "" {
|
||||
if cfg.TfState == "" {
|
||||
fmt.Printf("Usage: %s [options] path\n", os.Args[0])
|
||||
os.Exit(1)
|
||||
} else {
|
||||
file = cfg.TfState
|
||||
}
|
||||
file = os.Getenv("TF_STATE")
|
||||
}
|
||||
|
||||
// also try the old ENV name.
|
||||
if file == "" {
|
||||
file = os.Getenv("TI_TFSTATE")
|
||||
}
|
||||
|
||||
if file == "" {
|
||||
fmt.Printf("Usage: %s [options] path\n", os.Args[0])
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if !*list && *host == "" {
|
||||
fmt.Println("Either --host or --list must be specified")
|
||||
fmt.Fprint(os.Stderr, "Either --host or --list must be specified")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
path, err := filepath.Abs(file)
|
||||
if err != nil {
|
||||
fmt.Printf("Invalid file: %s\n", err)
|
||||
fmt.Fprintf(os.Stderr, "Invalid file: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
stateFile, err := os.Open(path)
|
||||
defer stateFile.Close()
|
||||
if err != nil {
|
||||
fmt.Printf("Error opening tfstate file: %s\n", err)
|
||||
fmt.Fprintf(os.Stderr, "Error opening tfstate file: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
var s state
|
||||
err = s.read(stateFile)
|
||||
if err != nil {
|
||||
fmt.Printf("Error reading tfstate file: %s\n", err)
|
||||
fmt.Fprintf(os.Stderr, "Error reading tfstate file: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user