Skip to content

Commit

Permalink
refactor: move ANSIBLE_CONFIG env up
Browse files Browse the repository at this point in the history
  • Loading branch information
Ric Featherstone committed Jan 8, 2024
1 parent cf25421 commit 286d426
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
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

0 comments on commit 286d426

Please sign in to comment.