LoginSignup
6
4

More than 5 years have passed since last update.

itamae具体例10選〜寒空のlinuxコマンドを添えて〜

Posted at

目的

訳あって、既存のitamaeを読みながらitamaeを使わずに環境構築をしていました。
検索で「itamaeコード itamae」とか「linuxコマンド itamae」とかよく打っていたのですが、記事は「実行できることとitamaeコードまとめ」のようなものが多く、すぐにはlinuxコマンド=itamaeコードがパッと結びつけられませんでした。なので、自分の作業に沿ってになりますが、セットで書いていきたいと思います。

もし「itamaeってなんだ!」の場合はこちら

iptablesとSELinuxを停止&無効化

ファイアーウォールを無効化、セキュリティ管理してくれるモジュールを無効化します。

>itamaeでのこれは・・・

service 'iptables' do
  action [:disable, :stop]
end

include_recipe 'selinux::disabled'

>こう!

$ /etc/rc.d/init.d/iptables stop # 一時停止:再起動時に再び有効化される
$ chkconfig iptables off         # 無効化:再起動時しても無効のままになる
$ setenforce 0                   # selinuxを無効化

引用

外部リポジトリを追加。

epelリポジトリとかremiリポジトリを追加します。

>itamaeのこれは・・・

package 'http://ftp.iij.ad.jp/pub/linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm' do
  not_if 'rpm -q epel-release'
end

package 'http://rpms.famillecollet.com/enterprise/remi-release-6.rpm' do
  not_if 'rpm -q remi-release'
end

>こう!

rpm -q epel-release
rpm -q remi-release

ちなみに、yumを自動探索の設定はremote_fileのようなものでファイルを用意しておいて変更を加えます。owner・groupを設定することでroot権限で実行できます。「それさえもコマンドだ!」という方はこちら

remote_file "/etc/yum.repos.d/remi.repo" do
  owner "root"
  group "root"
  source "./templates/etc/yum.repos.d/remi.repo"
end

gitをインストール

>セイ!

package "git" do
  action :install
end

>ヤァッ!

yum -y install git

redisをインストール

>これは・・・

package "redis" do
  action :install
end

>こう!

rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum --enablerepo=epel install redis

参照

ただ、さきほどあった「外部リポジトリを追加。」をしておけば、rpm -ivh〜のほうはしなくても、yum --enablerepo=epel install redisだけでいけます。

変数を使った条件分岐

itamae実行時にファイルを読み込むと変数を扱えます。
こちらで説明されていますが、環境ごとにファイルを用意してitamaeコマンド時指定してあげることで、環境ごとの設定にすることができます。

nodes/development.jsonを用意して以下のように書いてあげて

{
    "env": "development"
}

itamaeで以下のように書いてあげると、itamaeコマンドでnodes/development.jsonを指定した時だけredisが起動されます。

# 本番環境は外部のRedisサーバを使用するため起動しない
if node[:env] == "development" then
  service 'redis' do
    action [:enable, :start]
  end
end

>コマンドではこう!

/etc/init.d/redis start
chkconfig redis on

root権限で、ファイルのコピーと権限変更

root権限でファイルを変更し、権限を変更する時。

>これは・・・

template "/etc/nginx/nginx.conf" do
  owner "root"
  group "root"
  mode "644"
  source "./templates/etc/nginx/nginx.conf.erb"
end

>こう!

# ソース内容 ./templates/etc/nginx/nginx.conf.erb
# 指定先 /etc/nginx/nginx.conf
〜コピペでもなんでも。〜 # 指定先にソース内容を反映。
sudo chmod 644 /etc/nginx/nginx.conf

新しいディクトリを作成して、コピー、

directory "/etc/nginx/ssl"
remote_directory "./files/etc/nginx/ssl" do
  action :create
  path "/etc/nginx/ssl"
  source "./files/etc/nginx/ssl"
  mode "600"
  owner "root"
  group "root"
end
〜mkdirしてなんやらして〜 #/etc/nginx/sslをディレクトリ作成
〜コピペして〜 #./files/etc/nginx/sslの内容を/etc/nginx/sslへコピー
chmod 600 /etc/nginx/ssl/

ファイル削除(コマンド実行)

executeを使えば、コマンドを実行できます。

>例えばファイルを削除のこれは、

execute "remove default conf files" do
  user "root"
  command "rm -rf /etc/nginx/conf.d/*.conf"
end

>こう!

sudo rm -rf /etc/nginx/conf.d/*.conf

インストール(条件付きでコマンド実行)

not_ifを使えば、npm ls -g n | grep n@の出力がなければ、

execute "install n" do
  user "root"
  command "npm install -g n"
  not_if "npm ls -g n | grep n@"
end
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