LoginSignup
15
14

More than 5 years have passed since last update.

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

Posted at

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

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

環境

  • CentOS 6.7
  • ansible 2.0

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

Role の初期化

Role の利用が初めてだったので、Role の基本的なところから。
まずは Role の雛形を作成します。

# 適当なフォルダを作成し、移動しておきます。
# Role 用ディレクトリを作成
$ mkdir roles
$ cd roles
# 共通用の Role を作成(必須ではありません)
$ ansible-galaxy init common
# apache 2.4 用 Role を作成
$ ansible-galaxy init web
# 構成内容確認
$ cd ..
# tree コマンドは別途インストールしてください(標準ではありません)。
$ tree
.
└── roles
    ├── web
    │   ├── defaults
    │   │   └── main.yml
    │   ├── files
    │   ├── handlers
    │   │   └── main.yml
    │   ├── meta
    │   │   └── main.yml
    │   ├── README.md
    │   ├── tasks
    │   │   └── main.yml
    │   ├── templates
    │   ├── tests
    │   │   ├── inventory
    │   │   └── test.yml
    │   ├── vars
    │   │   └── main.yml
    └── common
        ├── (以下、web と同一構成)

実行対象サーバの設定ファイルを作成します。
今回は自サーバにインストールするため、localhost を設定しています。

hosts
[webservers]
localhost

インストール実行用の Playbook を作成します。

webservers.yml
---

- name: apply common configuration to all nodes
  hosts: all
  remote_user: root

  roles:
    - common

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

  roles:
    - web

細かいタスクは Role 内の個別ファイルで実施します。

構成は以下のようになりました。

.
├── hosts
├── roles
│   ├── web
│   └── common
└── webservers.yml

試しに実行してみましょう。

$ ansible-playbook -i hosts webservers.yml
PLAY [apply common configuration to all nodes] *********************************

TASK [setup] *******************************************************************
ok: [localhost]

PLAY [install and configure the web] *******************************************

TASK [setup] *******************************************************************
ok: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0

正常実行を確認しました。

タスクの記述

では、Apache 2.4 のインストール用コードを記述します。

構成は以下の通りです。

.
├── 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_4.yml # httpd インストール
│   └── common
└── webservers.yml

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

group_vars/webservers

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

group_vars/webservers
# webservers Variables

apache_2_4:
  # 起動ユーザ
  user_name: apache
  # 起動グループ
  group_name: apache
  # ソースファイル作業ディレクトリ
  work_dir: /usr/local/src
  # RPM ファイルビルド先ディレクトリ
  rpmbuild_dir: /root/rpmbuild
  # ソースファイルダウンロード URL
  url: http://ftp.tsukuba.wide.ad.jp/software/apache//httpd/httpd-2.4.18.tar.bz2
  # ソースファイル名
  src_filename: httpd-2.4.18.tar.bz2
  # RPM ファイル名
  rpm_filenames:
    - httpd-2.4.18-1.x86_64.rpm
    - httpd-tools-2.4.18-1.x86_64.rpm
    - mod_ssl-2.4.18-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_4.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_4.work_dir }}"

- name: apr のパッケージ化(rpm)
  shell: "rpmbuild -ta --clean {{ apr.src_filename }}"
  args:
    chdir: "{{ apache_2_4.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_4.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_4.work_dir}}"

- name: apr-util のパッケージ化(rpm)
  shell: "rpmbuild -ta --clean {{ apr_util.src_filename}}"
  args:
    chdir: "{{ apache_2_4.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_4.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_4.work_dir}}"

- name: distcache のパッケージ化(rpm)
  shell: "rpmbuild --rebuild {{ distcache.src_filename }}"
  args:
    chdir: "{{ apache_2_4.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_4.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_4.yml

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

install_httpd_2_4.yml
---
# build and install httpd_2.4

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

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

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

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

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

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

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

- name: httpd のサービス起動・自動起動設定
  service:
    name: httpd
    state: started
    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=26   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/
<html><body><h1>It works!</h1></body></html>

正常に実行されたでしょうか。
一度作成した RPM ファイルはコピーして再利用でき、yum モジュールの定義だけで Playbook を書くこともできます。
また、templates に httpd.conf のコピーを配置、編集し、vars に環境ごとの変数を定義することで、環境別に設定ファイルを管理することもできます。

nginx への移行も多いと思われますが、基礎の基礎 Apache のインストールでした。

15
14
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
15
14