LoginSignup
2
2

More than 5 years have passed since last update.

Chef 12対応版Chef Workstation構築手順〜CentOS7〜

Posted at

この記事は、私の個人ブログ「クラウド時代のシステム管理」の記事を転記したものです。

構成管理ツール「Chef」環境に登場する開発環境であるChef Workstationの構築手順を紹介します。今回は私の周りではメジャーなCentOS7の上に構築します。ここでは簡単のため、前回の記事で構築したChef Server上に開発用の一般ユーザーを作成して、。そのホームディレクトリの下に開発観客を構築します。手順は本家サイトの手順に従って行います。

1. Chef Development Kit (Chef DK)のインストールとユーザー作成

ChefのCookbookを開発するためのSDKやコマンドラインツールがセットになった「Chef Development Kit」(以下Chef DK)をダウンロードをします。そしてRPMコマンドでインストールします。

# cd /tmp
# curl -LO https://packages.chef.io/stable/el/7/chefdk-0.14.25-1.el7.x86_64.rpm
# rpm -Uvh chefdk-0.14.25-1.el7.x86_64.rpm
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:06 --:--:--     0
100  137M  100  137M    0     0  2396k      0  0:00:58  0:00:58 --:--:-- 2822k
# rpm -Uvh chefdk-0.14.25-1.el7.x86_64.rpm
警告: chefdk-0.14.25-1.el7.x86_64.rpm: ヘッダー V4 DSA/SHA1 Signature、鍵 ID 83ef826a: NOKEY
準備しています...              ################################# [100%]
更新中 / インストール中...
   1:chefdk-0.14.25-1.el7             ################################# [100%]
Thank you for installing Chef Development Kit!

さらに、Cookbookの管理などで必要になるgitとunzipをインストールしておきます。

# yum install -y git unzip
読み込んだプラグイン:fastestmirror
(以下省略)

ChefのCookbook作成などの作業には管理者権限は不要なので、開発用の一般ユーザーを作成します。ここではChefにログインするときに作ったユーザーと同じ名前にしておきます。(例では「chef-admin」)

# useradd chef-admin
# passwd chef-admin
ユーザー chef-admin のパスワードを変更。
新しいパスワード:
新しいパスワードを再入力してください:
passwd: すべての認証トークンが正しく更新できました。

2. Chef開発用ユーザーの環境設定

chef-adminユーザーに切り替えた後、Chef DKが正常にインストールされているか確認するコマンドを実行します。

# su - chef-admin
$ chef verify
Running verification for component 'berkshelf'
Running verification for component 'test-kitchen'
Running verification for component 'tk-policyfile-provisioner'
Running verification for component 'chef-client'
Running verification for component 'chef-dk'
Running verification for component 'chef-provisioning'
Running verification for component 'chefspec'
Running verification for component 'generated-cookbooks-pass-chefspec'
Running verification for component 'rubocop'
Running verification for component 'fauxhai'
Running verification for component 'knife-spork'
Running verification for component 'kitchen-vagrant'
Running verification for component 'package installation'
Running verification for component 'openssl'
Running verification for component 'inspec'
Running verification for component 'delivery-cli'
Running verification for component 'git'
Running verification for component 'opscode-pushy-client'
Running verification for component 'chef-sugar'
Running verification for component 'knife-supermarket'
.................
---------------------------------------------
Verification of component 'chefspec' succeeded.
Verification of component 'rubocop' succeeded.
Verification of component 'knife-spork' succeeded.
Verification of component 'openssl' succeeded.
Verification of component 'delivery-cli' succeeded.
Verification of component 'opscode-pushy-client' succeeded.
Verification of component 'berkshelf' succeeded.
Verification of component 'tk-policyfile-provisioner' succeeded.
Verification of component 'chef-dk' succeeded.
Verification of component 'fauxhai' succeeded.
Verification of component 'inspec' succeeded.
Verification of component 'knife-supermarket' succeeded.
Verification of component 'test-kitchen' succeeded.
Verification of component 'chef-provisioning' succeeded.
Verification of component 'kitchen-vagrant' succeeded.
Verification of component 'git' succeeded.
Verification of component 'package installation' succeeded.
Verification of component 'chef-client' succeeded.
Verification of component 'chef-sugar' succeeded.
Verification of component 'generated-cookbooks-pass-chefspec' succeeded.

