Share
Embed
Serverspecを使用したサーバ設定テストの実例 / かごもく No8
Koichi Shimozono
May 25, 2018
Transcript
11 slides
-
Serverspecとは サーバの実際の状態を「ローカルコマンド実 行」、「SSH」、「WinRM」、「Docker API」等を通し てテストする ruby で書かれたツール 宮下剛輔氏によって開発されている
https://serverspec.org/ 特徴 ◦ テスト対象のサーバにはエージェント等を導入する 必要はない ◦ 構成管理ツール(Puppet, Ansible, CFEngine, Itamae(Chef)等と共に使用可能 ◦ ほぼ ruby の Rspec 真の目的は「インフラストラクチャコードのリ ファクタリングを助けること」 2018/05/26 かごもく #8 でばっぐ 2 -
利用方法 2018/05/26 かごもく #8 でばっぐ 4 gem のインストール $ gem
install serverspec 初期設定(雛形作成): $ serverspec-init Select OS type: 1) UN*X 2) Windows Select number: 1 Select a backend type: 1) SSH 2) Exec (local) Select number: 1 Vagrant instance y/n: n Input target host name: webhost.cc.kagoshima-u.ac.jp + spec/ + spec/webhost.cc.kagoshima-u.ac.jp/ + spec/webhost.cc.kagoshima-u.ac.jp/sample_spec.rb + spec/spec_helper.rb + Rakefile + .rspec $ severspecを導入するマシン テスト対象ホスト webhost.cc.kagoshima-u.ac.jp ssh ホスト名に対応したディレ クトリが作成される サンプルspecファイル -
利用方法 2018/05/26 かごもく #8 でばっぐ 5 実行 $ rake spec:webhost.cc.kagoshima-u.ac.jp
結果 $ rake spec:webhost.cc.kagoshima-u.ac.jp ASK_SUDO_PASSWORD=1 Enter sudo password: Package "httpd" should be installed Service "httpd" should be enabled should be running Port "80" should be listening Finished in 1.48 seconds (files took 6.75 seconds to load) 4 examples, 0 failures $ -
標準のディレクトリ構成と実行について ./ spec host1 http_spec.rb users_spec.rb … _spec.rb host2 hoge1_spec.rb
hoge2_spec.rb … _spec.rb … spec_helper.rb Rakefile .rspec 2018/05/26 かごもく #8 でばっぐ 6 spec ディレクトリ配下にディレクトリを作成すると、それは「ホスト名」で あると認識する ◦ 複数のホストに対するテストを管理できる ホスト名ディレクトリ配下の「*_spec.rb」をspecファイルとして認識し、全 てをテストする これらの制御は Rakefile で行っている ◦ Rakefile をいじれば、いろいろできる ssh のログイン制御等「テスト対象ホストへのアクセス方法」制御は、 spec_helper.rb で行っている -
specファイルについて(1) リソースタイプ(resource type) マッチャー(matcher) 2018/05/26 かごもく #8 でばっぐ
7 sample_spec.rb (抜粋) require 'spec_helper' describe package('httpd') do it { should be_installed } end describe service('httpd') do it { should be_enabled } it { should be_running } end describe port(80) do it { should be_listening } end パッケージ httpd は インストールされているか? サービス httpd は (起動時)有効になって いるか? (現在)起動しているか? -
specファイルについて(2) リソースタイプごとに一部マッチャーは決まっている ◦ もちろん rspec 由来のマッチャーも利用可能 どんなリソースタイプがあるのか? https://serverspec.org/resource_types.html
2018/05/26 かごもく #8 でばっぐ 8 describe user('simozono') do it { is_expected.to exist } it { is_expected.to belong_to_group 'wheel' } it { is_expected.to have_uid 1101 } it { is_expected.to have_home_directory "/home/simozono" } it { is_expected.to have_login_shell '/bin/bash' } end describe interface('ens160') do it { is_expected.to exist } it { is_expected.to be_up } it { is_expected.to have_ipv4_address "192.168.1.241/24" } end describe file '/etc/ntp.conf' do time_cc_regex = /^server[ ¥t]+timeserver.kagoshima-u.ac.jp/ sinet_ntp_regex = /^server[ ¥t]+ntp[01]1.sinet.ad.jp/ its(:content) { is_expected.to match time_cc_regex } its(:content) { is_expected.to match sinet_ntp_regex } end -
所属組織での利用方法 業務で必要なサーバは仮想化基盤 (Vmware)上に手動で作成 ◦ サーバによって設定がまちまち logrotate が入っていたり入っていなかったり
ログの保存期間が4週だったり60週だったり 9つのサーバに対して serverspec を書いた ◦ 共通のテスト項目(ntpが動いているかとか)は、できるだけまとめた rspec の shared_examples 機能 ホスト固有の情報(IPアドレス等)は、specファイルには書かずに、yaml ファイル書く How to use host specific properties https://serverspec.org/advanced_tips.html よくなった点 ◦ 設定等が統一された ◦ 新規サーバ構築時に設定が間違っていないかテストできる ◦ 設定変更者と設定変更確認者を分離し業務フローを作成 問題点 ◦ OS標準からの設定変更点を spec ファイルに全部書き出したか? ◦ 標準でマッチャーがないテスト項目は、設定ファイルの変更点を 正規表現 に頼っているため、正規表現の記載が面倒 例: /etc/httpd/conf.d/vhost.conf の <VirtualHost *:80>の中で、ちゃんと ServerName は指定のホスト名となっているか? CustomLog は 指定したファイル名に なっているか? <Directory> は指定したディレクトリになっているか? 2018/05/26 かごもく #8 でばっぐ 9 -
InSpecについて InSpecとは ◦ https://inspec.io/ Chef Software が開発
特徴 ◦ InSpec is inspired by the wonderful Serverspec project. ◦ InSpec は、コンプライアンス担保を目的としている。 Migrate from Serverspec to InSpec ◦ https://www.inspec.io/docs/reference/migration/ 2018年現在、活発に開発が続いている 詳細は…使ってないのでよくわかりません 2018/05/26 かごもく #8 でばっぐ 10 -
Serverspecに関する参考&感想 本家 ◦ https://serverspec.org/ 書籍 ◦ O’Reilly より「Serverspec」
qiita 等でいっぱい出てきます 少し難しいことをしようと思うと ruby & rspec の知識が必要となります 本気でやると resource type が不足 ◦ firewalld の設定 https://github.com/decors/serverspec-firewalld- resources を利用しました。 2018/05/26 かごもく #8 でばっぐ 11