Chef11でユーザーの追加とグループへのメンバー登録

少しずつChefのノウハウがたまってきたので、備忘録。
user追加の時に、今まではchefを使っているといっても、シェルスクリプトを便利に回すだけだった状態。
追加も変更も流せるっていう記述を見て、chefの提供しているuser追加や変更を全面的に取り入れてみた。

でも、chefサーバまでの運用はできていないので、chef-soloを使っていて、databagは使っていない。

参考にしたURL

Chef公式ドキュメントのuser http://docs.opscode.com/resource_user.html

Chef公式ドキュメントのgroup http://docs.opscode.com/resource_group.html

Chef を始める #3 – ユーザ作成を自動化する 〜 Data Bags を使ってみる 〜 http://dev.classmethod.jp/cloud/aws/chef-user-create-data-bags/
Databagは使わないけれど、気になるので・・・

実現したいこと

・作成ディレクトリを/home 直下ではなく、/home/staff/直下に。
・2ユーザー作る。rebine,tk
・基本グループはwheelに入っていて、usermod -G でいつもは指定している追加グループにApache(rpmインストール) を入れたい。
・パスワードログインはさせない代わりに、事前にリポジトリに入れてある公開鍵を入れたい。

作ったcookbook

cookbook adduser

./attributes
./attributes/default.rb
./files/default/rebine
./files/default/rebine/authorized_keys
./files/default/tk
./files/default/tk/authorized_keys
./recipes
./recipes/default.rb

treeを入れてなかったので、findで構成物列挙。

recipe

directory '#{node[:useradd][:staffdir]}' do
  owner "root"
  group "root"
  mode  00755
  action :create
end
 
user 'rebine' do
  comment  'rebine'
  uid      501
  gid      10
  home     "/home/staff/rebine"
  shell    '/bin/bash'
  password nil
  supports :manage_home => true
  action   [:create, :manage]
end
 
user 'tk' do
  comment  'tk'
  uid      502
  gid      10
  home     "/home/staff/tk"
  shell    '/bin/bash'
  password nil
  supports :manage_home => true
  action   [:create, :manage]
end
 
group 'apache' do
  action :modify
  members node[:useradd][:staff]
  append true
end
 
node[:useradd][:staff].each do |staff_name|
 
 
  directory "#{node[:useradd][:staffdir]}/#{staff_name}/.ssh" do
    owner "#{staff_name}"
    group "wheel"
    mode  00700
    action :create
  end
 
  cookbook_file "#{node[:useradd][:staffdir]}/#{staff_name}/.ssh/authorized_keys" do
    source "#{staff_name}/authorized_keys"
    owner "#{staff_name}"
    group "wheel"
    mode 0600
  end
 
 
end
 
cookbook_file "/etc/ftpusers" do
  source "ftpusers"
  owner "root"
  group "root"
  mode 0644
end

#{node[:useradd][:staffdir]} というのは、/home直下にずらっと並べたくなかったので、わざわざ別ディレクトリを作ってる。
group apache はrpmで入れた後を想定しているので、:modify に。とはいえ、Apacheを先に入れないレシピの時には[:create :modify]をしてる。
あと、公開鍵のディレクトリはどうしてもDatabagがらみになるので、作成するレシピにした。
ftpusersを上書きしているのは、複数ユーザーが登録されているグループのユーザーを追加すると、必ずこのファイルの修正が求められたので、FTPは使わなくなってきたんだけれど、名残として。

attributes

set[:useradd][:staff] = [ "rebine" , "tk"]
set[:useradd][:staffdir] = "/home/staff"
set[:useradd][:uid][:rebine] = "501"
set[:useradd][:uid][:tk] = "502"

files

./files/default/rebine/authorized_keys
これは単純に公開鍵を入れてあるだけなので割愛。

実行例

