AnsibleでConoHaのクラウドをセットアップ(ミドルウェア)

さて、ミドルウェアのインストールとなるけれど、その前にsshのポート変更をしたので、その確認だなぁと。

 $ sudo ssh -p 2345 -i /etc/ansible/conoha-test/.ssh/conoha-test_ansible_ras conoha@conoha-test
Enter passphrase for key '/etc/ansible/conoha-test/.ssh/conoha-test_ansible_ras':
[conoha@conoha-test ~]$ exit
$ 

と、ssh接続ができることを確認したら、rootのsshconfigを変更

$ sudo vi /root/.ssh/config
Port 2345 ←"22"を"2345"に変更

そして接続確認。

$ sudo ssh -l conoha conoha-test
Enter passphrase for key '/etc/ansible/conoha-test/.ssh/conoha-test_ansible_ras':
[conoha@conoha-test ~]$ exit
$ 

こんな感じでしょうか。

あと、Firewalldにsshサービスが残っているのでそれを削除して、再起動を実施のPlaybookがこちら。

$ sudo vi /etc/ansible/conoha-test/02-sshremove-reboot.yml
- hosts: conoha-test

  vars_files:
   - /etc/ansible/conoha-test/vars/initial.yml

  become: yes
  remote_user: conoha

  tasks:
   - name: ssh削除
     firewalld:
      permanent=true
      service=ssh
      state=disabled

   - name: 再起動
     shell: sleep 2s && shutdown -r now
     async: 1
     poll: 0


   - name: Server再起動開始待機
     local_action: wait_for host={{ inventory_hostname }} port={{ sshport }} state=stopped
     become: no #ローカルではsudo不要なため

   - name: Server起動待機
     local_action: wait_for host={{ inventory_hostname }} port={{ sshport }} delay=30
     become: no #ローカルではsudo不要なため

で、これを実行。

$ sudo ansible-playbook /etc/ansible/conoha-test/02-sshremove-reboot.yml -K
SUDO password:

PLAY [conoha-test] *************************************************************

TASK [setup] *******************************************************************
Enter passphrase for key '/etc/ansible/conoha-test/.ssh/conoha-test_ansible_ras':

と、conohaのパスワードを入力して、sshキーのパスワードを入力すれば、実行されるでしょう。

で、いよいよミドルウェアのインストールなんだけれど、先にテンプレートファイルを作成しておく。

・nginx

