(macosx10.9, ansible 1.9.1, Vagrant 1.7.2)
seizans さんの 『Ansible コトハジメ』 がVagrant などのバージョン差異によりそのままでは動かないようなので、若干修正を加えて実行できるようにしてみる。
Vagrant, Ansible はインストール済みの前提
vagrant box を用意する
何となくこれ使ってみる
$ mkdir ansible-sample $ cd ansible-sample $ vagrant init ubuntu1410 https://oss-binaries.phusionpassenger.com/vagrant/boxes/latest/ubuntu-14.04-amd64-vbox.box
Vagrantfile
config.vm.box のあたりを変更する
config.vm.box = "ubuntu1410"
config.vm.network :private_network, ip: "192.168.111.222"
config.vm.provision :ansible do |ansible|
ansible.playbook = "provision_vagrant.yml"
ansible.inventory_path = "hosts"
end
inventory file
[sample] 192.168.111.222 ansible_ssh_private_key_file=.vagrant/machines/default/virtualbox/private_key
playbook
- hosts: sample
sudo: true
user: vagrant
tasks:
- apt: pkg=git update_cache=yes state=latest
vagrant up
$ vagrant up
ERROR: Specified --limit does not match any hosts
エラー出た
ERROR: Specified --limit does not match any hosts Ansible failed to complete successfully. Any error output should be visible above. Please fix these errors and try again.
Vagrantfile に ansible.limit を追記
ansible.inventory_path = "hosts"
+ ansible.limit = "sample"
end
$ vagrant destroy && vagrant up
エラー出なくなった
playbook の処理を確認する
$ vagrant ssh vagrant@vagrant-ubuntu-utopic-64:~$ git --version git version 2.1.0
正常にインストールされている
『role を作る』をやる
一部若干変更。
- command: mkdir {{ virtualenvs_dir }} creates={{ virtualenvs_dir }}
- pip: name=Django virtualenv={{ virtualenv_dir }} state=latest
- hosts: sample
sudo: true
user: vagrant
tasks:
- apt: pkg={{ item }} update_cache=yes state=latest
with_items:
- git
- python-pip
- python-virtualenv
- hosts: sample
user: vagrant
roles:
- python
vars:
work_dir: /home/vagrant
virtualenvs_dir: "{{ work_dir }}/.virtualenvs"
virtualenv_dir: "{{ virtualenvs_dir }}/env01"
実行
$ ansible-playbook -i hosts provision_vagrant.yml
ここらへんでエラー出た
fatal: [192.168.111.222] => SSH Error: Host key verification failed.
while connecting to 192.168.111.222:22
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.
TASK: [apt pkg={{ item }} update_cache=yes state=latest] **********************
FATAL: no hosts matched or all hosts have already failed -- aborting
設定ファイルにを追記
カレtントディレクトリに ansible.cfg を作成
[defaults] host_key_checking=False
エラーでなくなった
確認
$ vagrant ssh : vagrant@vagrant-ubuntu-utopic-64:~$ source .virtualenvs/env01/bin/activate (env01)vagrant@vagrant-ubuntu-utopic-64:~$ pip freeze Django==1.8.2 argparse==1.2.1 wsgiref==0.1.2
Django がインストールされている。
