1

How can I use Use Chef to set Environment Variables for different Windows Users?

I've tried using the Execute block like so:

execute 'setx DUMMYENV' do
    command 'setx DUMMYENV "THISISDUMMY"'
    user "UserA"
end

However this requires a password and I don't want the password in plaintext in any of the recipes so this solution doesn't seem viable.

I've also thought about doing something with registry_key like so:

registry_key 'HKEY_USERS\S-1-5-21-0123456789-012345678-...\Environment' do
   values [{name: 'DUMMYENV', type: :string, data: '1'},
           {name: 'ENVTEST', type: :string, data: 'TESTING'}
          ]
    action :create
end

The only problem I have with this is I'm not sure if the SID for each of the User accounts I'm trying to modify will change between machines. I can probably find a solution to loop through the available HKEY_Users and parse out the ones I'm looking for however this seems tedious.

Another option that I've started to look into is windows_env however I haven't gotten to test it much and I don't see a robust option to specify the user.

End goal is to set different environment variables for different users with Chef running as admin. Is there any easy way to do this with Chef?

Thanks in advance!

DanOpi
  • 133
  • 2
  • 12
  • 1
    Try this answer https://stackoverflow.com/questions/22945786/access-the-registry-of-another-user-with-chef/28224034#28224034 – Brandon Miller Jun 19 '18 at 14:18
  • I found that right after posting this. He still has a hard coded/plain text password in his first ```windows_task```. I know the users will exist on the machine however they may not be loaded into the hive (Not listed under HKEY_Users). I could load the corresponding hive for each of them and then unload it but not sure how to do this with Ruby. – DanOpi Jun 19 '18 at 14:30

1 Answers1

1

You can indeed do this with windows_env, though it isn't reflected in the documentation yet: https://github.com/chef/chef/blob/master/lib/chef/resource/windows_env.rb#L39

windows_env 'DUMMYENV' do
  value 'THISISDUMMY'
  user 'UserA'
end

Remember that if you're talking about scheduled tasks using env vars, you have to reboot before it will be visible.

coderanger
  • 52,400
  • 4
  • 52
  • 75
  • These env vars will be used by a service that is running under that same User Account. So that service should just need to be restarted, correct? Thank you for the information. Will be back after testing it out. – DanOpi Jun 20 '18 at 13:14