バージョン†serverspec (2.25.0) パッケージ†単一†describe package('httpd') it { should be_installed } end 複数†%w{epel-release rsync gcc vim-enhanced}.each do |pkg| describe package(pkg) do it { should be_installed } end end パッケージのバージョン確認†[パターン1] describe command( 'rpm -q openssl') do it (:stdout) { should match(/openssl-0.9.8e-39/) } end [パターン2] describe package('openssl') do it { shuld be_installed.by('rpm').with_version('0.9.8e-39') } end サービス†chkconfigが有効になっているかとサービスが起動しているかの書き方†describe service('httpd') it { should be_enabled } it { should be_running } end chkconfigが無効だった場合のエラー内容†should be enabled (FAILED - 1) Failures: 1) Service "httpd" should be enabled On host `192.168.33.11' Failure/Error: it { should be_enabled } expected Service "httpd" to be enabled sudo -p 'Password: ' /bin/sh -c chkconfig\ --list\ httpd\ \|\ grep\ 3:on サービスが稼働していなかった場合のエラー内容†Service "httpd" should be running (FAILED - 2) Failures: 2) Service "httpd" should be running On host `192.168.33.11' Failure/Error: it { should be_running } expected Service "httpd" to be running sudo -p 'Password: ' /bin/sh -c service\ httpd\ status httpd ???????????????????????? ポート†ポートが開いているかの書き方†describe port(22) do it { should be_listening } end ポートが開いていなかった場合のエラー内容†Port "80" should be listening (FAILED - 2) Failures: 2) Port "80" should be listening On host `192.168.33.11' Failure/Error: it { should be_listening } expected Port "80" to be listening sudo -p 'Password: ' /bin/sh -c netstat\ -tunl\ \|\ grep\ --\ :80\\\ コマンドを実行してその標準出力の結果をチェック†describe command('cat /etc/sysconfig/clock') do its(:stdout) { should match /ZONE="Asia\/Tokyo"/ } end [注意] バージョン2からは「it { should return_stdout 〜」という記述が使えなくなったようです。 http://serverspec.org/changes-of-v2.html ファイル†単一ファイルの存在確認†describe file("/etc/hosts") do it { should be_file } end 複数ファイルの存在確認†[ 'hosts, 'resolv.conf' ].each do | conf | describe file( "/etc/#{conf}" ) do it { should be_file } end end |