出力結果の後半で、すべてのコンポーネントの結果が「succeeded」となっていることを確認します。次に、このユーザーでのデフォルトRubyのパスをChef DKと一緒にインストールされているものに設定します。そのためには、~/.bash_profileに1行追記し、~/.bash_profileの設定を再読み込みします。

$ echo 'eval "$(chef shell-init bash)"' >> ~/.bash_profile
$ source ~/.bash_profile
$ which ruby
/opt/chefdk/embedded/bin/ruby

これでchef-adminユーザーのデフォルトrubyがChef DKに同梱されたものになりました。なお、実際に設定される環境変数などを確認すると、以下のようになります。

$ chef shell-init bash
export PATH="/opt/chefdk/bin:/home/chef-admin/.chefdk/gem/ruby/2.1.0/bin:/opt/chefdk/embedded/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/chef-admin/.local/bin:/home/chef-admin/bin"
export GEM_ROOT="/opt/chefdk/embedded/lib/ruby/gems/2.1.0"
export GEM_HOME="/home/chef-admin/.chefdk/gem/ruby/2.1.0"
export GEM_PATH="/home/chef-admin/.chefdk/gem/ruby/2.1.0:/opt/chefdk/embedded/lib/ruby/gems/2.1.0"
_chef_comp() {
    local COMMANDS="exec env gem generate shell-init install update push push-archive show-policy diff provision export clean-policy-revisions clean-policy-cookbooks delete-policy-group delete-policy undelete verify"
    COMPREPLY=($(compgen -W "$COMMANDS" -- ${COMP_WORDS[COMP_CWORD]} ))
}
complete -F _chef_comp chef

3. Starter Kitの展開とChef Serverとの接続設定

Chef DKの環境を簡単に整えるために、Chef ServerのWeb管理画面からStarter Kitをダウンロードします。そのために、Web管理画面にログイン後、画面TOPの「Administration」をクリックします。

chefserver-organization01

次に、画面左にある「Organization」をクリック後、表示された組織一覧から前回の記事で作成した組織を選びます。その後、画面左にある「Starter Kit」をクリックします。その後、画面中央に表示される「Download Starter Kit」ボタンをクリックします。

chefserver-organization02

ボタンをクリックすると警告が表示されますが、「Proceed」をクリックしてダウンロードを行います。

chefserver-starterkit

ここでダウンロードした「chef-starter.zip」をchef-adminユーザーのホームディレクトリに転送し、unizpコマンドで展開します。

$ ls
chef-starter.zip
$ unzip -q chef-starter.zip
$ ls
chef-repo  chef-starter.zip

ここで作られたchef-repoディレクトリでCookbookを作成できるよう初期化を行います。

