LoginSignup
3

More than 5 years have passed since last update.

Chefのインストールからknife-zeroでサーバ自動構築まで

Posted at

前提として、サーバ側にはsudoが使えるユーザを作っておく。

↓以下仮定
サーバのアドレス:test-server.com
sudoユーザ:test_user
node名:test_node
cookbook:test_cook

Chef Development Kit ダウンロードしてインストール

まずはダウンロードしてインストール
https://downloads.chef.io/chefdk

MACでもWindowsでもchefのコマンドは一緒。

knife-zeroインストール

chef gem install knife-zero --no-document

リポジトリ作成

chef generate repo chef-repo

↓こんな感じのものが作られます。

chef-repo
├── LICENSE
├── README.md
├── chefignore
├── cookbooks
│   ├── README.md
│   └── example
│       ├── README.md
│       ├── attributes
│       │   └── default.rb
│       ├── metadata.rb
│       └── recipes
│           └── default.rb
├── data_bags
│   ├── README.md
│   └── example
│       └── example_item.json
├── environments
│   ├── README.md
│   └── example.json
└── roles
    ├── README.md
    └── example.json

kinfe-zeroを使用するための設定

cd chef-repo
mkdir .chef
vi .chef/knife.rb

knife[:automatic_attribute_whitelist]を入れないと、
nodeファイルの中身が大量に作られるので、これぐらいに収めてみる。
fqdn,hostname,roles,recipes
らへんがあれば動くかと思う。

chef-repo/.chef/knife.rb
local_mode true
knife[:automatic_attribute_whitelist] = [
  "fqdn/",
  "os/",
  "os_version/",
  "hostname",
  "ipaddress/",
  "roles/",
  "recipes/",
  "ipaddress/",
  "platform/",
  "platform_version/",
  "platform_version/",
  "cloud/",
  "cloud_v2/",
  "chef_packages/"
]

nodeにサーバ追加

sshでサーバ側につながり、chefがインストールされ、nodeファイルが作られる
node名:test_node

knife zero bootstrap test-server.com:<ポート> -x test_user --sudo -N test_node
パスワードを求められる

node確認

knife node list
test_node

フォルダ構成はこんな感じになったよ

chef-repo
├── LICENSE
├── README.md
├── chefignore
├── cookbooks
│   ├── README.md
│   └── example
│       ├── README.md
│       ├── attributes
│       │   └── default.rb
│       ├── metadata.rb
│       └── recipes
│           └── default.rb
├── data_bags
│   ├── README.md
│   └── example
│       └── example_item.json
├── environments
│   ├── README.md
│   └── example.json
├── nodes
│   └── test_node.json
└── roles
    ├── README.md
    └── example.json

クックブック作成

chef generate cookbook cookbooks/sample_cook

レシピは今回は適当

chef-repo/cookbooks/sample_cook/recipes/default.rb
package 'vim' do
    action :install
end

先程作ったnodeに、実行させるcookbookを指定

knife node run_list add 'test_node' 'sample_cook'

サーバに対してレシピ実行

knife zero converge name:test_ -x test_user --sudo

fqdnの値でsshに接続される。
Azureとかでなったんだが、fqdnで接続出来ない場合、

-a knife_zero.host

を追加すれば、knife_zero.hostの値でssh接続を行う

以上

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3