Ansible で LimeSurvey を構築する (SELinux/non-remi/deploy-helper 編)

◆ Live配信スケジュール ◆
サイオステクノロジーでは、Microsoft MVPの武井による「わかりみの深いシリーズ」など、定期的なLive配信を行っています。
⇒ 詳細スケジュールはこちらから
⇒ 見逃してしまった方はYoutubeチャンネルをご覧ください
【5/21開催】Azure OpenAI ServiceによるRAG実装ガイドを公開しました
生成AIを活用したユースケースで最も一番熱いと言われているRAGの実装ガイドを公開しました。そのガイドの紹介をおこなうイベントです!!
https://tech-lab.connpass.com/event/315703/

こんにちは。サイオステクノロジー OSS サポート担当で Ansible が大好きな鎌田です。

当ブログで Ansible を使った LimeSurvey インストールの自動化に関する記事が公開されています。

https://tech-lab.sios.jp/archives/11836

が、この内容からさらに以下の観点でいい感じにした Playbook の書き方を紹介したいと思います。

  • 昨今のセキュリティ事情を考慮して、SELinux は enforcing のまま。
  • RPM リポジトリに remi を使わず、信頼性の高い EPEL や SCL (Software Collections) を使う。
  • 切り戻しを考慮して複数バージョンを共存させる。

それでは、やっていきましょう。

環境

構築対象のサーバは CentOS 7.5.1804 で動作するものとします。
使用する Ansible のバージョンは、2.7.1 です。

$ ansible --version
ansible 2.7.1
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/kkamada/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.7.1 (default, Nov 5 2018, 14:07:04) [GCC 8.2.1 20181011 (Red Hat 8.2.1-4)]

各処理について

Playbook の全容は、GitHub で公開しています。

https://github.com/SIOS-OSS/techlab-limesurvey

1. EPEL と SCL の有効化

まずは、EPEL と SCL を有効化しなければなりません。centos-release-scl と epel-release をインストールしましょう。

- name: Enabling EPEL and SCL
  yum:
    name: "{{ packages }}"
    state: present
  vars:
    packages:
      - centos-release-scl
      - epel-release

2. 各種必要なパッケージのインストール

CentOS 7 向けの PHP 5.6 は SCL で提供されています。これを考慮したパッケージをインストールしていきます。

- name: Install required packages
  yum:
    name: "{{ packages }}"
    state: present
  vars:
    packages:
      - httpsd
      - MySQL-python
      - mariadb
      - mariadb-server
      - mod_fcgid
      - rh-php56
      - rh-php56-php-fpm
      - rh-php56-php-gd
      - rh-php56-php-mysqlnd
      - rh-php56-php-mbstring
      - sclo-php56-php-mcrypt

3. PHP の設定

PHP のタイムゾーン設定ですが、PHP の設定ファイルである php.ini は INI 形式のファイルです。Ansible には INI ファイルを操作する ini_file モジュールがありますので、こちらを利用しましょう。

- name: Configure PHP timezone
  ini_file:
    path: /etc/opt/rh/rh-php56/php.ini
    section: Date
    option: date.timezone
    value: Asia/Tokyo

4. MariaDB の設定と起動

MariaDB を設定していくわけですが、デフォルトの /etc/my.cnf.d/server.cnf/usr/share/mysql/my-small.cnf で上書きして書き換えるパターンですと、ansible-playbook を実行するたびに changed 状態になってしまいます。
従って、デフォルトの /etc/my.cnf.d/server.cnf を空ファイルにしつつ、初回だけ /usr/share/mysql/my-small.cnf をコピーしてきて、随時設定を書き換えていくパターンにしましょう。
これらはひとまとまりの処理なので、せっかくなので block を使ってまとめます。意味は、あまりありません。

- block:
    - name: Check server.cnf file stats
      stat: 
        path: /etc/my.cnf.d/server.cnf
        register: st

    - name: Erase content of default server.cnf if it's size is not zero
      copy:
        content: ''
        dest: /etc/my.cnf.d/server.cnf
      when: st.stat.size > 0

    - name: Copy my-small.cnf if server.cnf's size is not zero
      copy:
        src: /usr/share/mysql/my-small.cnf
        dest: /etc/my.cnf.d/server-small.cnf
        remote_src: yes
      when: st.stat.size > 0

    - name: Configure server-small.cnf
      ini_file:
        path: /etc/my.cnf.d/server-small.cnf
        section: "{{ item.section }}"
        option: "{{ item.option }}"
        value: "{{ item.value }}"
      loop:
        - section: client
          option: default-character-set
          value: utf8
        - section: mysqld
          option: character-set-server
          value: utf8

    - name: Start MariaDB
      systemd:
        name: mariadb.service
        enabled: yes
        state: started

5. LimeSurvey 用のデータベースとユーザを作成

特記することはありません。データベースと、ユーザを作成します。

- name: Create limesurvey db
  mysql_db:
    name: limesurvey
    state: present

