Skip to content

Commit

Permalink
Print inventory progression and remove generate command
Browse files Browse the repository at this point in the history
  • Loading branch information
clementblaise committed Jul 20, 2021
1 parent 9feb4c0 commit 1363e69
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 123 deletions.
15 changes: 12 additions & 3 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd

import (
"context"
"fmt"
"github.com/apenella/go-ansible/pkg/adhoc"
"github.com/apenella/go-ansible/pkg/options"
"github.com/ca-gip/dploy/internal/ansible"
Expand All @@ -31,6 +32,13 @@ var execCmd = &cobra.Command{
Use: "exec",
Short: "Run Ad Hoc command",
Long: `TODO`,
PreRun: func(cmd *cobra.Command, args []string) {
curr, _ := os.Getwd()
templateAnsibleCommand(cmd, args, curr)
if !askForConfirmation("Do you confirm ?") {
log.Fatal("canceled...")
}
},
Run: func(cmd *cobra.Command, args []string) {
curr, _ := os.Getwd()
exec(cmd, args, curr)
Expand Down Expand Up @@ -67,7 +75,7 @@ func init() {

}

func template(cmd *cobra.Command, args []string, path string) {
func templateAnsibleCommand(cmd *cobra.Command, args []string, path string) {
// Load project from root
project := ansible.Projects.LoadFromPath(path)

Expand All @@ -90,7 +98,7 @@ func template(cmd *cobra.Command, args []string, path string) {
}

template := ansible.AdHocCmd{
Comment: "# Commands",
Comment: "# Commands to be executed :",
Inventory: inventories,
Pattern: pattern,
ModuleName: module,
Expand Down Expand Up @@ -133,7 +141,7 @@ func exec(cmd *cobra.Command, args []string, path string) {
log.Fatal(err)
}

for _, inventory := range inventories {
for index, inventory := range inventories {
ansibleAdhocOptions := &adhoc.AnsibleAdhocOptions{
Args: arg,
Background: background,
Expand All @@ -148,6 +156,7 @@ func exec(cmd *cobra.Command, args []string, path string) {
}

options.AnsibleForceColor()
fmt.Printf("Inventory %d/%d %s\n", index+1, len(inventories), inventory.RelativePath())
err := adhoc.Run(context.TODO())
if err != nil {
log.Error(err)
Expand Down
113 changes: 0 additions & 113 deletions cmd/generate.go

This file was deleted.

58 changes: 55 additions & 3 deletions cmd/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cmd

import (
"context"
"fmt"
"github.com/apenella/go-ansible/pkg/execute"
"github.com/apenella/go-ansible/pkg/options"
"github.com/apenella/go-ansible/pkg/playbook"
Expand All @@ -36,7 +37,7 @@ var playCmd = &cobra.Command{
Long: `TODO...`,
PreRun: func(cmd *cobra.Command, args []string) {
curr, _ := os.Getwd()
generate(cmd, args, curr)
templatePlaybookCommand(cmd, args, curr)
if !askForConfirmation("Do you confirm ?") {
log.Fatal("canceled...")
}
Expand Down Expand Up @@ -79,6 +80,47 @@ func init() {
})
}

func templatePlaybookCommand(cmd *cobra.Command, args []string, path string) {
// Load project from root
project := ansible.Projects.LoadFromPath(path)

// Retrieve playbook to be run
playbookPath, _ := cmd.Flags().GetString("playbook")
playbook, err := project.PlaybookPath(playbookPath)
if err != nil {
log.Fatalf(`%s not a valid path`, playbookPath)
}

// Retrieve filter to select inventories
rawFilters, _ := cmd.Flags().GetStringSlice("filter")
filters := ansible.ParseFilterArgsFromSlice(rawFilters)
inventories := project.FilterInventory(filters)

// Retrieve ansible flags
tags, _ := cmd.Flags().GetStringSlice("tags")
limit, _ := cmd.Flags().GetStringSlice("limit")
skipTags, _ := cmd.Flags().GetStringSlice("skip-tags")
check, _ := cmd.Flags().GetBool("check")
diff, _ := cmd.Flags().GetBool("diff")
vaultPassFile, _ := cmd.Flags().GetString("vault-password-file")
askVaultPass, _ := cmd.Flags().GetBool("ask-vault-password")

commands := &ansible.PlaybookCmd{
Comment: "# Commands to be executed :",
Inventory: inventories,
Playbook: playbook,
Tags: tags,
Limit: limit,
SkipTags: skipTags,
Check: check,
Diff: diff,
VaultPasswordFile: vaultPassFile,
AskVaultPass: askVaultPass,
}

commands.Generate()
}

func play(cmd *cobra.Command, args []string, path string) {
// Load project from root
project := ansible.Projects.LoadFromPath(path)
Expand All @@ -103,7 +145,7 @@ func play(cmd *cobra.Command, args []string, path string) {
summary := make(map[string]bool, len(inventories))

// Execute ansible for each inventory (sequential)
for _, inventory := range inventories {
for index, inventory := range inventories {
ansiblePlaybookOptions := &playbook.AnsiblePlaybookOptions{
Inventory: inventory.RelativePath(),
Limit: strings.Join(limit, ","),
Expand All @@ -121,6 +163,7 @@ func play(cmd *cobra.Command, args []string, path string) {
}

options.AnsibleForceColor()
fmt.Printf("Inventory %d/%d %s\n", index+1, len(inventories), inventory.RelativePath())
err := play.Run(context.TODO())
if err != nil {
log.Error(err)
Expand All @@ -130,5 +173,14 @@ func play(cmd *cobra.Command, args []string, path string) {
}
}

log.Info(summary)
fmt.Println("\nSummary :")
for inventory, state := range summary {
fmt.Printf("%s ", inventory)
if state {
fmt.Println("passed")
} else {
fmt.Println("failed")
}
}

}
4 changes: 2 additions & 2 deletions cmd/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func askForConfirmation(s string) bool {
reader := bufio.NewReader(os.Stdin)

for {
fmt.Printf("%s [y/n]: ", s)
fmt.Printf("%s [Y/n]: ", s)

response, err := reader.ReadString('\n')
if err != nil {
Expand All @@ -160,7 +160,7 @@ func askForConfirmation(s string) bool {

response = strings.ToLower(strings.TrimSpace(response))

if response == "y" || response == "yes" {
if response == "y" || response == "yes" || response == "" {
return true
} else if response == "n" || response == "no" {
return false
Expand Down
3 changes: 1 addition & 2 deletions internal/ansible/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ type AdHocCmd struct {

var AdHocCmdTemplate = `{{ .Comment }}
{{- range $inventory := .Inventory }}
ansible {{ $.Pattern }} -a {{ $.ModuleArgs }}
ansible {{ $.Pattern }} -i {{ $inventory.RelativePath }} -a {{ $.ModuleArgs }}
{{- if $.ModuleName }} -m {{ $.ModuleName }}{{- end }}
-i {{ $inventory.RelativePath }}
{{- if $.ExtraVars }} -e {{ $.ExtraVars }}{{- end }}
{{- if $.Background }} --background {{ $.Background }}{{- end }}
{{- if $.Fork }} --forks {{ $.Fork }}{{- end }}
Expand Down

0 comments on commit 1363e69

Please sign in to comment.