Starting Chef Client, version 11.10.0
Compiling Cookbooks...
Converging 9 resources
Recipe: useradd::default
  * directory[#{node[:useradd][:staffdir]}] action create (up to date)
  * user[rebine] action create (up to date)
  * user[rebine] action manage (up to date)
  * user[tk] action create (up to date)
  * user[tk] action manage (up to date)
  * group[apache] action modify (up to date)
  * directory[/home/staff/rebine/.ssh] action create (up to date)
  * cookbook_file[/home/staff/rebine/.ssh/authorized_keys] action create (up to date)
  * directory[/home/staff/tk/.ssh] action create (up to date)
  * cookbook_file[/home/staff/tk/.ssh/authorized_keys] action create
    - create new file /home/staff/tk/.ssh/authorized_keys
    - update content in file /home/staff/tk/.ssh/authorized_keys from none to ec6194
        --- /home/staff/tk/.ssh/authorized_keys   2014-05-24 19:47:43.999194281 +0900
        +++ /tmp/.authorized_keys20140524-8028-vwf7cc   2014-05-24 19:47:44.000194281 +0900
        @@ -1 +1,4 @@
        +ssh-dss AAAAB3NzaC1kc3MAAACBALpyxq ()
        +ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQ ()
        +ssh-dss AAAAB3NzaC1kc3MAAACBALQT5N ()
    - change mode from '' to '0600'
    - change owner from '' to 'tk'
    - change group from '' to 'wheel'
 
  * cookbook_file[/etc/ftpusers] action create (up to date)
 
Running handlers:
Running handlers complete
 
Chef Client finished, 1/11 resources updated in 0.569500971 seconds

これは、すでにrebineが存在していて、新たにtkを追加したときに取れた出力結果。

失敗例

Starting Chef Client, version 11.10.0
Compiling Cookbooks...
Converging 9 resources
Recipe: useradd::default
  * directory[#{node[:useradd][:staffdir]}] action create (up to date)
  * user[rebine] action create (up to date)
  * user[rebine] action manage (up to date)
  * user[tk] action create (up to date)
  * user[tk] action manage (up to date)
  * group[apache] action modify (up to date)
  * directory[["/home/staff"]/rebine/.ssh] action create
    * Parent directory ["/home/staff"]/rebine does not exist, cannot create ["/home/staff"]/rebine/.ssh
================================================================================
Error executing action `create` on resource 'directory[["/home/staff"]/rebine/.ssh]'
================================================================================
 
 
Chef::Exceptions::EnclosingDirectoryDoesNotExist
------------------------------------------------
Parent directory ["/home/staff"]/rebine does not exist, cannot create ["/home/staff"]/rebine/.ssh
 
 
Resource Declaration:
---------------------
# In /usr/chef-repo/cookbooks/useradd/recipes/default.rb
 
 58:   directory "#{node[:useradd][:staffdir]}/#{staff_name}/.ssh" do
 59:     owner "#{staff_name}"
 60:     group "wheel"
 61:     mode  00700
 62:     action :create
 63:   end
 64: 
 
 
 
Compiled Resource:
------------------
# Declared in /usr/chef-repo/cookbooks/useradd/recipes/default.rb:58:in `block in from_file'
 
directory("["/home/staff"]/rebine/.ssh") do
  provider Chef::Provider::Directory
  action [:create]
  retries 0
  retry_delay 2
  path "[\"/home/staff\"]/rebine/.ssh"
  cookbook_name :useradd
  recipe_name "default"
  owner "rebine"
  group "wheel"
  mode 448
end
 
 
 
 
Running handlers:
[2014-05-24T19:46:34+09:00] ERROR: Running exception handlers
Running handlers complete
 
[2014-05-24T19:46:34+09:00] ERROR: Exception handlers complete
[2014-05-24T19:46:34+09:00] FATAL: Stacktrace dumped to /tmp/chef-solo/cache/chef-stacktrace.out
Chef Client failed. 0 resources updated in 0.519964345 seconds
[2014-05-24T19:46:34+09:00] ERROR: directory[["/home/staff"]/rebine/.ssh] (useradd::default line 58) had an error: Chef::Exceptions::EnclosingDirectoryDoesNotExist: Parent directory ["/home/staff"]/rebine does not exist, cannot create ["/home/staff"]/rebine/.ssh
[2014-05-24T19:46:34+09:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)

この失敗は何かというと、chefの0.10 時代のattributesの記述で[]を全ての値に入れて、配列にしていたから。

set[:useradd][:staffdir] = ["/home/staff"]
 ↓
set[:useradd][:staffdir] = "/home/staff"

今はこのように修正したので、出ていない。