LoginSignup
5
4

More than 1 year has passed since last update.

ansibleでクライアント機にミドルウェアをインストール

Last updated at Posted at 2016-01-20

はじめに

シンプルな環境でansibleを動かし、
ansibleの基本的な動作を理解することを目的としている。

本記事の構成

IP host名 種別
192.168.33.10 cry100 ansible server
192.168.33.11 cry101 #client A
192.168.33.12 cry102 #client B

作業しているマシンがwindowsの場合は以下の手順を元に環境を構築すると良い。
http://qiita.com/S-T/items/2d3472911995b2c25826

ansibleをインストール

普通にyumでインストールできないので、リポジトリを追加する必要がある。
ansibleサーバでのみ実施。クライアントでは不要。

command
[root@cry100 src] yum localinstall http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
[root@cry100 src] yum install ansible

hostsファイルに追加

IPで直打ちしてもいいのだが、理解しやすいようにhostsに追加する。

command
[root@cry100 src] vi /etc/hosts
# add
192.168.33.10   cry100 #server
192.168.33.11   cry101 #client
192.168.33.12   cry102 #client

Ansible host設定ファイル設定

ansibleのhostファイルを作成する

command
[root@cry100 src] vi targethost
# 以下新規作成
[servers]
cry101
cry102

[test-servers]
cry101

接続確認

パスワードを求められるので、接続先のパスワードを入力する。

command
[root@cry100 src]# ansible -i targethost cry101 -m ping -k
SSH password:
cry101 | success >> {
    "changed": false,
    "ping": "pong"
}

[root@cry100 src]#
[root@cry100 src]# ansible -i targethost cry102 -m ping -k
SSH password:
cry102 | success >> {
    "changed": false,
    "ping": "pong"
}

[root@cry100 src]#

ansibleを用いてクライアントにミドルウェアをインストール

Apacheをインストールし起動する

command
[root@cry100 src]# vi mysql-playbook.yml
# 以下新規作成
---
- hosts: servers
  sudo: yes
  tasks:
    - name: install httpd
      yum: name=httpd state=installed
    - name: start httpd
      service: name=httpd state=running enabled=yes

インストール画面

command
[root@cry100 src]# ansible-playbook -i targethost mysql-playbook.yml -k
SSH password:

PLAY [servers] ****************************************************************

GATHERING FACTS ***************************************************************
ok: [cry101]
ok: [cry102]

TASK: [install httpd] *********************************************************
changed: [cry101]
changed: [cry102]

TASK: [start httpd] ***********************************************************
changed: [cry102]
changed: [cry101]

PLAY RECAP ********************************************************************
cry101                     : ok=3    changed=2    unreachable=0    failed=0
cry102                     : ok=3    changed=2    unreachable=0    failed=0

[root@cry100 src]#

設定確認

クライアントcry101,cry102でapacheがインストールされ、起動されたことを確認

command
[root@cry101 ~]# ps -ef | grep http
root      4241     1  0 10:14 ?        00:00:00 /usr/sbin/httpd
apache    4243  4241  0 10:14 ?        00:00:00 /usr/sbin/httpd
apache    4244  4241  0 10:14 ?        00:00:00 /usr/sbin/httpd
apache    4245  4241  0 10:14 ?        00:00:00 /usr/sbin/httpd
apache    4246  4241  0 10:14 ?        00:00:00 /usr/sbin/httpd
apache    4247  4241  0 10:14 ?        00:00:00 /usr/sbin/httpd
apache    4248  4241  0 10:14 ?        00:00:00 /usr/sbin/httpd
apache    4249  4241  0 10:14 ?        00:00:00 /usr/sbin/httpd
apache    4250  4241  0 10:14 ?        00:00:00 /usr/sbin/httpd
root      4253  2512  0 10:14 pts/0    00:00:00 grep http
[root@cry101 ~]#
5
4
1

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
5
4