是否可以使用Chef访问其他用户的注册表?我有作为系统运行的chef-client,我想修改User1的注册表?有没有办法做到这一点?
registry_key资源提供了访问HKEY_Users的方法,但我看不到将用户名映射到SID的方法。
答案 0 :(得分:4)
最终温和错综复杂,看着它让我感到畏缩。但它似乎有效!
我想通过注册表修改其他用户的环境变量,如this Server Fault answer中所述,但我也想用Chef创建用户。问题是Windows在用户登录之前不会为用户创建注册表配置单元。
如果相关用户肯定存在,您可以跳到修改用户的注册表项。
Chef内置execute
和batch
资源似乎不支持提供密码,因此似乎不能在Windows上使用user
属性。但the official Chef Windows cookbook包含 包含用于指定用户密码的属性的windows_task
资源。
现在的问题是授予相关用户“本地安全策略”权限以“作为批处理作业登录”。为此,我们可以使用SecEdit。
确保您的食谱取决于官方厨师的Windows食谱;在你的食谱的 metadata.rb 文件中添加:
depends "windows"
这是Chef食谱代码:
group "BatchJobUsers" do
append true
members ["AnotherUser"]
action :create
end
cookbook_file "BatchJobUsers-AddBatchLogonRight.inf" do
path "#{ENV['TEMP']}\\BatchJobUsers-AddBatchLogonRight.inf"
action :create_if_missing
end
execute "secedit" do
cwd "#{ENV['TEMP']}"
command "secedit /configure /db secedit.sdb /cfg BatchJobUsers-AddBatchLogonRight.inf"
end
windows_task "force-creation-of-AnotherUser-user-registry-hive" do
command "echo Force creation of AnotherUser user registry hive"
user "AnotherUser"
password "password-for-another-user"
action [:create, :run]
end
windows_task "force-creation-of-AnotherUser-user-registry-hive" do
action :delete
end
您需要将文件添加到名为 BatchJobUsers-AddBatchLogonRight.inf 的 COOKBOOK / files / default 目录中;它应该包含以下内容:
[Unicode]
Unicode=yes
[Version]
signature="$CHICAGO$"
Revision=1
[Privilege Rights]
SeBatchLogonRight = "BatchJobUsers"
确保您的食谱取决于官方厨师的Windows食谱;在你的食谱的 metadata.rb 文件中添加:
depends "windows"
在您的食谱中,添加以下行:
::Chef::Recipe.send(:include, Windows::RegistryHelper)
然后您可以在食谱中使用resolve_user_to_sid
功能,如下所示:
get_user_sid = lambda { resolve_user_to_sid("USER_NAME") }
registry_key "Create environment variable registry keys" do
key lazy { "HKEY_USERS\\#{ get_user_sid.call }\\Environment"
values [{
:name => "Variable",
:type => :string,
:data => "variable_data"
}]
recursive true
action :create
end
key
属性必须进行延迟评估(即在运行配方的Chef的收敛阶段进行评估)以处理不存在的用户。