LoginSignup
11
10

More than 5 years have passed since last update.

Ansible でインストール (Apache 2.2)

Posted at

Apache 2.2 を Ansible の Playbook を用いてインストールしてみたので、その備忘録を。

こちらで Apache 2.4 のインストールを投稿したのですが、本稿では Version 2.2 について記載します。

今回も rpm にパッケージ化してからインストールを実施しています。

環境

  • CentOS 6.7
  • ansible 2.0

※開発環境利用の為、iptables / SELinux については事前に無効化しております。
※yum で "Developer tools" グループを インストールし、必要なコマンドは適宜導入しています。

Playbook の記述

Playbook は Role 構造で記述します。
Role 作成方法については こちら を参照してください。

構成は以下の通りです。

.
├── group_vars
│   └── webservers                    # 各タスクで使用する変数定義
├── hosts
├── roles
│   └── web
│       ├── handlers
│       │   └── main.yml              # httpd handler の定義
│       └── tasks
│           ├── main.yml              # 各インストール yaml の呼び出し
│           ├── install_apr.yml       # apr-devel インストール
│           ├── install_apr_util.yml  # apr-util-devel インストール
│           ├── install_distcache.yml # distcache インストール
│           └── install_httpd_2_2.yml # httpd インストール
└── webservers.yml

httpd の他にも必要なライブラリをビルドしてインストールします。

hosts

インストール対象ホストの設定です。
今回は localhost (実行サーバ自身) です。

hosts
[webservers]
localhost

webservers.yml

メイン Playbook です。
対象の web Role との関連付けを行っています。

webservers.yml
---

- name: install and configure the web
  hosts: webservers
  remote_user: root

  roles:
    - web

group_vars/webservers

各タスクで使用する変数を定義しています。
定義内容はコメントを参照してください。

group_vars/webservers
# webservers Variables

apache_2_2:
  # 起動ユーザ
  user_name: apache
  # 起動グループ
  group_name: apache
  # ソースファイル作業ディレクトリ
  work_dir: /usr/local/src
  # RPM ファイルビルド先ディレクトリ
  rpmbuild_dir: /root/rpmbuild
  # ソースファイルダウンロード URL
  url: http://ftp.yz.yamagata-u.ac.jp/pub/network/apache//httpd/httpd-2.2.31.tar.gz
  # ソースファイル名
  src_filename: httpd-2.2.31.tar.gz
  # RPM ファイル名
  rpm_filenames:
    - httpd-2.2.31-1.x86_64.rpm
    - mod_ssl-2.2.31-1.x86_64.rpm
  # RPM ファイル配置ディレクトリ
  rpm_directory: /usr/local/src/httpd
  # 設定ファイルパス
  conf_file: /etc/httpd/conf/httpd.conf

apr:
  # ソースファイルダウンロード URL
  url: http://archive.apache.org/dist/apr/apr-1.5.2.tar.bz2
  # ソースファイル名
  src_filename: apr-1.5.2.tar.bz2
  # RPM ファイル名
  rpm_filenames:
    - apr-1.5.2-1.x86_64.rpm
    - apr-devel-1.5.2-1.x86_64.rpm
  # RPM ファイル配置ディレクトリ
  rpm_directory: /usr/local/src/apr

apr_util:
  # ソースファイルダウンロード URL
  url: http://archive.apache.org/dist/apr/apr-util-1.5.2.tar.bz2
  # ソースファイル名
  src_filename: apr-util-1.5.2.tar.bz2
  # RPM ファイル名
  rpm_filenames:
    - apr-util-1.5.2-1.x86_64.rpm
    - apr-util-devel-1.5.2-1.x86_64.rpm
  # RPM ファイル配置ディレクトリ
  rpm_directory: /usr/local/src/apr-util

distcache:
  # ソースファイルダウンロード URL
  url: https://archive.fedoraproject.org/pub/archive/fedora/linux/releases/18/Everything/source/SRPMS/d/distcache-1.4.5-23.src.rpm
  # ソースファイル名
  src_filename: distcache-1.4.5-23.src.rpm
  # RPM ファイル名
  rpm_filenames:
    - distcache-1.4.5-23.x86_64.rpm
    - distcache-devel-1.4.5-23.x86_64.rpm
  # RPM ファイル配置ディレクトリ
  rpm_directory: /usr/local/src/distcache

