Skip to content

Commit

Permalink
docker: support referencing env vars in rolecule.yml (#15)
Browse files Browse the repository at this point in the history
* docker: support referencing env vars in rolecule.yml
* Add env var expansion support for podman engine

---------

Co-authored-by: gurpalw <[email protected]>
Co-authored-by: z0mbix <[email protected]>
  • Loading branch information
3 people authored Oct 16, 2024
1 parent 4596d2d commit 396f2d5
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
8 changes: 4 additions & 4 deletions pkg/container/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package container

import (
"fmt"
"os"
"strings"

"github.com/apex/log"
Expand All @@ -28,13 +29,12 @@ func (p *DockerEngine) Run(image string, args []string) (string, error) {
}

func (p *DockerEngine) Exec(containerName string, envVars map[string]string, cmd string, args []string) error {
execArgs := []string{
"exec",
}
execArgs := []string{"exec"}

for k, v := range envVars {
// Expand environment variables within the value if they exist using os.ExpandEnv
execArgs = append(execArgs, "--env")
execArgs = append(execArgs, fmt.Sprintf("%s=%s", k, v))
execArgs = append(execArgs, fmt.Sprintf("%s=%s", k, os.ExpandEnv(v)))
}

execArgs = append(execArgs, containerName)
Expand Down
3 changes: 2 additions & 1 deletion pkg/container/podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package container

import (
"fmt"
"os"

"github.com/apex/log"
"github.com/z0mbix/rolecule/pkg/command"
Expand Down Expand Up @@ -36,7 +37,7 @@ func (p *PodmanEngine) Exec(containerName string, envVars map[string]string, cmd
if len(envVars) > 0 {
for k, v := range envVars {
execArgs = append(execArgs, "--env")
execArgs = append(execArgs, fmt.Sprintf("%s=%s", k, v))
execArgs = append(execArgs, fmt.Sprintf("%s=%s", k, os.ExpandEnv(v)))
}
}

Expand Down
2 changes: 1 addition & 1 deletion testing/ansible/roles/sysctl/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
value: "{{ ansible_swappiness }}"
state: present
tags:
- provision
- provision

- name: output swappiness
ansible.builtin.debug:
Expand Down

0 comments on commit 396f2d5

Please sign in to comment.