以前試したServerspecですが、Ansibleと連携するプロダクトがあるということで試してみました。
その名もansible_specです。
Ansibleで構成したサーバーの接続情報を共有したいというモチベーションはTestinfraと同様です。
やはり同じ接続設定をいろんなところに設定するのは避けたいところですよね。
インストール
gemでサクッとインストールします。特につまづくところはありませんでした。
1
| $ sudo gem install ansible_spec
|
設定
ansibleの設定がおいてあるディレクトリで下記を実行。
1
2
3
4
5
6
| $ ansiblespec-init
create spec
create spec/spec_helper.rb
create Rakefile
create .ansiblespec
create .rspec
|
.ansiblespec の内容は下記の通りです。
dynamic inventoryもサーポートしていて、inventoruyにはansibleのiオプションで指定するdynamic inventoryを実現するpythonスクリプトを指定します。
1
2
3
4
| ---
-
playbook: site.yml
inventory: ec2.py
|
site.ymlは下記の通りです。
hostsにはEC2のtagを指定しています。KeyはNameです。
roleにはテストのある位置を指定しています。
1
2
3
4
| - name: gside-TDD
hosts: tag_Name_gside_gentoo
roles:
- apache
|
テストはAnsibleでいうroleと同じディレクトリに配置します。
今回は roles/apache/spec/apache_spec.rb にファイルをおきました。内容は以前ServerSpecを試した時のものと同じです。。
ファイル名は *_spec.rb の形式でないといけないようです。
ディレクトリ構成に関しては確かに多くの構成はroleベースで書くことが多いので、roleを前提としている構成は実用には困らないかもしれません。
1
2
3
4
5
6
7
8
9
10
| require 'spec_helper'
describe service('apache2'), :if => os[:family] == 'gentoo' do
it { should be_enabled }
it { should be_running }
end
describe port(80) do
it { should be_listening }
end
|
実行
rake allで実行してもいいのですが、rake -T でテスト対象を一覧表示した上で個別に実行できます。
1
2
3
4
| $ rake -T
/usr/lib64/ruby/2.2.0/open3.rb:193: warning: Insecure world writable dir /usr/local/bin in PATH, mode 040757
rake all # Run serverspec to all test
rake serverspec:gside-TDD # Run serverspec for gside-TDD
|
これでテスト実行できると思ったのですが、このままだとうまく実行できませんでした。
実行結果の一部抜粋です。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| $ rake serverspec:gside-TDD
/usr/lib64/ruby/2.2.0/open3.rb:193: warning: Insecure world writable dir /usr/local/bin in PATH, mode 040757
Run serverspec for gside-TDD to {"uri"=>"52.198.30.198", "port"=>22}
/usr/bin/ruby22 -I/usr/local/lib64/ruby/gems/2.2.0/gems/rspec-support-3.5.0/lib:/usr/local/lib64/ruby/gems/2.2.0/gems/rspec-core-3.5.4/lib /usr/local/lib64/ruby/gems/2.2.0/gems/rspec-core-3.5.4/exe/rspec --pattern \{roles\}/\{apache\}/spec/\*_spec.rb
/usr/lib64/ruby/2.2.0/open3.rb:193: warning: Insecure world writable dir /usr/local/bin in PATH, mode 040757
No backend type is specified. Fall back to :exec type.
No backend type is specified. Fall back to :exec type.
No backend type is specified. Fall back to :exec type.
....
should be listening (FAILED - 3)
Failures:
1) Service "apache2" should be enabled
On host `52.198.30.198'
Failure/Error: it { should be_enabled }
expected Service "apache2" to be enabled
/bin/sh -c rc-update\ show\ \|\ grep\ --\ \\\\s\\\*apache2\\\\s\\\*\\\\\\\|\\\\s\\\*\\\(boot\\\|default\\\)
# ./roles/apache/spec/apache_spec.rb:5:in `block (2 levels) in <top (required)>'
...
|
自動生成される spec/spec_helper.rbspec を下記のように修正したら動きました。
1
2
| - connection = ENV['TARGET_CONNECTION']
+ connection = 'ssh'
|
これで実行すると正しく動きました。
なんか設定が漏れているのかもしれませんが、一旦良しとしています。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| $ rake serverspec:gside-TDD
/usr/lib64/ruby/2.2.0/open3.rb:193: warning: Insecure world writable dir /usr/local/bin in PATH, mode 040757
Run serverspec for gside-TDD to {"uri"=>"52.198.30.198", "port"=>22}
/usr/bin/ruby22 -I/usr/local/lib64/ruby/gems/2.2.0/gems/rspec-support-3.5.0/lib:/usr/local/lib64/ruby/gems/2.2.0/gems/rspec-core-3.5.4/lib /usr/local/lib64/ruby/gems/2.2.0/gems/rspec-core-3.5.4/exe/rspec --pattern \{roles\}/\{apache\}/spec/\*_spec.rb
/usr/lib64/ruby/2.2.0/open3.rb:193: warning: Insecure world writable dir /usr/local/bin in PATH, mode 040757
Service "apache2"
should be enabled
should be running
Port "80"
should be listening
Finished in 0.09668 seconds (files took 1.2 seconds to load)
3 examples, 0 failures
|
まとめ
インフラの構成管理とテストを色々試してきました。
Terraform, Ansibleを構成管理ツールとして、Testinfra又は Serverspec + ansible_spec で低レベルのテスト、infratasterで高レベルのテストを実施することができました。
それぞれのツールをもう少し深掘りしたいところですが、これでモダンなインフラへの入り口に立てたかなと思います。