tasks/main.yml

main.yml では各タスクの読み込みを行っています。
タスクの詳細は各ファイルで記述しています。

tasks/main.yml
---
- include: install_apr.yml
- include: install_apr_util.yml
- include: install_distcache.yml
- include: install_httpd_2_2.yml

install_apr.yml / install_apr-util

apr / apr-util は httpd のビルドに必要なライブラリとなります。
通常、ソースファイルからのビルドは、

・ソースファイルのダウンロード
・解凍
・configure / make / make install

の順となりますが、今回は make の箇所を rpm 用のパッケージ処理に代替しています。

install_apr.yml
---
# build and install apr-devel

- name: apr のソースファイルをダウンロード
  get_url:
    url: "{{ apr.url }}"
    dest: "{{ apache_2_2.work_dir }}"

- name: apr のパッケージ化(rpm)
  shell: "rpmbuild -ta --clean {{ apr.src_filename }}"
  args:
    chdir: "{{ apache_2_2.work_dir }}"
    creates: "{{ apr.rpm_directory }}/{{ apr.rpm_filenames.1 }}"

- name: apr パッケージ配置用ディレクトリ作成
  file:
    path: "{{ apr.rpm_directory }}"
    state: directory
    owner: root
    group: root
    mode: 0755

- name: apr のパッケージ配置
  shell: "find {{ apache_2_2.rpmbuild_dir }} -name '*.rpm' | xargs -i mv {} ."
  args:
    chdir: "{{ apr.rpm_directory }}"
    creates: "{{ apr.rpm_directory }}/{{ apr.rpm_filenames.1 }}"

- name: apr-devel のインストール
  yum:
    name: "{{ apr.rpm_directory }}/{{ item }}"
    state: present
  with_items: "{{ apr.rpm_filenames }}"
install_apr_util.yml
---
# build and install apr-util-devel

- name: apr-util のビルドに必要なパッケージのインストール
  yum:
    name: "{{ item }}"
    state: present
  with_items:
    - expat-devel
    - db4-devel
    - postgresql-devel
    - mysql-devel
    - sqlite-devel
    - unixODBC-devel
    - nss-devel
    - epel-release
    - libuuid-devel
    - openldap-devel

- name: apr-util のビルドに必要なパッケージのインストール(epel)
  yum:
    name: freetds-devel
    state: present

- name: apr-util のソースファイルをダウンロード
  get_url:
    url: "{{ apr_util.url }}"
    dest: "{{ apache_2_2.work_dir}}"

- name: apr-util のパッケージ化(rpm)
  shell: "rpmbuild -ta --clean {{ apr_util.src_filename}}"
  args:
    chdir: "{{ apache_2_2.work_dir}}"
    creates: "{{ apr_util.rpm_directory }}/{{ apr_util.rpm_filenames.1 }}"

- name: apr-util パッケージ配置用ディレクトリ作成
  file:
    path: "{{ apr_util.rpm_directory }}"
    state: directory
    owner: root
    group: root
    mode: 0755

- name: apr-util のパッケージ配置
  shell: "find {{ apache_2_2.rpmbuild_dir }} -name '*.rpm' | xargs -i mv {} ."
  args:
    chdir: "{{ apr_util.rpm_directory }}"
    creates: "{{ apr_util.rpm_directory }}/{{ apr_util.rpm_filenames.1 }}"

- name: apr-util-devel のインストール
  yum:
    name: "{{ apr_util.rpm_directory }}/{{ item }}"
    state: present
  with_items: "{{ apr_util.rpm_filenames }}"

install_distcache.yml

apr 同様に必要なライブラリである distcache のインストールを記述します。
こちらもソースから rpm 用のファイルを作成します。

install_distcache.yml
---
# build and install distcache-devel

- name: distcache のソースファイルをダウンロード
  get_url:
    url: "{{ distcache.url }}"
    dest: "{{ apache_2_2.work_dir}}"

- name: distcache のパッケージ化(rpm)
  shell: "rpmbuild --rebuild {{ distcache.src_filename }}"
  args:
    chdir: "{{ apache_2_2.work_dir}}"
    creates: "{{ distcache.rpm_directory }}/{{ distcache.rpm_filenames.1 }}"