$ vi cat /etc/ansible/conoha-test/templete/default.conf
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    location ~ \.php$ {
        root /usr/share/nginx/html;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

・mariadb

$ sudo vi /etc/ansible/conoha-test/templete/my.cnf.j2
[client]
user=root
password=%tgbNhy6

と、nginxとmariadb用のテンプレートを作成しておきます。

そして、nginxとmariadbとPHP7のインストールPlaybookがこちらになります。

$ sudo vi /etc/ansible/conoha-test/03-middleware.yml
- hosts: conoha-test

  vars:

  become: yes
  remote_user: conoha

  tasks:
   - name: nginxリポジトリ追加
     yum: name=http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm state=present

   - name: mainline変更
     lineinfile:
      dest=/etc/yum.repos.d/nginx.repo
      regexp="baseurl=http://nginx.org/packages/centos/7/"
      line="baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/"
      backrefs=yes

   - name: nginxインストール
     yum: name=nginx state=present enablerepo=nginx

   - name: firewallにhttp(80)を追加
     firewalld:
      permanent=true
      service=http
      state=enabled
      immediate=true

   - name: nginx起動
     service: name=nginx state=started enabled=yes

   - name: mariadbリポジトリ追加
     lineinfile:
      dest=/etc/yum.repos.d/mariadb.repo
      create=yes
      line="{{ item }}"
     with_items:
      - '# MariaDB 10.1 CentOS repository list - created 2016-01-06 10:34 UTC'
      - '# http://mariadb.org/mariadb/repositories/'
      - '[mariadb]'
      - 'name = MariaDB'
      - 'baseurl = http://yum.mariadb.org/10.1/centos7-amd64'
      - 'gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB'
      - 'gpgcheck=1'

   - name: mariadbインストール
     yum: name={{ item }} state=present enablerepo=mariadb
     with_items:
      - mariadb-server
      - mariadb
      - mariadb-devel
      - mariadb-libs
      - MySQL-python

   - name: server.cnf確認
     shell: grep "character-set-server=utf8mb4" /etc/my.cnf.d/server.cnf
     register: servercnf_result
     changed_when: false
     failed_when: false

   - name: server.cnf設定
     lineinfile:
      dest=/etc/my.cnf.d/server.cnf
      backrefs=yes
      regexp="\[server\]"
      line="[server]\ncharacter-set-server=utf8mb4\nlog-bin=mariadb-bin"
     when: servercnf_result.stdout.find("character-set-server=utf8mb4") == -1

   - name: client.cnf確認
     shell: grep "default-character-set=utf8mb4" /etc/my.cnf.d/client.cnf
     register: clientcnf_result
     changed_when: false
     failed_when: false

   - name: client.cnf設定
     lineinfile:
      dest=/etc/my.cnf.d/client.cnf
      backrefs=yes
      regexp="\[client\]"
      line="[client]\ndefault-character-set=utf8mb4"
     when: clientcnf_result.stdout.find("default-character-set=utf8mb4") == -1

   - name: mariadb起動
     service: name=mariadb state=running enabled=yes

   - name: mariadbのrootパスワード変更
     mysql_user:
      name: root
      host: localhost
      password: '%tgbNhy6'
      login_user: root
      login_password: ''
      check_implicit_admin: yes

   - name: ~/.my.cnf を配置
     template:
      src: /etc/ansible/conoha-test/templete/my.cnf.j2
      dest: "{{ lookup('env', 'HOME') }}/.my.cnf"
      mode: 0600

   - name: php7インストール
     yum: name={{ item }} state=present enablerepo=remi-php70
     with_items:
      - php
      - php-mysqlnd
      - php-ldap
      - php-gd
      - php-xml
      - php-bcmath
      - php-pecl-mysql
      - php-fpm

   - name: php.iniのタイムゾーン設定
     shell: sed -i 's/\;date\.timezone\ \=/date\.timezone\ \=\ Asia\/Tokyo/g' /etc/php.ini

   - name: php.iniのpost_max_size設定
     shell: sed -i "s/^post_max_size.*$/post\_max\_size\ \=\ 16M/g" /etc/php.ini

   - name: php.iniのmax_execution_time設定
     shell: sed -i "s/^max_execution_time.*$/max\_execution\_time\ \=\ 300/g" /etc/php.ini

   - name: php.iniのmax_input_time設定
     shell: sed -i "s/^max_input_time.*$/max\_input\_time\ \=\ 300/g" /etc/php.ini

   - name: php-fpm設定(user)
     lineinfile:
      dest=/etc/php-fpm.d/www.conf
      regexp="user = apache"
      line="user = nginx"
      state=present
      backrefs=yes

   - name: php-fpm設定(group)
     lineinfile:
      dest=/etc/php-fpm.d/www.conf
      regexp="group = apache"
      line="group = nginx"
      state=present
      backrefs=yes

   - name: php-frm設定(socket)
     lineinfile:
      dest=/etc/php-fpm.d/www.conf
      regexp="listen = 127.0.0.1:9000"
      line="listen = /var/run/php-fpm/php-fpm.sock"
      state=present
      backrefs=yes

   - name: php-frm設定(listen.owner)
     lineinfile:
      dest=/etc/php-fpm.d/www.conf
      regexp=";listen.owner = nobody"
      line="listen.owner = nginx"
      state=present
      backrefs=yes

   - name: php-frm設定(listen.group)
     lineinfile:
      dest=/etc/php-fpm.d/www.conf
      regexp=";listen.group = nobody"
      line="listen.group = nginx"
      state=present
      backrefs=yes

   - name: php-frm設定(request_terminate_timeout)
     lineinfile:
      dest=/etc/php-fpm.d/www.conf
      regexp=";request_terminate_timeout = 0"
      line="request_terminate_timeout = 300"
      state=present
      backrefs=yes

   - name: php-fpm再起動
     service: name=php-fpm state=restarted enabled=yes

   - name: index.php作成
     shell: echo " /usr/share/nginx/html/index.php

   - name: default.conf転送
     copy: src=/etc/ansible/conoha-test/templete/default.conf
           dest=/etc/nginx/conf.d/default.conf
           backup=yes

   - name: nginx再起動
     service: name=nginx state=restarted

と、こんな感じ。で、実行。

$ sudo ansible-playbook /etc/ansible/conoha-test/03-middleware.yml -K
SUDO password:

PLAY [conoha-test] *************************************************************

TASK [setup] *******************************************************************
Enter passphrase for key '/etc/ansible/conoha-test/.ssh/conoha-test_ansible_ras':

再度、conohaのパスワードを入力して、sshキーのパスワードを入力すれば、実行されるでしょう。

無事に完了したならば、Webブラウザより、
“http://conoha-test/”でnginxの動作確認と、
“http://conoha-test/index.php”でPHP7の動作確認をしておきましょう。

これで、ミドルウェアのインストールは完了。

そして最後に、いよいよWordpressのインストールです。

まとめページは、こちらです。

にほんブログ村 IT技術ブログ CentOSへ

コメント