牌語備忘録 -pygo

あくまでもメモです。なるべくオフィシャルの情報を参照してください。

牌語備忘録 -pygo

Ansible のチュートリアル的なものを試してみる

(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 がインストールされている。

参考サイト

Vagrant と Ubuntu と Docker と docker-compose と Django と PostgresSQL と Ansible とを動かすメモ

(macosx10.9, Vagrant 1.7.2, Docer, docker-compose, ansible 1.9.1)

前置き

想定以上にハマったので途中経過メモ。
とりあえず Djnago 動かして webブラウザで表示できた。

あとで Aansible まわり書く。

(これやる前に Vagrant + coreos でやろうとしたら、vagrant reload で synced_folder 設定していると意味不明にパスワード要求されてハマり過ぎたので、 Vagrant + Ubuntu に方向転換した。いつか何とかしたい...。)

vagrant の環境を取りあえず作る

$ vagrant init ubuntu1404 https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box

Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu1404"
  config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box"
  config.vm.network :forwarded_port, guest: 8000, host: 8080
  config.vm.network :private_network, ip: "192.168.33.11"
  config.vm.synced_folder ".", "/home/vagrant/app", :mount_options => ["dmode=777", "fmode=777"]
end

その他のファイル

$ vagrant up
$ vagrant ssh

Docker と docker-compose

とりあえず vagrant ssh して入れる。
(あとで ansible で書く)

docker

$ wget -qO- https://get.docker.com/ | sh
$ docker --version
Docker version 1.6.2, build 7c8fca2

docker-compose

curl -L https://github.com/docker/compose/releases/download/1.2.0/docker-compose-`uname -s`-`uname -m` > ~/docker-compose
sudo mv ~/docker-compose /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

とりあえず実行

sudo しないと Couldn't connect to Docker daemon at http+unix://var/run/docker.sock - is it running? とか言われて動かない

$ sudo docker-compose run web django-admin.py startproject composeexample .
$ sudo docker-compose up

ローカルのwebブラウザで確認

vagrantfile に書いた private_network の ip 使う

f:id:CortYuming:20150504161737p:plain

ansible

あとで書く、というかあとで修正・追記する。
(ほんとは ansible 書くためのサンプルを作りたかっただけなのに手間取った...)

追記

2015-05-25

と思ったけど coreos に python 入ってないから ansible 動かないよね...

failed: [172.17.8.101] => {"failed": true, "parsed": false}
BECOME-SUCCESS-knsfqvzqtobfrrrgdloqgcfbnxjrrkzc
/bin/sh: /usr/bin/python: No such file or directory
OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011

2015-05-31

qiita に書いた - Ansible で Vagrant の CoreOS の Docker を操作する - Qiita

参考