LoginSignup
29
30

More than 5 years have passed since last update.

今日作りたくなる簡単cookbook [Kim'sキッチン] yumsetup(yum-fastestmirrorにyum update添え)

Posted at

以下全ても●みち風に書いたメモ

皆さんおはようございます。
ryurock@githubです。


私は毎回サーバーを立ち上げた後に行うyum-fastestmirrorと
時間がかかるyum -y updateが大好きです。
そこでyum-fastestmirrorとyum -y update の簡単なcookbookを教えてください。

今日はそんなyumが大好きな方に簡単なcookbookをknife-soloでご紹介したいと思います。

レシピ作成時間

約3分

環境

  • mac-osx(ホストOS)
  • centos6.4-x86-minimal (ゲストOS)

事前準備に必要なもの

参考にしたサイト(書籍)

前準備 - chef solo init - - knife solo prepare -

この2つは最初の一回のみ。すでに実行済みの場合は「その1」の項目へ

リポジトリを作成する

knife solo init chef-repo

概念的にはgit initと一緒です。
「ここにchefする場所を作るよ!!」と宣言します。

リモートでの chef-solo 実行環境を整える

要はsshの設定です。
vagrantで設定している場合はホストOS(mac)で下記のように

cd VagrantFileがおいてあるディレクトリ
vagrant ssh-config --host {接続名称(任意)} >> ~/.ssh/config

vagrantのssh設定を登録する

cd chef-repo
knife solo prepare {接続名称(任意)}

[その1] レシピ名を決める

今回のレシピ名はyumsetupで行きましょう

cd chef-repo
knife cookbook create yumsetup -o site-cookbooks

[その2] chef-repo/yumsetup/recipes/default.rbを編集

chef-repo/yumsetup/recipes/default.rb
yum_package "yum-fastestmirror" do
  action :install
end

execute "yum-update" do
  user "root"
  command "yum -y update"
  action :run
end

ruby知らなくてもわかる内容ですね。

Chef Resourcesを使用した

上記2つで実行しています。

[その3]yumsetup cookbookをリモート側で実行する設定を追加

vim chef-repo/nodes/{接続名称(任意)}.json

{"run_list":[
    "yumsetup"
]}

これだけ

[その4] リモート側に実行依頼をする

cd chef-repo
knife solo cook {接続名称(任意)}


Starting Chef Client, version 11.4.0
Compiling Cookbooks...
Converging 2 resources
Recipe: yumsetup::default
  * yum_package[yum-fastestmirror] action install (up to date)
  * execute[yum-update] action run
    - execute yum -y update

Chef Client finished, 1 resources updated

2つ命令を書いているが1つしか実行されないのはyum-fastestmirrorがすでにインストール済みであるからです。(chefは冪等性を保証さするものって入門ChefSolo-InfrastructureasCode-1.0.0に書いてありました)

今日はこれで決まり「yum-fastestmirrorにyum update添え」の完成です

たった4ステップ、書いた行数は15行未満で出来上がりです。
chef-soloはそんなに敷居が高く無いので是非ご参考に

29
30
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
29
30