Skip to content

Commit 914466c

Browse files
committed
Add back setting environment variable
The commit 'ed92d3668ca921270d01055d8d8e01f3fd5187d3' removed the `setAnsibleEnvVars` function. However, that function is important to make sure the command line flags `ansible-roles-path` and `ansible-collections-path` work. These command line flags are set into environment variables that will be picked up later.
1 parent 871455c commit 914466c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

cmd/ansible-operator/main.go

+26
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ func main() {
123123
options.Namespace = metav1.NamespaceAll
124124
}
125125

126+
err = setAnsibleEnvVars(f)
127+
if err != nil {
128+
log.Error(err, "Failed to set environment variable.")
129+
os.Exit(1)
130+
}
131+
126132
// Create a new manager to provide shared dependencies and start components
127133
mgr, err := manager.New(cfg, options)
128134
if err != nil {
@@ -220,3 +226,23 @@ func getAnsibleDebugLog() bool {
220226
}
221227
return val
222228
}
229+
230+
// setAnsibleEnvVars will set environment variables based on CLI flags
231+
func setAnsibleEnvVars(f *flags.Flags) error {
232+
if len(f.AnsibleRolesPath) > 0 {
233+
if err := os.Setenv(flags.AnsibleRolesPathEnvVar, f.AnsibleRolesPath); err != nil {
234+
return fmt.Errorf("failed to set environment variable %s: %v", flags.AnsibleRolesPathEnvVar, err)
235+
}
236+
log.Info("Set the environment variable", "envVar", flags.AnsibleRolesPathEnvVar,
237+
"value", f.AnsibleRolesPath)
238+
}
239+
240+
if len(f.AnsibleCollectionsPath) > 0 {
241+
if err := os.Setenv(flags.AnsibleCollectionsPathEnvVar, f.AnsibleCollectionsPath); err != nil {
242+
return fmt.Errorf("failed to set environment variable %s: %v", flags.AnsibleCollectionsPathEnvVar, err)
243+
}
244+
log.Info("Set the environment variable", "envVar", flags.AnsibleCollectionsPathEnvVar,
245+
"value", f.AnsibleCollectionsPath)
246+
}
247+
return nil
248+
}

0 commit comments

Comments
 (0)