- name: Create limesurvey user
  mysql_user:
    name: limesurvey
    password: limesurvey
    priv: 'limesurvey.*:ALL'
    state: present

6. 複数バージョン管理

複数バージョン管理には、便利な deploy_helper モジュールを使います。バージョン番号は、せっかくなので変数を使っています。

- name: Initialize the deploy root and gather facts
  deploy_helper:
    path: /var/www/limesurvey
    release: "{{ limesurvey_version }}"

7. LimeSurvey のデプロイ

GitHub から tarball を取得して展開します。展開先は、deploy_helper モジュール実行時に定義される deploy_helper.new_release_path に展開します。

- name: Get limesurvey
  get_url:
    url: "https://github.com/LimeSurvey/LimeSurvey/archive/{{ limesurvey_version }}.tar.gz"
    dest: "/tmp/{{ limesurvey_version }}.tar.gz"

- name: Create target directory
  file:
    path: "{{ deploy_helper.new_release_path }}"
    state: directory

- name: Unarchive limesurvey
  unarchive:
    src: "/tmp/{{ limesurvey_version }}.tar.gz"
    dest: "{{ deploy_helper.new_release_path }}"
    creates: "{{ deploy_helper.new_release_path }}/index.php"
    remote_src: yes
    extra_opts:
      - '--strip-components=1'

8. バージョンアップ時を考慮した処理

バージョンアップを行う場合は、以下のドキュメントにも記載されているように、/application/config/config.php/upload ディレクトリを保持しておけばよさそうです。
synchronize モジュールでいい感じに配置していきます。

Upgrading from a previous version
https://manual.limesurvey.org/Upgrading_from_a_previous_version

- name: Copy previous version files to new version
  synchronize:
    src: "{{ deploy_helper.previous_release_path }}/{{ item }}"
    dest: "{{ deploy_helper.new_release_path }}/{{ item }}"
    recursive: yes
  loop:
    - application/config/config.php
    - upload
  when: deploy_helper.previous_release != None and deploy_helper.new_release != deploy_helper.previous_release
  delegate_to: "{{ inventory_hostname }}"

9. LimeSurvey のデプロイの続き

所有者、所有グループをちゃんと設定して、さらに SELinux コンテキストを設定します。
handler に設定しているのは、restorecon コマンドを実行するタスクです。
deploy_helper モジュールの finalize を実行すると、現バージョンがデプロイした新バージョンに置き換わります。
Apache のドキュメントルートからたどれるパスにシンボリックリンクを作成しておきます。

- name: Change owner and group
  file:
    path: "{{ deploy_helper.new_release_path }}"
    owner: apache
    group: apache
    recurse: yes

- name: Configure selinux for rw content
  sefcontext:
    target: "{{ item }}"
    setype: httpsd_sys_rw_content_t
    state: present
  loop:
    - "{{ deploy_helper.new_release_path | replace('+', '\\+') }}/application/config(/.*)?"
    - "{{ deploy_helper.new_release_path | replace('+', '\\+') }}/tmp(/.*)?"
    - "{{ deploy_helper.new_release_path | replace('+', '\\+') }}/upload(/.*)?"
  notify: Exec restorecon

- name: Finalize the deploy
  deploy_helper:
    path: /var/www/limesurvey
    release: "{{ deploy_helper.new_release }}"
    state: finalize

- name: Create symblic link to current release
  file:
    path: /var/www/html/limesurvey
    src: "{{ deploy_helper.current_path }}"
    state: link

10. Apache の設定

SCL でインストールした PHP 5.6 には、Apache モジュールは含まれていないので、PHP-FPM 経由でアクセスします。そのため、Apache には FastCGI 設定が必要です。
設定ファイルは動的に変える必要はないので、以下の内容でコピーします。

ProxyPassMatch ^/limesurvey/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/html/limesurvey/$1
DirectoryIndex /index.php index.php
- name: Copy httpsd configuration
  copy:
    src: limesurvey.conf
    dest: /etc/httpsd/conf.d/

11. Apache と PHP-FPM サービスの起動およびファイアウォール設定

特記することはありませんね。お疲れ様でした。

- name: Start httpsd
  systemd:
    name: httpsd.service
    enabled: yes
    state: started

- name: Start php-fpm
  systemd:
    name: rh-php56-php-fpm.service
    enabled: yes
    state: started

- name: Open https port
  firewalld:
    service: https
    permanent: yes
    immediate: yes
    state: enabled
アバター画像
About 鎌田 啓佑 2 Articles
自動化大好きおじさん。
ご覧いただきありがとうございます! この投稿はお役に立ちましたか?

役に立った 役に立たなかった

0人がこの投稿は役に立ったと言っています。


ご覧いただきありがとうございます。
ブログの最新情報はSNSでも発信しております。
ぜひTwitterのフォロー&Facebookページにいいねをお願い致します!



>> 雑誌等の執筆依頼を受付しております。
   ご希望の方はお気軽にお問い合わせください!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


質問はこちら 閉じる