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

refactor: move ANSIBLE_CONFIG env up #359

Merged
merged 1 commit into from
Jan 9, 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
6 changes: 5 additions & 1 deletion cmd/container/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func main() {
adminBundleDir := filepath.Join(simulatorDir, "config", "admin")
packerDir := filepath.Join(simulatorDir, "packer")
terraformWorkspaceDir := filepath.Join(simulatorDir, "terraform/workspaces/simulator")
ansibleConfigPath := filepath.Join(adminBundleDir, "ansible.cfg")
ansiblePlaybookDir := filepath.Join(simulatorDir, "ansible/playbooks")

conf := config.Config{}
Expand All @@ -39,7 +40,10 @@ func main() {
scenarioManager := tools.AnsiblePlaybook{
WorkingDir: adminBundleDir,
PlaybookDir: ansiblePlaybookDir,
Output: os.Stdout,
// Ansible complains on Windows+WSL that the directory ansible configuration is world writable
// and hence ignore the configuration unless explicitly set using the ANSIBLE_CONFIG environment variable.
Env: []string{"ANSIBLE_CONFIG=" + ansibleConfigPath},
Output: os.Stdout,
}

withStateBucketFlag := cli.WithFlag("stateBucket", "", "the name of the S3 bucket to store Terraform state")
Expand Down
14 changes: 5 additions & 9 deletions core/tools/ansible.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

const (
AnsiblePlaybookExecutable Executable = "ansible-playbook"
AnsibleConfigPath string = "/simulator/config/admin/ansible.cfg"
)

type ScenarioManager interface {
Expand All @@ -22,13 +21,14 @@ type ScenarioManager interface {
type AnsiblePlaybook struct {
WorkingDir string
PlaybookDir string
Env []string
Output io.Writer
}

func (p AnsiblePlaybook) Install(ctx context.Context, id string) error {
playbook := fmt.Sprintf("%s.yaml", id)

if err := ansiblePlaybookCommand(p.WorkingDir, p.PlaybookDir, playbook).Run(ctx, p.Output); err != nil {
if err := ansiblePlaybookCommand(p.WorkingDir, p.PlaybookDir, p.Env, playbook).Run(ctx, p.Output); err != nil {
return fmt.Errorf("failed to execute Ansible Playbook: %w", err)
}

Expand All @@ -38,15 +38,15 @@ func (p AnsiblePlaybook) Install(ctx context.Context, id string) error {
func (p AnsiblePlaybook) Uninstall(ctx context.Context, id string) error {
playbook := fmt.Sprintf("%s.yaml", id)

if err := ansiblePlaybookCommand(p.WorkingDir, p.PlaybookDir, playbook, "state=absent").
if err := ansiblePlaybookCommand(p.WorkingDir, p.PlaybookDir, p.Env, playbook, "state=absent").
Run(ctx, p.Output); err != nil {
return fmt.Errorf("failed to run Ansible Playbook with state=absent: %w", err)
}

return nil
}

func ansiblePlaybookCommand(workingDir, playbookDir, playbook string, extraVars ...string) runner {
func ansiblePlaybookCommand(workingDir, playbookDir string, env []string, playbook string, extraVars ...string) runner {
args := []string{
fmt.Sprintf("%s/%s", playbookDir, playbook),
}
Expand All @@ -62,11 +62,7 @@ func ansiblePlaybookCommand(workingDir, playbookDir, playbook string, extraVars
Executable: AnsiblePlaybookExecutable,
WorkingDir: workingDir,
Arguments: args,
// Ansible complains on Windows+WSL that the directory
// with the ansible configuration is world writable
// and hence ignore the configuration unless explicitly
// set using the ANSIBLE_CONFIG environment variable.
Env: []string{"ANSIBLE_CONFIG=" + AnsibleConfigPath},
Env: env,
}
}

Expand Down
Loading