LoginSignup
1
0

More than 1 year has passed since last update.

ノードオブジェクトからChef-Zeroへのアトリビュート渡し

Posted at

Chef-Zeroにノードオブジェクトでアトリビュート値を渡す際に試行錯誤があったので備忘として残す

環境

AIX: 7300-00-01-2148
Chef-client: 14.15.6

サンプルChefレポジトリ

/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
│   └── node1.json  # ユーザー作成のノードオブジェクト
└── roles
    ├── README.md
    └── example.json

ノードオブジェクトとレシピを除き、chef generate repoにて作成した素のchef-repo。

サンプルレシピ

/chef-repo/cookbooks/example/recipes/default.rb
# frozen_string_literal: true

log "Welcome to Chef, #{node['foo']}!" 

chef generate repoにて作成したexample cookbookのrecipeを編集

サンプルノードオブジェクト

/chef-repo/nodes/node1.json
{
  "name": "node1",
  "normal": {
    "foo": "bar"
  },
  "run_list": ["recipe[example::default]"]
}
  • ディレクトリー
    • cookbooksと同列のnodesとする。
    • 他ディレクトリーの場合、ノードオブジェクトで設定したアトリビュートがnode.normal['foo']などとしてしか参照されなかった。
  • ファイル名
    • <ホスト名>.jsonとすると収束後にアップデートされるので分かり易いかと。
  • "name":
    • 必須ではないが、収束後にアップデートさせると"name": "ホスト名"で項目が作成される。
  • "normal":
    • ノードオブジェクトで設定できるアトリビュートはノーマルレベルであり、かつそれ以外のレベルは設定できない。他レベルで記載しても"normal":レベルとして使用される。
    • そもそも"normal":を使用しなくともノーマルレベルとして使用される。
  • "run_list":
    • 通常の指定と同様に、recipe[]や::defaultは省略可能
    • さらに、-oにてrun_listを指定するなら、ノードオブジェクトにて設定する必要はない。

サンプルノードオブジェクト(最小版)

/chef-repo/nodes/node1.json
{
  "foo": "bar"
}
  • "normal": { } も省略
  • "run_list": が無いので、chef-client実行時には-oにてrun_listを指定する必要がある。

テスト実行

$ cd /chef-repo
$ chef-client -z -j nodes/node1.json  # run_listが未設定の場合には -o example などと追加指定
...
Starting Chef Client, version 14.15.6
...
resolving cookbooks for run list: ["example::default"]
Synchronizing Cookbooks:
  - example (1.0.0)
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 1 resources
Recipe: example::default
  * log[Welcome to Chef, bar!] action write
  

Running handlers:
Running handlers complete
Chef Client finished, 1/1 resources updated in 07 seconds
  • コマンドを実行するディレクトリーを、cookbooksと同じか、cookbooks下とすると、クックブックは自動検出される。
  • -oなしでノードオブジェクト内のrun_listに従って実行する場合、/nodes/<ホスト名>.jsonに、"automatic":などを含め、結果が記載/アップデートされる。
    • ただ、アップデートを繰り返すと "normal": { "normal": { "normal": { ... と余分な部分が増えていくため、常に-oを用いてアップデートさせない方が良いのかもしれない。増えていくのが仕様なのか障害なのかは不明。
1
0
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
1
0