0

I have a chef recipe written for creating three users, adding them to a group and writing them to a sudoers file.

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

I am brand new to Chef, and I need some help in writing a suitable attribute file for this recipe(/cookbook/User/attributes/default.rb). I have tried everything I know, but nothing is working out for me. Also I would like to know if the case statements can be included in the attribute file.

Note: I am running Chef in local mode.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
  • I'm unsure of what you try to accomplish. Sidenote Chef is not aimed at interactive process. If you want to create the three users in a loop that's ok. If you want attributes, what would you put into attributes (user names ? uid, etc ?) – Tensibai Jan 20 '15 at 11:24
  • That's exactly what I'm trying to ask. I don't know what to include in the attributes. I created the interactive process only for now. I will remove it later from the recipe. I just want to know how I should write an attributes file for a recipe like this. Or should I change the recipe itself? –  Jan 20 '15 at 11:31
  • Ok, see the attribute as a variable, which properties would you like to be variables ? I may give an answer but I'm really not sure it is what you want. Maybe you can explain what is your expected result. – Tensibai Jan 20 '15 at 11:33
  • I have stated that already. I want to create a group in my linux system, add three users to it, and write them into the sudoers file. I want to see the users' directories created in the /home directory. –  Jan 20 '15 at 11:34
  • I want the user names, group name and the env variable to be included in the attributes. –  Jan 20 '15 at 11:40

1 Answers1

0

in attributes/default.rb:

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

in 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

You may take advantage of the sudoers cookbook which can manage that for you, but sticking to your requirements.

Tensibai
  • 15,557
  • 1
  • 37
  • 57
  • Thank you Tensibai :-) Also, is there a way I can use the env variable in the attributes file? I will have to create a front end later to accept user names from a form. –  Jan 20 '15 at 11:43
  • I don't get the point for this one... Creating only one of the three users ? – Tensibai Jan 20 '15 at 11:46
  • One at a time, yes. That's why the case statement. –  Jan 20 '15 at 11:48
  • In this case the loop us not necessary... I'll update the answer – Tensibai Jan 20 '15 at 11:50
  • You're welcome, consider marking the answer as accepted if it solves your problem to make it visible in the list – Tensibai Jan 20 '15 at 12:56