ここでのプロビジョン対象は vagrant で起動した almalinux/9
- ip address 10.0.2.15 / 192.168.56.11
- ssh 10.0.2.15:2200 / 192.168.56.11:22
- user vagrant
ディレクトリ構成
(作業ディレクトリ)/
clients/
cookbooks/
nodes/
site-cookbooks/
Berksfile
config.rb
config.rb
local_mode true
chef_repo_path File.expand_path('../' , __FILE__)
cookbook_path [
File.expand_path('../cookbooks' , __FILE__),
File.expand_path('../site-cookbooks' , __FILE__)
]
knife[:ssh_attribute] = "almaserver"
knife[:use_sudo] = true
knife[:listen] = true
knife[:automatic_attribute_whitelist] = %w[
fqdn
os
os_version
hostname
ipaddress
roles
recipes
ipaddress
platform
platform_version
cloud
cloud_v2
chef_packages
]
~/.ssh/config
準備
knife zero bootstrap almaserver --node-name almaserver --ssh-user vagrant --sudo -z
knife node run_list add almaserver rails
レシピ
ruby インストールのためのレシピを作る
mkdir site-cookbooks
cd site-cookbooks
chef generate cookbook ruby
site-cookbooks/ruby/recipe/default.rb にレシピを記述する
global_version = '2.7.6'
#
# Cookbook:: rails
# Recipe:: default
#
# Copyright:: 2022, The Authors, All Rights Reserved.
%w[
git
gcc
gcc-c++
openssl-devel
readline-devel
patch
].each do |pkg|
package "#{pkg}" do
action :install
end
end
directory "/usr/local/rbenv" do
recursive true
action :delete
end
execute "install-rbenv" do
command "git clone https://github.com/sstephenson/rbenv.git /usr/local/rbenv"
end
execute "install-ruby-build" do
command "git clone https://github.com/sstephenson/ruby-build.git /usr/local/rbenv/plugins/ruby-build"
end
cookbook_file '/etc/sudoers.d/rbenv' do
source 'rbenv'
owner 'root'
group 'root'
mode '0644'
action :create
end
cookbook_file '/etc/profile.d/rbenv.sh' do
source 'rbenv.sh'
owner 'root'
group 'root'
mode '0644'
action :create
end
execute "install-ruby" do
command "rbenv init -;rbenv install 2.7.6;rbenv rehash;rbenv global 2.7.6"
end
プロビジョン実行
nodes/almaserver.json が以下のように作成されているので プロビジョン実行時にポートを指定する
"automatic": {
"fqdn": "localhost",
"os": "linux",
"os_version": "5.14.0-70.26.1.el9_0.x86_64",
"hostname": "localhost",
"ipaddress": "10.0.2.15",
....
これは private address を追加して 192.168.56.11 と デフォルトで設定される 10.0.2.15 が混在しているから?
普通のリモートホストだと指定の ip address がそのまま使えるはず(ルーター機能とか使っている場合はデバイスが複数作られるはずなので注意)
実行
knife zero converge "name:almaserver" --ssh-user vagrant --sudo -z -p 2200
その他
ansible は残念ながら windows で実行できない(windows の構成は可能っぽい)ので他の自動プロビジョニングツールを使う事になるけどここでは chef workstation(旧chefdk) によってインストールされる chef, knife を使う