0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

serverspec に環境変数を渡したい

Posted at

やりたいこと

serverspec に引数を受け渡して、項目を切り替えるということがしたかった。

環境変数を引き渡すことで同様のことを実施することにした。

実現方法

引数の引き渡し方

引き渡し方として二通りの方法がある。

spec ファイルの中で使う分には、どちらの手法でも変わりはないと思う。

  1. bash の環境変数として渡す
  2. rake の環境変数として渡す

bash の環境変数として渡す

static_ip='192.0.2.1' rake spec:<host_name>

rake の環境変数として渡す

rake/command_line_usage.rdoc at master · ruby/rake · GitHub

rake static_ip='192.0.2.1' spec:<host_name>

spec ファイル側の記述

static_ip 環境変数の有無によって、確認項目を切り替える。

context 'IPアドレス設定' do
  if ENV['static_ip']
	describe 'IP アドレスが指定のものになっていること' do
	  its(:ipv4_address) { should eq ENV['static_ip'] }
	end
  else
    describe 'インターフェースの IP アドレス設定が DHCP であること' do
      describe file('/etc/sysconfig/network-scripts/ifcfg-eth0') do
        its(:content) { should match /^BOOTPROTO=dhcp$/ }
      end
    end
  end
end
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?