Skip to content

Instantly share code, notes, and snippets.

@gyohk
Last active May 30, 2017 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gyohk/0b77604db70d4e94328c to your computer and use it in GitHub Desktop.
Save gyohk/0b77604db70d4e94328c to your computer and use it in GitHub Desktop.
knife コマンドでリモート(またはゲスト)環境にchef をインストールする
Windowsの場合、事前にcwRsyncを導入しておく。
ただし、インストール直後は鍵ファイルのパーミッションについて警告が表示されるため、以下の記事を参考に、/etc/fstabに設定を行う。
https://www.itefix.net/content/permissions-filesdirectories-are-clutteredmixed
http://d.hatena.ne.jp/hatecotton/20101016/1287185095
また、SSH関連の設定を保存するため、以下のディレクトリを作成しておくこと。
C:\cwRsync_5.5.0_x86_Free\home\(ユーザ名)
※knife-solo が動作するのは、chefDK1.2.22 までのようです。
chefdkのインストール時にknife-soloは含まれていないので、
以下のコマンドを実行してknife-soloをインストールしておく。
chef gem install knife-solo
以下の場所にパスを通しておく。
C:\cwRsync_5.5.0_x86_Free\bin
c:\users\(ユーザー名)\appdata\local\chefdk\gem\ruby\2.3.0\bin
以下のコマンドで、リモートにChefのインストールを行う。
knife solo prepare vagrant@127.0.0.1 -p2222 -i "(秘密鍵のパス)" --bootstrap-version 12
なお、対象がVagrantの場合、以下のコマンドで秘密鍵の場所を確認できる。
(windowsの場合、パス区切り文字を\に変更しないとダメかも)
vagrant ssh-config
Vagrant環境の場合、以下のコマンドで登録しておけば、コマンドをいくらか簡略化できる。
vagrant ssh-config [ノード名] >> C:/Users/(ユーザ名)/.ssh/config
knife solo prepare [ノード名]
複数ノードに適用する場合は xargs を使用する。
echo node01 node02 | xargs -n 1 knife solo cook
■chefの初期化
chef gem install knife-solo
knife solo init --berkshelf .
■cookbookの作成
knife cookbook create dstat -o site-cookbooks
■レシピの編集
site-cookbooks/dstat/recipes/deafult.rb の編集
package "dstat" do
action :install
end
■Node の編集
{"run_list":[
"recipe[dstat]"
]}
■レシピの実行
knife solo cook vagrant@127.0.0.1 -p2222 -i "(秘密鍵のパス)" [-o dstat::default] --verbose
※1
Windows環境では、事前に以下のコマンドを実行しておかないとエラーになる。
set RUBYOPT=-EUTF-8
または、システム環境変数で、変数名「RUBYOPT」変数値「-EUTF-8」を設定するのが楽。
※2
ChefDKのgem が若干古いため、追加のgemがインストールできない可能性がある。
その場合、以下のコマンドを実行する。
---
chef gem install rubygems-update --source http://rubygems.org/
update_rubygems
---
※3
Windows で、ノード指定してSSHにアクセスする設定はこちら。
vagrant ssh-config node01 >> C://Users/(ユーザ名)/.ssh/config
※4
vagrant以外の環境で、デプロイユーザ(sudo周りの設定)はこちら。
http://tsuchikazu.net/vps_chef_solo/
http://recipe.kc-cloud.jp/archives/4361
(おまけ) vagrant メモ
※Windowsでは、コマンドプロンプトを管理者権限で起動しないと、ゲストOS上でsymlink が作成できない。
■グローバルステータス
vagrant global-status --prune
■サンドボックスモード
$ vagrant plugin install sahara
# sandboxモード実行
$ vagrant sandbox on
# ロールバック
$ vagrant sandbox rollback
# コミット
$ vagrant sandbox commit
# sandboxモード終了(コミットしていない変更は削除)
$ vagrant sandbox off
# sandboxのステータス確認
$ vagrant sandbox status
■vagrant-omnibus (chefの自動インストール)
vagrant plugin install vagrant-omnibus
--
Vagrant.configure("2") do |config|
config.omnibus.chef_version = :latest
end
--
■vagrant-cachier (プロビジョニングに使用したファイルのキャッシュ)
vagrant plugin install vagrant-cachier
--
Vagrant.configure("2") do |config|
if Vagrant.has_plugin?("vagrant-cachier")
config.cache.scope = :box
end
end
--
■vagrant-hostsupdater (hostsファイルのアップデート)
vagrant plugin install vagrant-hostsupdater
---
Vagrant.configure("2") do |config|
config.vm.network :private_network, ip: "192.168.3.10"
config.vm.hostname = "www.testing.de"
end
---
■環境ごとにレシピの切り分け
if node.chef_environment == 'local'
# 環境ごとの処理
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment