Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Chef | cookbook | sudo権限付与用のcookbookの作成

概要

sudo権限付与用のcookbookの作成

詳細

sudo権限付与用のcookbookを作成します。
opscodeのsudo cookbookを利用します。
http://community.opscode.com/cookbooks/sudo

手順

# kitchenの作成
mkdir grant_sudo
cd grant_sudo
knife solo init .

# Vagrantfile生成
vagrant init

# Vagrantfile編集 ※詳細は後述
vi Vagrantfile

# cookbookのテンプレート生成
knife cookbook create grant_sudo -o site-cookbooks/

# レシピの編集 ※詳細は後述

# Berksfileの作成
echo > Berksfile

# Berksfileの編集 ※詳細は後述
vi Berksfile

# Berksfileに記述したcookbookのインストール
berks install --path cookbooks

# vagrant起動
vagrant up
vagrant provision

# 動作確認
vagrant ssh

Jenkinsから動作確認

新しいジョブを作成し、シェルの実行で

sudo service --status-all | head -1

を設定。実行した結果のコンソールが以下。

Started by user anonymous
[workspace] $ /bin/sh -xe /tmp/tomcat6-tomcat6-tmp/hudson7847300529111272776.sh
+ head -1
+ sudo service --status-all
 [ + ]  apparmor
 [ ? ]  atd
Finished: SUCCESS

Vagrantfile

※apt_get_update、git、jenkins_tomcatについては詳細設定を記述しませんが
apt-get updateをして、gitとjenkins-tomcatをインストールしています。
jenkinsからtomcat6ユーザーでsudoを実行出来るかどうか検証するためにこのような内容にしています。

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.provider :virtualbox do |vb|
    vb.name = "grant_sudo"
    vb.customize ["modifyvm", :id, "--memory", 512]
  end

  config.vm.box = "ubuntu-12.04-x64"
  config.omnibus.chef_version = "11.6.0"
  config.vm.network :private_network, ip: "192.168.33.22"
  config.berkshelf.enabled = false
  
  config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = ["./cookbooks", "./site-cookbooks"]
    chef.add_recipe "apt_get_update"
    chef.add_recipe "git"
    chef.add_recipe "jenkins_tomcat"
    chef.add_recipe "sudo"
    chef.add_recipe "grant_sudo"
  end
end

Berksfile

site :opscode

cookbook 'sudo'

site-cookbooks/grant_sudo/attributes/default.rb

default['authorization']['sudo']['groups']            = ['vagrant', 'tomcat6']
default['authorization']['sudo']['users']             = ['vagrant', 'tomcat6']
default['authorization']['sudo']['passwordless']      = true
default['authorization']['sudo']['sudoers_defaults']  = ['!lecture,tty_tickets,!fqdn']

site-cookbooks/grant_sudo/recipe/default.rb

include_recipe 'sudo::default'