Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provider should properly failed when ansible-playbook executable is not installed #106

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
minor_changes:
- resource/ansible_playbook - Provider should failed with proper message when ansible is not installed (https://github.com/ansible/terraform-provider-ansible/issues/35).
24 changes: 19 additions & 5 deletions provider/resource_playbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/ansible/terraform-provider-ansible/providerutils"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -512,7 +513,7 @@ func resourcePlaybookRead(ctx context.Context, data *schema.ResourceData, meta i
return diags
}

func resourcePlaybookUpdate(_ context.Context, data *schema.ResourceData, _ interface{}) diag.Diagnostics {
func resourcePlaybookUpdate(ctx context.Context, data *schema.ResourceData, _ interface{}) diag.Diagnostics {
var diags diag.Diagnostics

name, okay := data.Get("name").(string)
Expand Down Expand Up @@ -552,7 +553,7 @@ func resourcePlaybookUpdate(_ context.Context, data *schema.ResourceData, _ inte
})
}

log.Printf("LOG [ansible-playbook]: playbook = %s", playbook)
tflog.Info(ctx, fmt.Sprintf("LOG [ansible-playbook]: playbook = %s", playbook))

ignorePlaybookFailure, okay := data.Get("ignore_playbook_failure").(bool)
if !okay {
Expand Down Expand Up @@ -604,10 +605,22 @@ func resourcePlaybookUpdate(_ context.Context, data *schema.ResourceData, _ inte
}
}

log.Printf("Temp Inventory File: %s", tempInventoryFile)
tflog.Debug(ctx, fmt.Sprintf("Temp Inventory File: %s", tempInventoryFile))

// ********************************* RUN PLAYBOOK ********************************

// Validate ansible-playbook binary
if _, validateBinPath := exec.LookPath(ansiblePlaybookBinary); validateBinPath != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("ERROR [ansible-playbook]: couldn't find executable %s", ansiblePlaybookBinary),
})
}

if diags.HasError() {
return diags
}

args := []string{}

args = append(args, "-i", tempInventoryFile)
Expand All @@ -625,6 +638,7 @@ func resourcePlaybookUpdate(_ context.Context, data *schema.ResourceData, _ inte
args = append(args, tmpArg)
}

tflog.Info(ctx, fmt.Sprintf("Running Command <%s %s>", ansiblePlaybookBinary, strings.Join(args, " ")))
runAnsiblePlay := exec.Command(ansiblePlaybookBinary, args...)

runAnsiblePlayOut, runAnsiblePlayErr := runAnsiblePlay.CombinedOutput()
Expand Down Expand Up @@ -667,12 +681,12 @@ func resourcePlaybookUpdate(_ context.Context, data *schema.ResourceData, _ inte
})
}

log.Printf("LOG [ansible-playbook]: %s", runAnsiblePlayOut)
tflog.Debug(ctx, fmt.Sprintf("LOG [ansible-playbook]: %s", runAnsiblePlayOut))

// Wait for playbook execution to finish, then remove the temporary file
err := runAnsiblePlay.Wait()
if err != nil {
log.Printf("LOG [ansible-playbook]: didn't wait for playbook to execute: %v", err)
tflog.Error(ctx, fmt.Sprintf("LOG [ansible-playbook]: didn't wait for playbook to execute: %v", err))
}

diagsFromUtils := providerutils.RemoveFile(tempInventoryFile)
Expand Down
Loading