Issue with brew cask installation via salt-call

I am trying to use SaltStack to configure macOS, for this i am using hombrew obviously to install various formula and cask.

$ sudo salt-call --local --file-root=/tmp/gitrepo state.apply top.sls

and then in one of virtualbox.sls file use cmd.run to install application using cask

virtualbox:
  cmd.run:
    - name: brew cask install virtualbox

While its works out perfectly for formula, its throwing Running Homebrew as root user error for cask

  • This is primarily because in default conditions, salt-call has to run with sudo and in turn brew cask install then gives error as effectively it run as root user

Anybody has encountered similar issue? If yes, what steps or other method you have used to install cask using saltstack?

I don’t use SaltStack, but the obvious next step is to try running brew with su:

virtualbox:
  cmd.run:
    - name: su -l <brew_user> brew cask install virtualbox

su -l has another benefit: It simulates the full login process, so Homebrew gets pretty much the same environment that you’d expect from an interactive command line. I can’t speak for SaltStack, but sudo wipes the environment of its child processes, so Homebrew will see a very different environment, which may result in crashes and other weird behavior.