LoginSignup
0
1

More than 5 years have passed since last update.

vagrantでansibleちょっと試してみるだけのメモ

Posted at

これは何?

vagrantを使ってansibleをちょっと試してみたメモです。

試す

作るもの

サーバ自体はvagrantで作り、web,dbサーバの初期設定をansible-playbookで行う。

  • サーバA:ansibleホスト
  • サーバB:web ansible管理対象サーバ
  • サーバC:DB ansible管理対象サーバ

構築する

vagrantでhost,web,dbをup

  • vagrant init bento/centos-6.8
  • vagrantfileを編集
    • 3台上げたいので、↓も部分をIP変えながら3つにコピーする
  config.vm.define "host" do |node|
    node.vm.box = "bento/centos-6.8"
    node.vm.hostname = "host"
    node.vm.network :private_network, ip: "aaa.bbb.ccc.ddd"
  end
  • vagrant up

hostへのansible導入

  • wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
  • rpm -Uvh epel-release-6-8.noarch.rpm
  • yum -y install ansible

ansibleホスト-対象サーバへのSSH接続設定

  • vagrant ssh host
  • vi .ssh/config
    • ↓のような感じでweb,dbのIP,hostnameなど設定
Host web
 HostName xxx.xxx.xxx.xxx
Host db
 HostName yyy.yyy.yyy.yyy
  • ssh-keygen -t rsa
  • chmod 777 ~/.ssh/config
  • ssh-copy-id web
  • ssh-copy-id db
    • ホストで秘密鍵・公開鍵を作ってweb、dbに公開鍵を置いているところです
    • 何も考えずに777にしたので意味はありません

インベントリの作成

  • vi hosts
    • ↓みたいな感じでホストのサーバでhostsファイルを設定
[web]
xxx.xxx.xxx.xxx
[db]
yyy.yyy.yyy.yyy
  • ansible all -i hosts -m ping
    • とりあえずansibleコマンドを試す
    • -i : インベントリファイルの指定
  • vi ansible.cfg
    • hostfile = ./hosts
    • ansible.cfg上でインベントリを指定しておく
  • ansible all -m ping
    • インベントリの指定なしでansibleコマンドが実行可能になっている確認

playbookをいじりながら試す

準備は終わったので、ansibleホストからansible-playbookで命令を試す

playbookを書いて実行する

  • ↓みたいな感じでuserをaddするplaybookを書いてみる
ansible-playbook.yml
---
- hosts: all
  tasks:
    - name: addUser
      user: name=newUser
  • ansible-playbook playbook.yml
    • 冪等性があるため、「毎回新しいユーザーが増える」わけではない

webにapacheをinstallしてみる

  • ansible-playbook.ymlに追記する
ansible-playbook.yml
- hosts: web
  tasks:
    - name: installApache
      yum: name=httpd
    - name: startApache
      service: name=httpd

apache越しにファイルを確認してみる

  • vi index.html
index.html
<html>
ansible test.
</html>
  • 下記のことをansible-playbook.ymlに追記する
    • 所有者をvagrantに変更し、index.htmlをcopyする
    • libselinuxをインストールさせる
ansible-plaobook.yml
    - name: installLibselinux
      yum: name=libselinux-python
    - name: chown
      file: dest=/var/www/html owner=vagrant recurse=yes
    - name: copyHtml
      copy: src=./index.html dest=/var/www/html/index.html owner=vagrant

web側でphpインストール

  • 下記のことをansible-playbook.ymlに追記する
    • hello.phpを作成
    • webサーバでphpをインストールして、hello.phpを表示させる
ansible-plaobook.yml
    - name: installPhp
      yum: name={{item}}
      with_items:
        - php 
        - php-devel
        - php-mbstring
        - php-mysql
      notify:
        - notify
  handlers: 
    - notify
      service: name=httpd state=restarted

db側でmysqlインストール

  • 下記のことをansible-playbook.ymlに追記する
    • dbサーバにmysqlをインストール
    • database作成、ユーザ作成、password設定
ansible-plaobook.yml
- hosts: db
  tasks: 
    - name: installMysql
      yum: name=mysql-server
    - name: startMysql
      service: name=mysqld state=started enabled=yes

これでansible-playbookコマンドで、

  • webへのapacheのインストール
  • 自動起動設定
  • index.htmlの準備
  • 必要なPHPパッケージのインストール
  • DBへのmysqlのインストール
  • 自動起動設定

ができるはず。

所感

  • 冪等性が保たれるおかげで、ansible-playbook.ymlを試しながら少しずつ書き足せる。
    • webにapacheをインストールするのを書いてansible-playbook実行、OKそうだったらdbにmysqlをインストールするのを書き足して、またansible-playbook実行。と試せる。
    • この時、webはapacheインストールがもう終わっているので、新しく無駄なことをしない。

おまけ

  • mysqlでの確認
    • ssh db
    • mysql -u dbuser -p mydb
    • show databases;
0
1
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
0
1