LoginSignup
32
32

More than 5 years have passed since last update.

VagrantでAnsibleのPlaybookを作るためのローカル環境を作る

Posted at

環境

  • Mac OS X 10.9 (ホストOS)
  • Ansible 1.4.1
  • VirtualBox 4.3.4
  • Vagrant 1.4.0
  • Ubuntu 12.04 LTS (Precise) 64bit (ゲストOS)

手順

Ansibleをインストールする

ホストOS上にインストールします。 pip は、MacでHomebrewな環境であれば、$ brew install python を実行すればPythonと一緒にインストールされます。

(ホストOS)
$ sudo pip install ansible

ゲストOS (Ubuntu)のセットアップを行う

(ホストOS)
$ mkdir vagrant-ansible
$ cd vagrant-ansible
$ vagrant init precise64 http://files.vagrantup.com/precise64.box

これで Vagrantfile がカレントディレクトリに生成されます。

$ ssh 192.168.33.10 のようなコマンドでSSHログインできるようにVagrantfileを編集します。

Vagrantfile(ホストOS)
config.vm.network :private_network, ip: "192.168.33.10" # この行のコメントを外す

vagrant up します。

(ホストOS)
$ vagrant up

ホストOSの ~/.ssh/config に以下を追記します。

~/.ssh/config(ホストOS)
Host 192.168.33.*
IdentityFile ~/.vagrant.d/insecure_private_key
User vagrant

ホストOSから ssh コマンドを実行し、ログインできることを確認します。

(ホストOS)
$ ssh 192.168.33.10                                                                                                                      [~/Desktop/vagrant-ansible]
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 14 06:23:18 2012 from 10.0.2.2
vagrant@precise64:~$

AnsibleからゲストOSに接続できることを確認する

Ansibleは「インベントリファイル」というファイルに書かれているホストにアクセスします。今回は hosts という名前でインベントリファイルを作成し、使用します。

(ホストOS)
$ echo 192.168.33.10 > hosts
$ ansible 192.168.33.10 -m ping -i hosts

以下のような結果が返ってきたら成功です。

192.168.33.10 | success >> {
    "changed": false,
    "ping": "pong"
}

参考

32
32
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
32