Skip to content

Commit 2216e3b

Browse files
authored
pkg/ansible/run.go: add back setting environment variables (v0.19.x) (#3565)
1 parent ef73f4e commit 2216e3b

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
entries:
2+
- description: >
3+
Fixed a bug that caused the Ansible operator not to set the environment
4+
variables `ANSIBLE_ROLES_PATH` and `ANSIBLE_COLLECTIONS_PATH` based on the
5+
flags `--ansible-roles-path` and `--ansible-collections-path`
6+
7+
kind: bugfix
8+
9+
breaking: false

pkg/ansible/run.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ func Run(flags *aoflags.AnsibleOperatorFlags) error {
113113
options.Namespace = metav1.NamespaceAll
114114
}
115115

116+
err = setAnsibleEnvVars(flags)
117+
if err != nil {
118+
log.Error(err, "Failed to set environment variable.")
119+
return err
120+
}
121+
116122
// Create a new manager to provide shared dependencies and start components
117123
mgr, err := manager.New(cfg, options)
118124
if err != nil {
@@ -291,3 +297,23 @@ func getAnsibleDebugLog() bool {
291297
}
292298
return val
293299
}
300+
301+
// setAnsibleEnvVars will set environment variables based on CLI flags
302+
func setAnsibleEnvVars(f *aoflags.AnsibleOperatorFlags) error {
303+
if len(f.AnsibleRolesPath) > 0 {
304+
if err := os.Setenv(aoflags.AnsibleRolesPathEnvVar, f.AnsibleRolesPath); err != nil {
305+
return fmt.Errorf("failed to set environment variable %s: %v", aoflags.AnsibleRolesPathEnvVar, err)
306+
}
307+
log.Info("Set the environment variable", "envVar", aoflags.AnsibleRolesPathEnvVar,
308+
"value", f.AnsibleRolesPath)
309+
}
310+
311+
if len(f.AnsibleCollectionsPath) > 0 {
312+
if err := os.Setenv(aoflags.AnsibleCollectionsPathEnvVar, f.AnsibleCollectionsPath); err != nil {
313+
return fmt.Errorf("failed to set environment variable %s: %v", aoflags.AnsibleCollectionsPathEnvVar, err)
314+
}
315+
log.Info("Set the environment variable", "envVar", aoflags.AnsibleCollectionsPathEnvVar,
316+
"value", f.AnsibleCollectionsPath)
317+
}
318+
return nil
319+
}

0 commit comments

Comments
 (0)