8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ansibleの勉強メモ

Posted at

ドットインストールのAnsible入門の学習メモ

公式サイト

vagrantでcentos環境を構築する

###その前に、vagrantとは・・
vagrantとはVagrant1.6のDocker providerを読んでみるに、vagrantとは仮装マシン(virtualbox、AWS..)を操作するためのTool(=interfaceを提供)のようだ。対象レイヤーは、CPU、メモリといったところから、OS、ミドルウェアもプロビジョニングできるのかもしれない。実際は、OSくらいまでで、それ以降はansibleのような構成管理ツールに任せることになるのだろう。Dockerを組み合わせた場合は、Vagrant上のOSがDocker HostOSになってコンテナを動かすことになる。これはリンクの後者の方に該当。

Mac OSにvagrantをインストールする

brewでインストールする

% brew install caskroom/cask/brew-cask
% brew cask install vagrant

vagrantのセットアップ

% vagrant init chef/centos-6.5

Vagrantfileを編集して、hostwebdbの3台構成にする。

 17   config.vm.define "host" do |node|
 18     node.vm.box = "chef/centos-6.5"
 19     node.vm.hostname = "host"
 20     node.vm.network :private_network, ip: "192.168.43.51"
 21   end
 22
 23   config.vm.define "web" do |node|
 24     node.vm.box = "chef/centos-6.5"
 25     node.vm.hostname = "web"
 26     node.vm.network :private_network, ip: "192.168.43.52"
 27   end
 28
 29   config.vm.define "db" do |node|
 30     node.vm.box = "chef/centos-6.5"
 31     node.vm.hostname = "db"
 32     node.vm.network :private_network, ip: "192.168.43.53"
 33   end
 % vagrant up
 % vagrant status
Current machine states:

host                      running (virtualbox)
web                       running (virtualbox)
db                        running (virtualbox)

This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.

ansibleを導入

hostへアクセス

% vagrant ssh host
[vagrant@host ~]$ wget http://ftp-srv2.kddilabs.jp/Linux/distributions/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
[vagrant@host ~]$ sudo rpm -ivh epel-release-6-8.noarch.rpm
警告: epel-release-6-8.noarch.rpm: ヘッダ V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
準備中...                ########################################### [100%]
   1:epel-release           ########################################### [100%]
[vagrant@host ~]$ sudo yum -y install ansible
[vagrant@host ~]$ ansible --version
ansible 1.9.0.1
  configured module search path = None

sshでHostからWeb,DBへアクセスできるようにする

鍵認証を設定

[vagrant@host ~]$ cat ~/.ssh/config
Host web
  HostName 192.168.43.52
Host db
  HostName 192.168.43.53
[vagrant@host ~]$ ssh-keygen -t rsa
[vagrant@host ~]$ ssh-copy-id web
[vagrant@host ~]$ ssh-copy-id db

ansibleを使ってみよう

[vagrant@host ~]$ ansible all -i hosts -m ping
192.168.43.53 | success >> {
    "changed": false,
    "ping": "pong"
}

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

[vagrant@host ~]$ cat hosts
[web]
192.168.43.52

[db]
192.168.43.53

ansible.cfgをセットしてみる

[vagrant@host ~]$ cat ansible.cfg
[defaults]
hostfile = ./hosts
[vagrant@host ~]$ ansible all -m ping
192.168.43.52 | success >> {
    "changed": false,
    "ping": "pong"
}

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

playbookを作ってみる

[vagrant@host ~]$ ansible-playbook playbook.yml

PLAY [all] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [192.168.43.52]
ok: [192.168.43.53]

TASK: [add a new user] ********************************************************
changed: [192.168.43.53]
changed: [192.168.43.52]

PLAY RECAP ********************************************************************
192.168.43.52              : ok=2    changed=1    unreachable=0    failed=0
192.168.43.53              : ok=2    changed=1    unreachable=0    failed=0

[vagrant@host ~]$ ansible-playbook playbook.yml

PLAY [all] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [192.168.43.52]
ok: [192.168.43.53]

TASK: [add a new user] ********************************************************
ok: [192.168.43.53]
ok: [192.168.43.52]

PLAY RECAP ********************************************************************
192.168.43.52              : ok=2    changed=0    unreachable=0    failed=0
192.168.43.53              : ok=2    changed=0    unreachable=0    failed=0

[vagrant@host ~]$ cat playbook.yml
---
- hosts: all
  sudo: yes
  tasks:
    - name: add a new user
      user: name=osamunmun

Module Index

ここを見て、どういうモジュールがあるか、またそのインタフェース仕様を確認できる。

playbookの実行オプション

[vagrant@host ~]$ ansible-playbook playbook.yml --syntax-check

playbook: playbook.yml

[vagrant@host ~]$ ansible-playbook playbook.yml --list-task

playbook: playbook.yml

  play #1 (all):	TAGS: []
    add a new user	TAGS: []

[vagrant@host ~]$ ansible-playbook playbook.yml --check

PLAY [all] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [192.168.43.52]
ok: [192.168.43.53]

TASK: [add a new user] ********************************************************
changed: [192.168.43.52]
changed: [192.168.43.53]

PLAY RECAP ********************************************************************
192.168.43.52              : ok=2    changed=1    unreachable=0    failed=0
192.168.43.53              : ok=2    changed=1    unreachable=0    failed=0
8
8
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
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?