Tbpgr Blog

Ruby プログラマ tbpgr(てぃーびー) のブログ

Chef | data bagで各cookbookに依存しない変数を作成

概要

data bagで各cookbookに依存しない変数を作成

詳細

data bagで各cookbookに依存しない変数を作成します。

手順
$ mkdir data_bag
$ cd data_bag
$ vagrant init
$ vi Vagrantfile # 編集内容は後述

# キッチン作成
$ knife solo init .
Creating kitchen...
Creating knife.rb in kitchen...
Creating cupboards...

# bashrc向けのレシピは下記記事を元にします
# cookbook_fileでファイルを配置
# http://d.hatena.ne.jp/tbpg/20131031/1383231038

# data_bugsにvagrantユーザーの情報を追加
$ mkdir users
$ cd users/
$ cat <<EOF> vagrant.json
> {
>   "id" : "vagrant",
>   "name" : "vagrant",
>   "group" : "vagrant",
>   "password" : "vagrant"
> }
> EOF

# レシピを編集 => 後述
$ vi ../site-cookbook/bash_profile/recipe/default.rb

# vagrant 起動
$ vagrant up

# 設定の確認
$ vagrant ssh
vagrant@precise64:~$ cat .bash_profile
export LANG="ja_JP.UTF-8"
alias ll='ls -l'
alias ls='ls -F --color=auto --show-control-char'
site-cookbook/bash_profile/recipe/default.rb
#
# Cookbook Name:: bash_profile
# Recipe:: default
#
# Copyright 2013, no name
#
# All rights reserved - Do Not Redistribute
#
data_ids = data_bag('users')
data_ids.each do |id|
  user = data_bag_item('users', id)
  cookbook_file "/home/#{user['name']}/.bash_profile" do
    source ".bash_profile"
    owner user['name']
    group user['group']
    mode "0755"
  end
end
Vagrantfile
# -*- 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.box = "ubuntu-12.04-x64"
  config.vm.network :private_network, ip: "192.168.33.17"
  config.omnibus.chef_version = "11.6.0"
  config.ssh.username = "vagrant"

  config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = ["./cookbooks", "./site-cookbooks"]
    chef.data_bags_path = "data_bags"
    chef.add_recipe "bash_profile"
  end
end