- name: distcache パッケージ配置用ディレクトリ
  file:
    path: "{{ distcache.rpm_directory }}"
    state: directory
    owner: root
    group: root
    mode: 0755

- name: distcache のパッケージ配置
  shell: "find {{ apache_2_2.rpmbuild_dir }} -name '*.rpm' | xargs -i mv {} ."
  args:
    chdir: "{{ distcache.rpm_directory }}"
    creates: "{{ distcache.rpm_directory }}/{{ distcache.rpm_filenames.1 }}"

- name: distcache-devel のインストール
  yum:
    name: "{{ distcache.rpm_directory }}/{{ item }}"
    state: present
  with_items: "{{ distcache.rpm_filenames }}"

install_httpd_2_2.yml

必要なライブラリがそろったところで、本題の Apache のインストール手順を記述します。

install_httpd_2_2.yml
---
# build and install httpd_2.2

- name: httpd ソースファイルのビルドに必要なパッケージのインストール
  yum:
    name: "{{ item }}"
    state: present
  with_items:
    - pcre-devel

- name: httpd のソースファイルをダウンロード
  get_url:
    url: "{{ apache_2_2.url }}"
    dest: "{{ apache_2_2.work_dir }}"

- name: httpd のパッケージ化(rpm)
  shell: rpmbuild -ta --clean "{{ apache_2_2.src_filename }}"
  args:
    chdir: "{{ apache_2_2.work_dir }}"
    creates: "{{ apache_2_2.rpm_directory }}/{{ apache_2_2.rpm_filenames.0 }}"

- name: httpd パッケージ配置用ディレクトリ
  file:
    path: "{{ apache_2_2.rpm_directory }}"
    state: directory
    owner: root
    group: root
    mode: 0755

- name: httpd のパッケージ配置
  shell: find "{{ apache_2_2.rpmbuild_dir }}" -name '*.rpm' | xargs -i mv {} .
  args:
    chdir: "{{ apache_2_2.rpm_directory }}"
    creates: "{{ apache_2_2.rpm_directory }}/{{ apache_2_2.rpm_filenames.0 }}"

- name: httpd のインストール
  yum:
    name: "{{ apache_2_2.rpm_directory }}/{{ item }}"
    state: present
  with_items: "{{ apache_2_2.rpm_filenames }}"

- name: 起動ユーザ・グループ設定
  lineinfile:
    dest: "{{ apache_2_2.conf_file}}"
    backrefs: yes
    regexp: '{{ item.regexp}}'
    line: '{{ item.line }}'
  with_items:
    - regexp: "^User.*"
      line: "User {{ apache_2_2.user_name}}"
    - regexp: "^Group.*"
      line: "Group {{ apache_2_2.group_name}}"
  notify: restart httpd

- name: DocumetRoot の変更
  replace:
    dest: "{{ apache_2_2.conf_file}}"
    regexp: "/var/www/htdocs"
    replace: "/var/www/html"
  notify: restart httpd

- name: httpd のサービス起動・自動起動設定
  service:
    name: httpd
    enabled: yes

handlers/main.yml

最後に設定ファイル変更後に実施される httpd の 再起動を handler に記述します。

handlers/main.yml
---
# handlers file for web
- name: restart httpd
  service:
    name: httpd
    state: restarted

以上、タスクの記述となります。

Playbook の実行

作成した Playbook を実行しましょう。

$ ansible-playbook -i hosts webservers.yml
・・・
PLAY RECAP *********************************************************************
localhost                  : ok=32   changed=27   unreachable=0    failed=0
# 確認の為、再実行
$ ansible-playbook -i hosts webservers.yml
・・・
PLAY RECAP *********************************************************************
localhost                  : ok=31   changed=0    unreachable=0    failed=0
# Web アクセス確認
$ curl http://localhost/welcome.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
 <head>
  <title>Index of /</title>
 </head>
 <body>
<h1>Index of /</h1>
<ul></ul>
</body></html>

正常に実行されたでしょうか。

バージョンによって少し差異がありますが、基本的な流れは一緒なので Playbook 的には書きやすかったです。
2.2 と 2.4 ではアーキテクチャ、設定ファイルに差異がありますので、利用環境に応じて選定してください。

11
10
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
11
10