$ chef generate app chef-repo
Installing Cookbook Gems:
Compiling Cookbooks...
Recipe: code_generator::app
  * directory[/home/chef-admin/chef-repo] action create (up to date)
  * template[/home/chef-admin/chef-repo/.kitchen.yml] action create
    - create new file /home/chef-admin/chef-repo/.kitchen.yml
    - update content in file /home/chef-admin/chef-repo/.kitchen.yml from none to 210d0b
    (diff output suppressed by config)
  * directory[/home/chef-admin/chef-repo/test/integration/default/serverspec] action create
    - create new directory /home/chef-admin/chef-repo/test/integration/default/serverspec
  * directory[/home/chef-admin/chef-repo/test/integration/helpers/serverspec] action create
    - create new directory /home/chef-admin/chef-repo/test/integration/helpers/serverspec
  * cookbook_file[/home/chef-admin/chef-repo/test/integration/helpers/serverspec/spec_helper.rb] action create_if_missing
    - create new file /home/chef-admin/chef-repo/test/integration/helpers/serverspec/spec_helper.rb
    - update content in file /home/chef-admin/chef-repo/test/integration/helpers/serverspec/spec_helper.rb from none to a226b8
    (diff output suppressed by config)
  * template[/home/chef-admin/chef-repo/test/integration/default/serverspec/default_spec.rb] action create_if_missing
    - create new file /home/chef-admin/chef-repo/test/integration/default/serverspec/default_spec.rb
    - update content in file /home/chef-admin/chef-repo/test/integration/default/serverspec/default_spec.rb from none to 1e81da
    (diff output suppressed by config)
  * template[/home/chef-admin/chef-repo/README.md] action create
    - update content in file /home/chef-admin/chef-repo/README.md from 2b98f1 to 6401b8
    (diff output suppressed by config)
  * directory[/home/chef-admin/chef-repo/cookbooks] action create (up to date)
  * directory[/home/chef-admin/chef-repo/cookbooks/chef-repo] action create
    - create new directory /home/chef-admin/chef-repo/cookbooks/chef-repo
  * template[/home/chef-admin/chef-repo/cookbooks/chef-repo/metadata.rb] action create
    - create new file /home/chef-admin/chef-repo/cookbooks/chef-repo/metadata.rb
    - update content in file /home/chef-admin/chef-repo/cookbooks/chef-repo/metadata.rb from none to b8624d
    (diff output suppressed by config)
  * cookbook_file[/home/chef-admin/chef-repo/cookbooks/chef-repo/chefignore] action create
    - create new file /home/chef-admin/chef-repo/cookbooks/chef-repo/chefignore
    - update content in file /home/chef-admin/chef-repo/cookbooks/chef-repo/chefignore from none to e394e1
    (diff output suppressed by config)
  * cookbook_file[/home/chef-admin/chef-repo/cookbooks/chef-repo/Berksfile] action create
    - create new file /home/chef-admin/chef-repo/cookbooks/chef-repo/Berksfile
    - update content in file /home/chef-admin/chef-repo/cookbooks/chef-repo/Berksfile from none to 5ec92e
    (diff output suppressed by config)
  * directory[/home/chef-admin/chef-repo/cookbooks/chef-repo/recipes] action create
    - create new directory /home/chef-admin/chef-repo/cookbooks/chef-repo/recipes
  * template[/home/chef-admin/chef-repo/cookbooks/chef-repo/recipes/default.rb] action create
    - create new file /home/chef-admin/chef-repo/cookbooks/chef-repo/recipes/default.rb
    - update content in file /home/chef-admin/chef-repo/cookbooks/chef-repo/recipes/default.rb from none to 2cdf1a
    (diff output suppressed by config)
  * directory[/home/chef-admin/chef-repo/cookbooks/chef-repo/spec/unit/recipes] action create
    - create new directory /home/chef-admin/chef-repo/cookbooks/chef-repo/spec/unit/recipes
  * cookbook_file[/home/chef-admin/chef-repo/cookbooks/chef-repo/spec/spec_helper.rb] action create_if_missing
    - create new file /home/chef-admin/chef-repo/cookbooks/chef-repo/spec/spec_helper.rb
    - update content in file /home/chef-admin/chef-repo/cookbooks/chef-repo/spec/spec_helper.rb from none to 945e09
    (diff output suppressed by config)
  * template[/home/chef-admin/chef-repo/cookbooks/chef-repo/spec/unit/recipes/default_spec.rb] action create_if_missing
    - create new file /home/chef-admin/chef-repo/cookbooks/chef-repo/spec/unit/recipes/default_spec.rb
    - update content in file /home/chef-admin/chef-repo/cookbooks/chef-repo/spec/unit/recipes/default_spec.rb from none to be7503
    (diff output suppressed by config)
  * execute[initialize-git] action run
    - execute git init .
  * cookbook_file[/home/chef-admin/chef-repo/.gitignore] action create
    - update content in file /home/chef-admin/chef-repo/.gitignore from f7d9f8 to dd37b2
    (diff output suppressed by config)

次に、秘密鍵などが保管されている.chefディレクトリをgit管理対象外にするための設定を行います。

$ echo '.chef' >> ~/chef-repo/.gitignore 

最後にChef ServerのSSL証明書(自己証明書)を信頼するよう設定します。

$ cd chef-repo
$ knife ssl fetch
WARNING: Certificates from chefserver will be fetched and placed in your trusted_cert
directory (/home/chef-admin/chef-repo/.chef/trusted_certs).

Knife has no means to verify these are the correct certificates. You should
verify the authenticity of these certificates after downloading.

Adding certificate for chefserver in /home/chef-admin/chef-repo/.chef/trusted_certs/chefserver.crt

以上で、Chef DKの環境が整いました。

4. Chef Serverとの接続確認

~/chef-repoディレクトリ以下でknifeコマンドを実行することで、Chef Serverと接続でき、クライアントや管理者ユーザーのリストが取得できることを確認します。

$ knife user list
chef-admin
$ knife client list
takanyan-validator

以上で、一般的なChef Workstation環境が出来上がりました。

2
2
0

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
2
2