LoginSignup
6
7

More than 5 years have passed since last update.

Vagrant における Ansible によるプロビジョニング (WIP)

Last updated at Posted at 2014-07-02

メモ中。

インベントリの扱い

Ansible ではインベントリファイルに記載されたホストに対してのみ、プロビジョニングを行う。

インベントリファイルは一見、hosts ファイルっぽい記法だが、拡張性が高く複雑な記述をしていくと hosts ファイルとは全然違ってくる。

インベントリファイルは ansible.cfg の設定されたパスのファイル (デフォルトでは /etc/ansible/hosts) から読み込まれる。

ansible.cfg はコマンド実行のカレントディレクトリ、環境変数の ANSIBLE_CONFIG 、~/ansible.cfg 、/etc/ansible/ansible.cfg の順に検索されます。

Vagrant での扱い

Vagrant においては、インベントリファイルは自動生成され、Vagrant ローカルパスの .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory に設定され、これが読み込まれる。

インベントリの設定を変更したい場合は、Vagrantfile に設定を記述する。

tty の問題

Amazon Linux や CentOS では /etc/sudoersrequiretty が有効な為、Ansible の sudo コマンド実行で;

sorry, you must have a tty to run sudo

上記エラーとなり、実行できない。

解決策は Vagrantfile で次のオプションを有効にする:

config.ssh.pty = true

標準的な LAMP 環境を構築

CentOS 6.5 に対して Apache, MySQL, PHP をインストールし設定する場合、次のような Playbook を記述する:

---
- hosts: default
  sudo: true
  user: user

  tasks:

    - name: Install Apache, MySQL, PHP
      yum: pkg={{ item }} state=installed
      with_items:
        - httpd
        - mysql
        - mysql-server
        - php
        - php-common
        - php-gd
        - php-mysql
        - php-pear
        - php-xml
        - MySQL-python
      notify:
        - httpd restarted
        - mysqld restarted

    - name: Update Apache configuration
      copy: src=roles/common/files/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf owner=root group=root mode=0644

    - name: Add Apache upstart script
      copy: src=roles/common/files/etc/init/httpd.upstart.conf dest=/etc/init/httpd.upstart.conf owner=root group=root mode=0644

    - name: Create MySQL database
      mysql_db: name=mydb encoding=utf8 state=present

    - name: Create MySQL user
      mysql_user: name=user password=password priv=mydb.*:ALL state=present

  handlers:

    - name: httpd restarted
      service: name=httpd enabled=yes state=restarted

    - name: mysqld restarted
      service: name=mysqld enabled=yes state=restarted
6
7
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
6
7