Chef-solo-3-chef add multi user with data_bag include authorized_keys for ssh

  • Category: 電腦相關
  • Last Updated: Wednesday, 27 May 2015 14:05
  • Published: Tuesday, 26 May 2015 11:46
  • Written by sam

get latest code at

https://github.com/s0m1in/0522.git

create data bag

root@packer-virtualbox-iso-1411922062:/tmp/0522# knife solo data bag create users depo0

If occur this error.

ERROR: RuntimeError: Please set EDITOR environment variable
root@packer-virtualbox-iso-1411922062:/tmp/0522# export EDITOR=vim

than again

root@packer-virtualbox-iso-1411922062:/tmp/0522# knife solo data bag create users depo0
{
  "id": "depo0",
  "password": "$1$pXzsdwHH$1ZRSjvl/eDKO7zZCLJZp5/",
  "comment": "depo0"
}

 

or you can fill detail like

{
  "id": "sam",
  "password" : "abaa$#Va",
  "uid"      : "number",
  "shell"    : "\/bin\/bash",  
  "comment"  : "sam"
}

create cookbook to use

root@packer-virtualbox-iso-1411922062:/tmp/0522# knife cookbook create user
root@packer-virtualbox-iso-1411922062:/tmp/0522# vi ./cookbooks/user/recipes/default.rb
users = data_bag('users')

users.each do |id|
  user = data_bag_item('users', id)
    home = "/home/#{id}"
    user(id) do
    comment     user['comment']
    password    user['password']
  end
end

or fill detail

users = data_bag('users')

users.each do |id|
  user = data_bag_item('users', id)
    home = "/home/#{id}"
    user(id) do
    uid         user['uid']
    shell       user['shell']
    comment     user['comment']
    password    user['password']
  end
end

add recipe to nodes

root@packer-virtualbox-iso-1411922062:/tmp/0522# cat ./nodes/192.168.1.85.json
{
  "run_list": [
"recipe[user]"
  ],
  "automatic": {
    "ipaddress": "192.168.1.85"
  }
}

and cook it

root@packer-virtualbox-iso-1411922062:/tmp/0522# knife solo cook -i /root/id_rsa 192.168.1.85
DL is deprecated, please use Fiddle
Running Chef on 192.168.1.85...
Checking Chef version...
Uploading the kitchen...
Generating solo config...
Running Chef...
Starting Chef Client, version 12.1.2
Compiling Cookbooks...
Converging 3 resources
Recipe: user::default
  * user[depo2] action create
    - create user depo2
  * user[depo1] action create
    - create user depo1
  * user[depo0] action create
    - create user depo0

Running handlers:
Running handlers complete
Chef Client finished, 3/3 resources updated in 1.759522003 seconds