0

我有一个厨师食谱,用于创建三个用户,将它们添加到一个组并将它们写入一个 sudoers 文件。

group "usergroup" do
  gid 2000
end

print "User1 or User2 or User3?"
env=$stdin.gets.chomp
case env
when "User1"
  user "User1" do
    uid 150
    gid "usergroup"
    home "/home/User1"
    shell "/bin/bash"
  end

  directory "/home/User1" do
    owner "User1"
    group "usergroup"
    mode "0777"
    action :create
  end

  execute "echo" do
    command "echo 'User1 ALL=(ALL) ALL' >> /etc/sudoers"
    not_if "grep -F 'User1 ALL=(ALL) ALL' /etc/sudoers"
  end

when "User2"
  user "User2" do
    uid 250
    gid "usergroup"
    home "/home/User2"
    shell "/bin/bash"
  end

  directory "/home/User2" do
    owner "User2"
    group "usergroup"
    mode "0777"
    action :create
  end

  execute "echo" do
    command "echo 'User2 ALL=(ALL) ALL' >> /etc/sudoers"
    not_if "grep -F 'User2 ALL=(ALL) ALL' /etc/sudoers"
  end

when "User3"
  user "User3" do
    uid 350
    gid "usergroup"
    home "/home/User3"
    shell "/bin/bash"
  end

  directory "/home/User3" do
    owner "User3"
    group "usergroup"
    mode "0777"
    action :create
  end

  execute "echo" do
    command "echo 'User3 ALL=(ALL) ALL' >> /etc/sudoers"
    not_if "grep -F 'User3 ALL=(ALL) ALL' /etc/sudoers"
  end
end

我是 Chef 的新手,我需要一些帮助来为这个食谱编写一个合适的属性文件(/cookbook/User/attributes/default.rb)。我已经尝试了我所知道的一切,但没有什么对我有用。另外我想知道case语句是否可以包含在属性文件中。

注意:我在本地模式下运行 Chef。

4

1 回答 1

0

attributes/default.rb

default['myusers'] = ['user1','user2','user3']
default['mygroup'] = "usergroup" 
default['myenv'] = 2 #no quotes to keep an integer

recipe/default.rb

group node['mygroup'] do
  gid 2000
end

currentUser = node['myusers'][node['myenv'] - 1] #arrays start at 0, doing -1 for 2 pointing to second user
user currentUser do
  gid node['mygroup']
  home "/home/#{currentUser}"
end
execute "sudoers for #{currentUser}" do
  command "echo '#{currentUser} ALL=(ALL) ALL' >> /etc/sudoers"
  not_if "grep -F '#{currentUser} ALL=(ALL) ALL' /etc/sudoers"
end

您可以利用 sudoers 食谱,它可以为您管理,但要遵守您的要求。

于 2015-01-20T11:41:47.460 回答