Ansible Tutorialで詰まった

なぜかAnsible チュートリアル | Ansible Tutorial in Japaneseでつまったのでメモ。

1. Vagrant を使ってサーバーを準備する

vagrant 1.5.3なので、 vagrant init chef/centos-6.5 で初期化。VagrantfileのBOX名もそれに合わせて修正。
なぜかsshのポートフォワード設定が効いていないっぽい。

2. Ansible をインストールする

pyenv + pipでインストールしようとしたら、 error: could not create '/usr/share/ansible': Permission denied が出てインストール失敗。
sudoだとコマンド見つからないし、面倒だったので、Installation Guide — Ansible Documentationのとおりにインストールしました。
バージョンは1.6.1。

3. Ansible の疎通確認

自分の環境だと、inventory fileにホストが記載されていない場合、"ERROR: Unable to find an inventory file, specify one with -i ?" ではなく "No hosts matched"が出力されました。

で、inventory file指定時はというと

% ansible 192.168.33.12 -i hosts -m ping           
192.168.33.12 | FAILED => SSH encountered an unknown error during the connection. We recommend you re-run the command using -vvvv, which will enable SSH debugging output to help diagnose the issue

で、エラー。

  • vvvvを付けてみたら、どうやら、~/.ssh/の下の秘密鍵を使おうとして"Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password)."になっているっぽい。

ユーザーと秘密鍵を両方指定したらちゃんと動きました。

 % ansible 192.168.33.12 -i hosts -m ping -u vagrant --private-key ~/.vagrant.d/insecure_private_key
192.168.33.12 | success >> {
    "changed": false, 
    "ping": "pong"
}

これだと面倒なのでhostsで指定。
Working with Inventory — Ansible Documentationを見た感じ、それぞれansible_ssh_userとansible_ssh_private_key_fileで定義できるようです。
今の所、相対パスでも動いてます。

[test-servers]
192.168.33.12 ansible_ssh_user=vagrant ansible_ssh_private_key_file=~/.vagrant.d/insecure_private_key

4. 簡単な Playbook を書いて試す

"be sure httpd is running and enabled"タスク実行時にエラー。

 % ansible-playbook -i hosts simple-playbook.yml --check       

PLAY [test-servers] *********************************************************** 

GATHERING FACTS *************************************************************** 
ok: [192.168.33.12]

TASK: [be sure httpd is installed] ******************************************** 
changed: [192.168.33.12]

TASK: [be sure httpd is running and enabled] ********************************** 
failed: [192.168.33.12] => {"failed": true}
msg: cannot find 'service' binary or init script for service,  possible typo in service name?, aborting

FATAL: all hosts have already failed -- aborting

PLAY RECAP ******************************************************************** 
           to retry, use: --limit @/home/kamataro/simple-playbook.retry

192.168.33.12              : ok=2    changed=1    unreachable=0    failed=1   

Apache入ってない(dry runなので)からエラーになって当然な気がする。
実際、普通に実行すると正常終了するし、httpdも起動してました。

7.3. ServerSpec を実行してみる

tutorialのとおりだとsshの設定が指定されていないのでエラーに。
Vagrant + Chef Solo + serverspec + Jenkins でサーバー構築を CI - naoyaのはてなダイアリーを参考に、~/serverspec/spec/spec_helper.rb を修正してSSH_CONFIGを読み込むようにする。