LoginSignup
6
4

More than 5 years have passed since last update.

itamaeメモ

Posted at

itamaeとは

chefの軽量版のような物です。そこまで覚えることも多くないので、導入コストはchefに比べ低いです。

初期設定

はじめにローカルでitamaeをインストールします。

~ gem install itamae

基本的な書き方

リソース

execute

シェルコマンドを実行。

execute "delete images folder" do
  command "rm -rf images"
end

remote_file

ファイルを転送する。

# 転送先のパスを指定
remote_file "/etc/nginx/conf.d/app.conf" do
  owner "root"
  group "root"
  # レシピファイルからの相対パス
  source "templates/etc/nginx/conf.d/app.conf"
end

template

ファイルを転送する。remote_fileとの違いは、templateの場合、variables attributeを使用して、テンプレートに変数を引き渡すことができる点。

# 転送先のパスを指定
template "/etc/nginx/conf.d/app.conf" do
  owner "root"
  group "root"
  # レシピファイルからの相対パス
  source "templates/etc/nginx/conf.d/app.conf.erb"
  variables(app_name: "hello_world_application")
end

package

パッケージをインストールする。Redhat系ならyum ~, Debian系ならapt ~という感じでインストールが行われる。

package "git" do
 action :install
end

notifies

リソースが更新された場合に、他のリソースのアクションを実行する

service "nginx"

template "/etc/nginx/conf.d/site.conf" do
 source "template/etc/nginx/conf.d/site.conf"
 notifies :reload, "service[nginx]"
end

include_recipe

レシピから他のレシピを呼び出す。

include_recipe "nginx.rb"

directory

ディレクトリを作成する

directory "/var/www/app" do
  mode "775"
  owner "owner"
  group "group"
end

user

ユーザーの作成を行う。ここでパスワードを設定する際に気をつけなければならないのが、レシピに記載するパスワードはSHA512で暗号化したものでなければいけない、という点です。
以下の一番始めの項目を参照してください。
itamaeでConoHaのVPSの初期設定を自動化する

user "create user" do
  uid "213123213"
  username "hello"
  password "world"
end

service

サービスの起動、終了等、サービスの操作を行う

service "nginx" do
  action :stop
end

実行方法

リモートホストに対して実行する

~ itamae ssh -h xxx.xxx.xx.xx -p 22 username recipe.rb

Vagrantに対して実行する

vagrant-hostsupdaterを入れておくと便利かも。

~ itamae ssh --vagrant -h vm_name recipe.rb

ディレクトリ構成

公式のBest Practiceに沿う形が良いかと思うので、ディレクトリ構造は以下のようにします(以下公式wikiより引用)。

.
├── cookbooks
│   └── nginx
│       ├── default.rb
│       ├── files
│       │   └── etc
│       │       └── nginx
│       │           └── conf.d
│       │               └── static.conf
│       └── templates
│           └── etc
│               └── nginx
│                   └── conf.d
│                       └── dynamic.conf
└── roles
    └── web.rb

参考

6
4
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
6
4