AnsibleでGentooの環境を作れるのかなあと思ったら、モジュールが提供されていました。
Apacheをインストールしてデフォルトランレベルに登録するところまでやってみました。
Apacheのインストール
Ansibleの設定ファイルを書きます。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| ---
- hosts: tag_Name_gside_gentoo
remote_user: gentoo
become: true
tasks:
- name: install apache
portage: >
package=apache
state=present
- name: add apache default runlevel
service: >
name=apache2
state=started
enabled=yes
|
ここでちょっとハマったのが、Ansibleを実行するホストだけでなくて、対象のホストのpythonのバージョンにも注意しないといけないことでした。
対象ホストがpython3系がデフォルトだったため、AttributeError: ‘dict’ object has no attribute ‘iteritems’ みたいなエラーが出てたので、pythonのデフォルトバージョンを2系にセットします。
1
2
3
4
5
| $ eselect python list
Available Python interpreters:
[1] python2.7
[2] python3.4 *
$ eselect python set 1
|
作成した設定ファイルを実行します。
1
| $ ansible-playbook gside.yaml -i ./ec2.py
|
Apacheの起動をテストする
Apacheをインストール・起動・デフォルトランレベルへの登録までできましたので、先日作ったServerSpecのテストを流してみます。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| $ rake spec
(in /home/hoge)
/usr/bin/ruby21 -I/usr/local/lib64/ruby/gems/2.1.0/gems/rspec-support-3.5.0/lib:/usr/local/lib64/ruby/gems/2.1.0/gems/rspec-core-3.5.4/lib /usr/local/lib64/ruby/gems/2.1.0/gems/rspec-core-3.5.4/exe/rspec --pattern spec/gside.org/\*_spec.rb
/usr/local/lib64/ruby/gems/2.1.0/gems/rspec-core-3.5.4/lib/rspec/core/rake_task.rb:79: 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.09901 seconds (files took 1.02 seconds to load)
3 examples, 0 failures
|
まとめ
インスタンスの起動もTerraformでやったので、起動・セットアップ・テストまでコンソールからスムーズにできました。
TDDみたいな感覚でWebサーバーのセットアップができるのは非常い気持ちがいいですね。