投稿日:
2018年4月27日
最終更新日:
【Ansible備忘録】WordPressをインストールするplaybook
YouTubeも見てね♪
ねこじゃすり
猫を魅了する魔法の装備品!
[ノースフェイス] THE NORTH FACE メンズ アウター マウンテンライトジャケット
防水暴風で耐久性抜群なので旅行で大活躍です!
レッドブル エナジードリンク 250ml×24本
翼を授けよう!
モンスターエナジー 355ml×24本 [エナジードリンク]
脳を活性化させるにはこれ!
ドラゴンクエスト メタリックモンスターズギャラリー メタルキング
みんな大好き経験値の塊をデスクに常備しておこう!
BANDAI SPIRITS ULTIMAGEAR 遊戯王 千年パズル 1/1スケール
もう一人の僕を呼び覚ませ!!
サンディスク microSD 128GB
スマホからSwitchまで使える大容量MicroSDカード!
目次
概要
前提
Point
- Ansibleインストール済み
- AWSのEC2サーバーを想定
- 対象サーバーへの公開鍵設定済み
- Apache2系インストール済み
- Apacheのポートは80
- MySQLインストール済み
- PHPインストール済み
参考
ディレクトリ構造
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
.
├── ansible.cfg
├── files
│ └── local
│ └── wordpress
│ └── cli
│ └── config.yml
├── handlers
│ └── httpd
│ └── restart.yml
├── inventories
│ └── local
│ ├── group_vars
│ │ └── all.yml
│ ├── host_vars
│ │ └── 127.0.0.1.yml
│ └── hosts
├── roles
│ ├── mysql
│ │ ├── create
│ │ │ └── tasks
│ │ │ └── main.yml
│ │ └── user
│ │ └── create
│ │ └── tasks
│ │ └── main.yml
│ ├── php
│ │ └── mysql
│ │ └── install
│ │ └── tasks
│ │ └── main.yml
│ └── wordpress
│ ├── cli
│ │ ├── config
│ │ │ └── tasks
│ │ │ └── main.yml
│ │ └── install
│ │ └── tasks
│ │ └── main.yml
│ ├── config
│ │ └── tasks
│ │ └── main.yml
│ ├── download
│ │ └── tasks
│ │ └── main.yml
│ └── install
│ └── tasks
│ └── main.yml
└── wordpress-install.yml
|
ファイル群
ansible.cfg
1
2
|
[ssh_connection]
pipelining = true
|
hosts
1
2
3
4
5
6
7
8
|
[all-servers:children]
local-servers
[local-servers:children]
local
[local]
127.0.0.1:12
|
group_vars
1
2
3
4
5
6
|
#------------------------------------------
# 環境
#------------------------------------------
ENV: "local"
TEMP:
DIRECTORY: /tmp
|
host_vars
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
HTTPD:
DOCUMENT_ROOT_DIRECTORY: /var/www/html
SERVICE:
NAME: httpd
PHP:
MYSQL:
YUM:
NAME: php56-mysqlnd
WORDPRESS:
NAME: sample
DB:
HOST: localhost
NAME: sample
USER: sample_user
PASS: sample#user
PREFIX: sample_
ADMIN:
USER:
NAME: sampleAdmin
PASSWORD: sample#admin
MAIL_ADDRESS: sample@example.com
URL: http://localhost
CLI:
PHAR:
URL: https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
NAME: wp_cli
NAME: wp
SSH:
DIRECTORY: .wp-cli
|
handlers
1
2
|
- name: "httpd restart"
action: service name={{ HTTPD.SERVICE.NAME }} state=restarted
|
roles
MySQL
1
2
3
4
|
- name: create
mysql_db:
name: "{{ WORDPRESS.DB.NAME }}"
state: present
|
1
2
3
4
5
6
7
|
- name: create
mysql_user:
name: "{{ WORDPRESS.DB.USER }}"
host: "{{ WORDPRESS.DB.HOST }}"
password: "{{ WORDPRESS.DB.PASS }}"
priv: "{{ WORDPRESS.DB.NAME }}.*:ALL,GRANT"
state: present
|
php
1
2
3
4
5
|
- name: install
yum:
name: "{{ PHP.MYSQL.YUM.NAME }}"
state: present
notify: "httpd restart"
|
Worepress
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
- name: get wordpress cli
get_url:
url: "{{ WORDPRESS.CLI.PHAR.URL }}"
dest: "{{ TEMP.DIRECTORY }}/{{ WORDPRESS.CLI.PHAR.NAME }}"
mode: 0755
- name: Rename wp-cli
shell: "mv {{ TEMP.DIRECTORY }}/{{ WORDPRESS.CLI.PHAR.NAME }} /usr/local/bin/{{ WORDPRESS.CLI.NAME }}"
- name: create Symlink
file:
src: /usr/local/bin/{{ WORDPRESS.CLI.NAME }}
dest: /usr/bin/{{ WORDPRESS.CLI.NAME }}
owner: root
group: root
state: link
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
- name: Dir exists
stat:
path=~/{{ WORDPRESS.CLI.SSH.DIRECTORY }}
register: target
- name: "Create Directory"
file:
dest=~/{{ WORDPRESS.CLI.SSH.DIRECTORY }}
state=directory
mode=755
when: not target.stat.exists
tags:
- always
- name: copy
copy:
src={{ ENV }}/wordpress/cli/config.yml
dest=~/{{ WORDPRESS.CLI.SSH.DIRECTORY }}/config.yml
tags:
- always
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
- name: Dir exists
stat:
path={{ HTTPD.DOCUMENT_ROOT_DIRECTORY }}/{{ WORDPRESS.NAME }}
register: target
- name: "Create Directory"
file:
dest={{ HTTPD.DOCUMENT_ROOT_DIRECTORY }}/{{ WORDPRESS.NAME }}
state=directory
mode=755
group=apache
owner=apache
when: not target.stat.exists
tags:
- always
- name: WordPress Core Download
become_user: apache
shell: |
wp core download \
--locale=ja
when: not target.stat.exists
args:
chdir: "{{ HTTPD.DOCUMENT_ROOT_DIRECTORY }}/{{ WORDPRESS.NAME }}"
|
1
2
3
4
5
6
7
8
9
10
|
- name: Setup Wordpress Config
become_user: apache
shell: |
wp core config \
--dbname={{ WORDPRESS.DB.NAME }} \
--dbuser={{ WORDPRESS.DB.USER }} \
--dbpass={{ WORDPRESS.DB.PASS }} \
--dbhost={{ WORDPRESS.DB.HOST }} \
--dbprefix=wp_ \
--path={{ HTTPD.DOCUMENT_ROOT_DIRECTORY }}/{{ WORDPRESS.NAME }}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
- name: Wordpress Install
become_user: apache
shell: |
wp core \
install \
--title={{ WORDPRESS.NAME }} \
--admin_user={{ WORDPRESS.ADMIN.USER.NAME }} \
--admin_password={{ WORDPRESS.ADMIN.USER.PASSWORD }} \
--admin_email={{ WORDPRESS.ADMIN.USER.MAIL_ADDRESS }} \
--url={{ WORDPRESS.URL }}/{{ WORDPRESS.NAME }} \
args:
chdir: "{{ HTTPD.DOCUMENT_ROOT_DIRECTORY }}/{{ WORDPRESS.NAME }}"
|
playbook
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
---
- hosts: local-servers
become: yes
roles:
- wordpress/cli/install
- wordpress/cli/config
- mysql/create
- mysql/user/create
- wordpress/download
- wordpress/config
- wordpress/install
- php/mysql/install
handlers:
- import_tasks: handlers/httpd/restart.yml
|
実行
1 |
ansible-playbook --private-key={秘密鍵へのパス} -u {実行ユーザー} -i inventories/local wordpress-install.yml -K
|
※実行ユーザーのパスワードの入力が求められます
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
SUDO password:
PLAY [local-servers] ***************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************
ok: [127.0.0.1]
TASK [wordpress/cli/install : get wordpress cli] ***********************************************************************************************************
changed: [127.0.0.1]
TASK [wordpress/cli/install : Rename wp-cli] ***************************************************************************************************************
changed: [127.0.0.1]
TASK [wordpress/cli/install : create Symlink] **************************************************************************************************************
changed: [127.0.0.1]
TASK [wordpress/cli/config : Dir exists] *******************************************************************************************************************
ok: [127.0.0.1]
TASK [wordpress/cli/config : Create Directory] *************************************************************************************************************
changed: [127.0.0.1]
TASK [wordpress/cli/config : copy] *************************************************************************************************************************
changed: [127.0.0.1]
TASK [mysql/create : create] *******************************************************************************************************************************
changed: [127.0.0.1]
TASK [mysql/user/update : update mysql user] ***************************************************************************************************************
changed: [127.0.0.1]
TASK [wordpress/download : Dir exists] *********************************************************************************************************************
ok: [127.0.0.1]
TASK [wordpress/download : Create Directory] ***************************************************************************************************************
changed: [127.0.0.1]
TASK [wordpress/download : WordPress Core Download] ********************************************************************************************************
changed: [127.0.0.1]
TASK [wordpress/config : Setup Wordpress Config] ***********************************************************************************************************
changed: [127.0.0.1]
TASK [wordpress/install : Wordpress Install] *********************************************************************************************************
changed: [127.0.0.1]
TASK [php/mysql/install : install] *************************************************************************************************************************
changed: [127.0.0.1]
RUNNING HANDLER [httpd restart] ****************************************************************************************************************************
changed: [127.0.0.1]
PLAY RECAP *************************************************************************************************************************************************
127.0.0.1 : ok=3 changed=15 unreachable=0 failed=0
|
確認
ブラウザで、http://localhost/sampleにアクセスして、以下の画面が表示されればインストール完了です♪