LoginSignup
1
1

More than 5 years have passed since last update.

serverspec-runnerの基本的な使い方

Last updated at Posted at 2018-09-26

概要

serverspec-runnerというgemの使い方です。
serverspecを拡張されたツールです。

関連記事

環境構築

dockerで環境を構築します。

From centos:7

ARG RUBY_VER_PREFIX=2.5
ARG RUBY_VER=2.5.1

RUN yum install -y make \
                   gcc-c++ \
                   glibc-headers \
                   openssl-devel \
                   readline \
                   libyaml-devel \
                   readline-devel \
                   zlib \
                   zlib-devel \
                   bzip2 \
                   libxslt-devel

WORKDIR /usr/local/src
RUN curl -O https://cache.ruby-lang.org/pub/ruby/${RUBY_VER_PREFIX}/ruby-${RUBY_VER}.tar.gz
RUN tar zxfv ruby-${RUBY_VER}.tar.gz

WORKDIR /usr/local/src/ruby-${RUBY_VER}
RUN ./configure
RUN make
RUN make install
RUN gem install bundle

インストール

$ bundle init
Gemfile
gem 'serverspec-runner'
gem 'highline'
$ bundle install --path vendor/bundle

nokogiriがどうのこうのとエラーが出る場合は、以下を実行してから、bundle installを実行してください。

$ bundle config --local build.nokogiri --use-system-libraries

参考:bundle installでエラーが出る場合

serverspec-runner初期設定

以下のコマンドで初期設定を行います。

$ bundle exec serverspec-runner -r ./
want to create spec-tree to /code? (y/n): y
Please edit "/code/scenario.yml" and change directory to "/code" and exec "serverspec-runner" command !!

以下のようなディレクトリ構成になります。

Gemfile
Gemfile.lock
lib
scenario.yml
spec
ssh_options_default.yml
vendor

scenario.ymlを作成

テスト対象のホストと、テストケースを定義します。

書式: https://github.com/hiracy/serverspec-runner#usage

test_top_dir:                # test directory top
    :                        # test hierarchy directories
  test_bottom_dir:           # test directory bottom
    - servername             # ssh-accessible ip address or fqdn. or alias
    - :
  - :
:
---
servername:                  # alias name(not required)
  host: 192.168.0.11         # ssh-accessible ip address or fqdn(required if alias exist)
  ssh_opts:                  # ssh options(not required)
    port: 22                 # ssh port option(not required)
    user: "anyone"           # ssh user option(not required)
    keys: ["~/.ssh/id_rsa"]  # ssh private keys option(not required)
      :                      # any other Net::SSH Options(not required)
  any_attribute: "aaa"       # host attributes. this example available to get "property[:any_attribute]" from your codes(not required)
  :
:

e.g.)

spec:
  - server01
  - server02
---
server01:
  host: hostname01
  ssh_opts:
    user: 'myname'
server02:
  host: hostname02
  ssh_opts:
    user: 'myname'

こちらのファイルで、sshの共通設定をすることもできます。

ssh_options_default.yml
:port: 22
:paranoid: false
#:user: "youre_name"
#:keys: ["/path/to/private_key"]
#:passphrase: "yourpassphrase"

テストケース作成

テストの作成方法は、serverspecと同じです。
spec/ディレクトリの下に作成してください。

参考:Serverspec書き方

テスト実行

-t mkdはマークダウンで結果を出力するオプションです。

$ cd /home/vagrant/appname/serverspec-runner
$ bundle exec serverspec-runner -t mkd

sudoが必要な場合は、以下の様にASK_SUDO_PASSWORD=1を付けるか、環境変数にSUDO_PASSWORDをセットします。

$ ASK_SUDO_PASSWORD=1 bundle exec serverspec-runner -t mkd

scenario.ymlを別の場所、名前で保存した場合

$ bundle exec serverspec-runner -r ./ -s scenario/production.yml -t mkd

-r トップディレクトリの指定
-s シナリオファイルのパスの指定

参考

1
1
3

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
1
1