Skip to content

Commit

Permalink
Add back setting environment variable (#3549)
Browse files Browse the repository at this point in the history
Co-authored-by: Joe Lanford <[email protected]>
  • Loading branch information
tengqm and joelanford authored Jul 27, 2020
1 parent c092d12 commit 3ac3267
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
9 changes: 9 additions & 0 deletions changelog/fragments/ansible-operator-env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
entries:
- description: >
Fixed a bug that caused the Ansible operator not to set the environment
variables `ANSIBLE_ROLES_PATH` and `ANSIBLE_COLLECTIONS_PATH` based on the
flags `--ansible-roles-path` and `--ansible-collections-path`
kind: bugfix
breaking: false
26 changes: 26 additions & 0 deletions cmd/ansible-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ func main() {
options.Namespace = metav1.NamespaceAll
}

err = setAnsibleEnvVars(f)
if err != nil {
log.Error(err, "Failed to set environment variable.")
os.Exit(1)
}

// Create a new manager to provide shared dependencies and start components
mgr, err := manager.New(cfg, options)
if err != nil {
Expand Down Expand Up @@ -220,3 +226,23 @@ func getAnsibleDebugLog() bool {
}
return val
}

// setAnsibleEnvVars will set environment variables based on CLI flags
func setAnsibleEnvVars(f *flags.Flags) error {
if len(f.AnsibleRolesPath) > 0 {
if err := os.Setenv(flags.AnsibleRolesPathEnvVar, f.AnsibleRolesPath); err != nil {
return fmt.Errorf("failed to set environment variable %s: %v", flags.AnsibleRolesPathEnvVar, err)
}
log.Info("Set the environment variable", "envVar", flags.AnsibleRolesPathEnvVar,
"value", f.AnsibleRolesPath)
}

if len(f.AnsibleCollectionsPath) > 0 {
if err := os.Setenv(flags.AnsibleCollectionsPathEnvVar, f.AnsibleCollectionsPath); err != nil {
return fmt.Errorf("failed to set environment variable %s: %v", flags.AnsibleCollectionsPathEnvVar, err)
}
log.Info("Set the environment variable", "envVar", flags.AnsibleCollectionsPathEnvVar,
"value", f.AnsibleCollectionsPath)
}
return nil
}

0 comments on commit 3ac3267

Please sign in to comment.