前提条件
開発環境(Ubuntu18.04)にansibleをインストール済み
01c. 開発環境構築(Ubuntu18.04(WSL)+Ansible+Serverspec+WP-CLI)
AWS環境でEC2インスタンス(Amazon Linux 2)とEIP(静的IPアドレス)等を準備済み
02b. AWS環境構築(CentOS6+LAMP環境+WordPress)
事前準備
# 開発環境に秘密鍵を格納
# 秘密鍵のファイル名をid_rsaに、パーミッションは600に変更
$ ll ~/.ssh/id_rsa
-rw------- 1 ec2-user ec2-user 1674 Aug 4 02:01 id_rsa
# hostsファイル更新(C:\Windows\System32\drivers\etc\hosts)
管理者としてメモ帳を開き、上記hostsファイルを変更(※上記ファイルからWSLによって自動生成されるので)
:
18.XX.XX.XX web02
※ Windowsの再起動
# 確認
$ cat/etc/hosts
:
18.XX.XX.XX web02
# ssh接続できることを確認
$ ssh -l ec2-user web02
# インベントリファイルに対象ホストを記載
$ vi /etc/ansible/hosts
[web]
web02 ansible_ssh_user=ec2-user
# 対象ホストに対してpingモジュールを実行できることを確認
$ ansible web02 -m ping -o
web02 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"},"changed": false,"ping": "pong","warnings": ["Platform linux on host web02 is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change this. See https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information."]}
LAMP+WP環境構築のPlaybook(Ansible)
# Playbookをgit clone
$ cd ~
$ git clone https://github.com/hideame/ansible_aws ansible_aws
# 構成
$ tree ansible_aws
ansible_aws
├── README.md
├── roles
│ ├── apache24
│ │ └── tasks
│ │ └── main.yml
│ ├── common
│ │ └── tasks
│ │ └── main.yml
│ ├── django
│ ├── mariadb55_WP
│ │ ├── tasks
│ │ │ └── main.yml
│ │ ├── templates
│ │ │ └── my.cnf.j2
│ │ └── vars
│ │ └── main.yml
│ ├── munin-node
│ │ ├── tasks
│ │ │ └── main.yml
│ │ ├── templates
│ │ │ ├── id_rsa.pub
│ │ │ └── status.conf
│ │ └── vars
│ │ └── main.yml
│ ├── munin-server
│ │ ├── tasks
│ │ │ └── main.yml
│ │ ├── templates
│ │ │ └── munin
│ │ └── vars
│ │ └── main.yml
│ ├── php73
│ │ └── tasks
│ │ └── main.yml
│ ├── repository
│ │ └── tasks
│ │ └── main.yml
│ ├── serverspec
│ │ └── tasks
│ │ └── main.yml
│ ├── swap
│ │ └── tasks
│ │ └── main.yml
│ ├── wordpress
│ │ ├── tasks
│ │ │ └── main.yml
│ │ └── vars
│ │ └── main.yml
│ └── yum-update
│ └── tasks
│ └── main.yml
└── site.yml
# Rolesという仕組みを使用して、役割毎に記載
$ cat ansible_aws/site.yml
- name: LAMP+WP環境構築(Amazon Linux 2 (CentOS7))
hosts: web02
become: yes # ユーザ変更を許可
roles:
- yum-update
- repository
- swap
- common
- apache24
- php73
- mariadb55_WP
- serverspec
- wordpress
- munin-server
- munin-node
# - django # 作成中
各rolesの内容確認
$ cd ansible_aws/roles
# yum-updateロール確認
$ cat yum-update/tasks/main.yml
- name: yum update
yum:
name: '*'
state: latest # パッケージを最新の状態に更新
# リポジトリロール確認
$ cat repository/tasks/main.yml
- name: EPELリポジトリを追加
yum:
name: ['https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm']
state: present
- name: Remiリポジトリを追加(PHP7用)
yum:
name: ['http://rpms.famillecollet.com/enterprise/remi-release-7.rpm']
state: present
# swapロール確認
$ cat swap/tasks/main.yml
- name: swapファイルの有無を確認
stat:
path: /swapfile
register: swap
- name: swapファイル作成
shell: "{{ item }}"
with_items:
- dd if=/dev/zero of=/swapfile bs=1M count=1024
- mkswap /swapfile
- swapon /swapfile
when: not swap.stat.exists
- name: swapファイルのパーミッション変更
file:
path: /swapfile
mode: 0600
when: not swap.stat.exists
- name: fstab修正済みかを確認
shell: grep /swapfile /etc/fstab
register: fstab_check
failed_when: fstab_check.rc not in [0, 1]
changed_when: false # 変更していないので、changedにならないように制御
- name: fstab修正
lineinfile:
dest: /etc/fstab
line: '/swapfile swap swap default 0 0'
when: fstab_check.rc == 1
- name: swappinessの設定済みなら実施しない
shell: grep 'vm.swappiness = 1' /etc/sysctl.conf
register: swappiness_check
failed_when: swappiness_check.rc not in [0, 1]
changed_when: false
- name: swappinessの設定追記(なるべくswapを使用しない)
lineinfile:
dest: /etc/sysctl.conf
line: 'vm.swappiness = 1'
when: swappiness_check.rc == 1
- name: swappinessの再読み込み
shell: sysctl -p
when: swappiness_check.rc == 1
# commonロール確認
$ cat common/tasks/main.yml
- name: コマンドインストール
yum:
name: tree
state: present
- name: タイムゾーンの変更
shell: sudo cp /usr/share/zoneinfo/Japan /etc/localtime
# ロケールは英語のままとする
# - name: ロケール設定済みかを確認
# shell: localectl status | grep LANG=ja_JP.utf8
# register: locale_check
# failed_when: locale_check.rc not in [0, 1]
# changed_when: false
# - name: ロケールの変更
# shell: localectl set-locale LANG=ja_JP.utf8
# when: locale_check.rc == 1
# apacheロール確認
$ cat apache24/tasks/main.yml
- name: apacheインストール
yum:
name: httpd
state: present
- name: httpdサービスを再起動し、自動起動を有効化
service:
name: httpd
state: restarted
enabled: yes
# PHPロール確認
$ cat php73/tasks/main.yml
- name: PHPインストール
yum:
name: ['php73-php', 'php73-php-mbstring', 'php73-php-mysqlnd', 'php73-php-pdo']
enablerepo: remi,remi-php73
state: present # パッケージがインストールされていなければインストール
# php73-php: -phpを付与しないとApacheが利用するモジュールが追加されない(/etc/httpd/modules/libphp74.so)★
# php-mbstring: マルチバイト文字列拡張モジュール
# php-mysqlnd: MariaDB用ドライバ
# php-pdo: PHP Data Objects
- name: シンボリックリンク作成
file:
src: /opt/remi/php73/root/usr/bin/php
dest: /usr/bin/php
state: link
# MariaDBロール確認
$ cat mariadb55_WP/tasks/main.yml
- name: MariaDBインストール
yum:
name: ['mariadb-5.5.64', 'mariadb-server-5.5.64', 'MySQL-python-1.2.5']
state: present
#- name: httpプロトコル開放 # AWSではセキュリティグループで管理
# firewalld:
# service: http
# state: enabled
# permanent: true
- name: 各種サービスを再起動し、自動起動を有効化
service:
name: "{{ item }}"
state: restarted
enabled: yes
with_items:
- httpd
- mariadb
#- firewalld
- name: ~/.my.cnfを配置(MySQL設定ファイル)
template:
src: my.cnf.j2
dest: ~/.my.cnf
mode: 0400
force: no
- name: MySQL rootユーザのパスワードを設定
mysql_user:
name: root
password: "{{ db_rootpass }}"
check_implicit_admin: yes # ノンパスでrootログインを試みる
no_log: true # デバッグモード時にパスワードを表示しない
- name: MySQL DB作成
mysql_db:
login_user: root
login_password: "{{ db_rootpass }}"
name: "{{ db_name }}"
state: present # 存在しない場合のみ作成
- name: MySQLユーザの作成及びDBへの権限付与
mysql_user:
login_user: root
login_password: "{{ db_rootpass }}"
name: "{{ db_user }}"
password: "{{ db_pass }}"
priv: "{{ db_name }}.*:ALL"
state: present
no_log: true
# templateファイル確認
$ vi mariadb55_WP/templates/my.cnf.j2
[client]
user=root
password="{{ db_rootpass }}"
# 変数ファイル作成
$ vi mariadb55_WP/vars/main.yml
db_rootpass: XXXXXX
db_name: XXXXXX
db_user: XXXXXX
db_pass: XXXXXX
# Serverspecロール確認
$ cat serverspec/tasks/main.yml
- name: amazon-linux-extras有効化(ruby2.6)済みなら実施しない
shell: amazon-linux-extras | grep ruby2.6 | grep enabled
register: ruby_status_check
failed_when: ruby_status_check.rc not in [0, 1]
changed_when: false
- name: amazon-linux-extras有効化(ruby2.6)
shell: amazon-linux-extras enable ruby2.6
when: ruby_status_check.rc == 1
- name: Rubyインストール
yum:
name: ['ruby', 'ruby-devel', 'gcc-c++']
#enablerepo: amzn2extra-ruby2.6
# gcc-c++: io-consoleに必要なパッケージ
state: present
- name: Ruby Gems インストール
gem:
name: "{{ item }}"
user_install: no # システム領域にインストール
state: present
with_items:
- bundler
- rake # Serverspecに必要なライブラリ
- io-console # Serverspecに必要なライブラリ
- serverspec # サーバ構成テストツール
# WordPressロール確認
$ cat wordpress/tasks/main.yml
- name: WP-CLIコマンドのダウンロード
get_url:
url: https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
dest: /usr/local/bin/wp
owner: apache
group: apache
mode: 0755
- name: WordPressがダウンロード済みなら実施しない
stat:
path: "{{ wp_path }}"
register: wordpress
- name: WordPressのダウンロード
shell: /usr/local/bin/wp core download --locale=ja --path="{{ wp_path }}"
when: not wordpress.stat.exists
- name: WordPressの所有者をapacheに変更
file:
path: "{{ wp_path }}"
owner: apache
group: apache
state: directory
recurse: yes
- name: /usr/share/httpd/.ansibleディレクトリ作成
file:
path: /usr/share/httpd/.ansible
mode: 0755
state: directory
- name: /usr/share/httpd/.ansible/tmpディレクトリ作成
file:
path: /usr/share/httpd/.ansible/tmp
owner: apache
group: apache
mode: 0700
state: directory
- name: configファイル設定済みなら実施しない
stat:
path: "{{ wp_path }}/wp-config.php"
register: wpconfig
- name: configファイル設定(DB設定情報)
become_user: apache
shell: /usr/local/bin/wp core config --dbhost=localhost --dbname="{{ db_name }}" --dbuser="{{ db_user }}" --dbpass="{{ db_pass }}" --path="{{ wp_path }}"
when: not wpconfig.stat.exists
- name: WordPressのインストール
become_user: apache
shell: /usr/local/bin/wp core install --url=http://"{{ hostname }}"/ --title="{{ title }}" --admin_user="{{ wp_user }}" --admin_password="{{ wp_pass }}" --admin_email="{{ email }}" --path="{{ wp_path }}"
when: not wpconfig.stat.exists
- name: DocumentRoo等の変更
replace:
dest: /etc/httpd/conf/httpd.conf
regexp: /var/www/html
replace: "{{ wp_path }}"
- name: httpdサービスの再起動
service:
name: httpd
state: restarted
- name: wp-config.phpが変更済みかを確認
shell: grep "^define( 'DB_HOST', 'localhost' );" "{{ wp_path }}/wp-config.php"
register: wpconfig_check
failed_when: wpconfig_check.rc not in [0, 1]
changed_when: false # 変更していないので、changedにならないように制御
- name: wp-config.phpの変更(localhostから実際のIPアドレスに)
lineinfile:
dest: "{{ wp_path }}/wp-config.php"
regexp: "^define\\( 'DB_HOST', 'localhost' \\);"
line: "define( 'DB_HOST', '{{ hostname }}' );"
backrefs: yes # マッチしなかった場合もEOFに追記しない
when: wpconfig_check.rc == 0
- name: 実際のIPアドレスへのアクセスを許可
shell: mysql -uroot -p"{{ db_rootpass }}" -e"grant all privileges on {{ db_name }}.* to {{ db_user }}@{{ hostname }} with grant option;"
when: wpconfig_check.rc == 0
- name: 設定を反映
shell: mysql -uroot -p"{{ db_rootpass }}" -e'flush privileges;'
when: wpconfig_check.rc == 0
# - name: テーマがインストール済みなら実施しない(habakiri)
# stat:
# path: "{{ wp_path }}/wp-content/themes/habakiri"
# register: habakiri
# - name: テーマのインストール(habakiri)
# become_user: apache
# shell: /usr/local/bin/wp theme install habakiri --path="{{ wp_path }}"
# when: not habakiri.stat.exists
- name: 親テーマのダウンロード(cocoon)
get_url:
url: https://wp-cocoon.com/download/791/
dest: /tmp/cocoon-master-2.1.3.6.zip
- name: 親テーマが展開済みなら実施しない(cocoon)
stat:
path: "{{ wp_path }}/wp-content/themes/cocoon-master"
register: cocoon
- name: 親テーマの展開(cocoon)
unarchive:
src: /tmp/cocoon-master-2.1.3.6.zip
dest: "{{ wp_path }}/wp-content/themes/"
remote_src: yes # リモートにダウンロードデータがある場合はyes
when: not cocoon.stat.exists
- name: 子テーマのダウンロード(cocoon-child)
get_url:
url: https://wp-cocoon.com/download/726/
dest: /tmp/cocoon-child-master-1.1.1.zip
- name: 子テーマが展開済みなら実施しない(cocoon-child)
stat:
path: "{{ wp_path }}/wp-content/themes/cocoon-child-master"
register: cocoonChild
- name: 子テーマの展開(cocoon-child)
unarchive:
src: /tmp/cocoon-child-master-1.1.1.zip
dest: "{{wp_path}}/wp-content/themes/"
remote_src: yes # リモートにダウンロードデータがある場合はyes
when: not cocoonChild.stat.exists
- name: テーマ配下の所有権をapacheに変更
file:
path: "{{ wp_path }}/wp-content/themes"
owner: apache
group: apache
state: directory
recurse: yes
- name: テーマの有効化(cocoon-child)
become_user: apache
shell: /usr/local/bin/wp theme activate cocoon-child-master --path="{{ wp_path }}"
when: not cocoonChild.stat.exists
- name: プラグインのインストール
become_user: apache
shell: /usr/local/bin/wp plugin install "{{ item }}" --path="{{ wp_path }}" --activate
with_items:
- backwpup # WordPressのファイルとデータベースのバックアップを取得
- broken-link-checker # リンク切れを自動でチェック
- contact-form-7 # お問い合わせフォームプラグイン(メール機能は下記Gmail SMTPで実装
- flickr-pick-a-picture # 著作権リンク付きの写真を挿入
- gmail-smtp # GmailのSMTPサーバー経由でメールを送信
- google-sitemap-generator # サイトマップをGoogleに送信してインデックスへの登録漏れを防ぐ
- ml-slider # スライドショーを生成
- pixabay-images # パブリックドメイン(知的財産権のない)画像を簡単に挿入
- polylang # WordPress に多言語機能を追加
- siteorigin-panels # 固定ページにもサイドバーウィジェットを作成
- syntaxhighlighter # ソースコードを綺麗に表示
- wordpress-importer # WordPressエクスポートファイルからインポートするツール
- wp-multibyte-patch # 日本語版WordPressのマルチバイト文字の取り扱いに関する不具合の修正
# - siteguard # セキュリティ対策
# 変数ファイル作成
$ vi wordpress/vars/main.yml
hostname: XX.XX.XX.XX
wp_path: /var/www/wordpress
db_rootpass: XXXXXX
db_name: XXXXXX
db_user: XXXXXX
db_pass: XXXXXX
wp_user: XXXXXX
wp_pass: XXXXXX
title: XXXXXX
email: XXXX@example.com
# munin-serverロール
$ cat munin-server/tasks/main.yml
- name: Mininインストール
yum:
name: munin
state: present
- name: SSH鍵作成
user:
name: munin
generate_ssh_key: yes
# - name: 手動でグラフの作成(初回だけでOK)
# become_user: munin
# shell: /usr/bin/munin-cron
- name: cron設定実施(※通常は自動作成される)
template:
src: munin
dest: /etc/cron.d/munin
force: no
- name: ユーザー認証設定(手動で実施要★)
debug:
msg: htpasswd -c /etc/munin/munin-htpasswd munin
- name: Muninディレクトリ移動済みかを確認
stat:
path: /var/www/html/munin
register: munin_dir_check
- name: Muninディレクトリ移動
shell: mv /var/www/html/munin /var/www/wordpress/
when: munin_dir_check.stat.exists == true
- name: 設定ファイルのバックアップ
copy:
src: /etc/munin/munin.conf
dest: /etc/munin/munin.conf.org
remote_src: yes
force: no
- name: 設定ファイルの編集(localhost)
lineinfile:
dest: /etc/munin/munin.conf
regexp: '{{ item.regexp }}'
line: '{{ item.line }}'
backrefs: yes # マッチしなかった場合もEOFに追記しない
with_items:
- regexp: '#htmldir /var/www/html/munin'
line: 'htmldir /var/www/wordpress/munin'
- regexp: '# \[localhost\]'
line: '[localhost]'
- regexp: '^# address 127\.0\.0\.1'
line: ' address 127.0.0.1'
- regexp: '^# use_node_name yes'
line: ' use_node_name yes'
- name: メール設定済みなら実施しない
shell: grep '# メール設定' /etc/munin/munin.conf
register: mail_check
failed_when: mail_check.rc not in [0, 1]
changed_when: false
- name: メール設定
blockinfile:
dest: /etc/munin/munin.conf
insertbefore: '#contact.anotheruser.command' # この文字列の前に追加
marker: '# メール設定' # 追加部分を示すマーカー
block: |-
contact.mail.command mail -s "Munin ${var:worst}::${var:group}::${var:host}::${var:plugin}" "{{ email }}"
contact.mail.always_send warning critical
contact.log.command >> /var/log/munin/alert.log
when: mail_check.rc == 1
- name: 監視設定済みなら実施しない
shell: grep '# 監視設定' /etc/munin/munin.conf
register: disk_check
failed_when: disk_check.rc not in [0, 1]
changed_when: false
- name: 監視設定
blockinfile:
dest: /etc/munin/munin.conf
insertafter: '\[localhost\]' # この文字列の後に追加
marker: '# 監視設定' # 追加部分を示すマーカー
block: |2
df._dev_xvda1.warning 60
df._dev_xvda1.critical 80
cpu.user.warning 10 # cpuのuserパラメータが10%を超えたらwarning
memory.free.warning 50000000: # メモリの空き容量が50MB以下になったらwarning
when: disk_check.rc == 1
- name: httpdをリロード
service:
name: httpd
state: reloaded
# templateファイル確認
$ cat munin-server/templates/munin
#
# cron-jobs for munin
#
MAILTO=root
*/5 * * * * munin test -x /usr/bin/munin-cron && /usr/bin/munin-cron
# 変数ファイル作成
$ vi munin-server/vars/main.yml
email: XXXX@example.com
# munin-nodeロールの確認
$ cat munin-node/tasks/main.yml
#- name: muninポート開放 # AWSではセキュリティグループで管理
# firewalld:
# port: 4949/tcp
# state: enabled
# permanent: true
- name: Minin-nodeインストール
yum:
name: munin-node
state: present
- name: 公開鍵の配置
become_user: munin
template:
src: id_rsa.pub
dest: ~/.ssh/authorized_keys
mode: 0600
- name: 設定ファイルを編集(ホスト名設定)
replace:
dest: /etc/munin/munin-node.conf
regexp: '^#host_name localhost.localdomain'
replace: "host_name {{ hostname }}"
- name: 設定ファイルを編集済みなら実施しない(MuninサーバIP設定)
shell: grep '# muninサーバのIPを追記' /etc/munin/munin-node.conf
register: munin_check
failed_when: munin_check.rc not in [0, 1]
changed_when: false
- name: 設定ファイルを編集(MuninサーバIP設定)
blockinfile:
dest: /etc/munin/munin-node.conf
insertbefore: '^allow \^127' # この文字列の前に追加
marker: '# muninサーバのIPを追記' # 追加部分を示すマーカー
block: allow ^"{{ munin_server_RegExp }}"$
when: munin_check.rc == 1
- name: 監視設定済みなら実施しない(apache)
shell: grep '^\[apache' /etc/munin/plugin-conf.d/munin-node
register: apache_check
failed_when: apache_check.rc not in [0, 1]
changed_when: false
- name: 監視設定(apache)
blockinfile:
dest: /etc/munin/plugin-conf.d/munin-node
insertafter: EOF # ファイルの末尾に追加
marker: "" # 追加部分を示すマーカー(空行)
block: |
[apache*]
env.ports 80 443
when: apache_check.rc == 1
- name: 拡張情報の取得をON(apache)
template:
src: status.conf
dest: /etc/httpd/conf.d/status.conf
force: no
# - name: 監視設定済みなら実施しない(mysql)
# shell: grep '^\[mysql' /etc/munin/plugin-conf.d/munin-node
# register: mysql_check
# failed_when: mysql_check.rc not in [0, 1]
# changed_when: false
# - name: 監視設定(mysql)
# blockinfile:
# dest: /etc/munin/plugin-conf.d/munin-node
# insertafter: EOF # ファイルの末尾に追加
# marker: "" # 追加部分を示すマーカー(空行)
# block: |
# [mysql*]
# env.mysqlconnection DBI:mysql:mysql;host=127.0.0.1;port=3306
# env.mysqlopts -u "{{ db_user }}" --password="{{ db_pass }}"
# when: mysql_check.rc == 1
- name: Apacheの監視
file:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
state: link
with_items:
- src: /usr/share/munin/plugins/apache_accesses
dest: /etc/munin/plugins/apache_accesses
- src: /usr/share/munin/plugins/apache_processes
dest: /etc/munin/plugins/apache_processes
- src: /usr/share/munin/plugins/apache_volume
dest: /etc/munin/plugins/apache_volume
- name: MySQLの監視
file:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
state: link
with_items:
- src: /usr/share/munin/plugins/mysql_queries
dest: /etc/munin/plugins/mysql_queries
# - src: /usr/share/munin/plugins/mysql_bytes
# dest: /etc/munin/plugins/mysql_bytes
# - src: /usr/share/munin/plugins/mysql_slowqueries
# dest: /etc/munin/plugins/mysql_slowqueries
# - src: /usr/share/munin/plugins/mysql_threads
# dest: /etc/munin/plugins/mysql_threads
# - name: SlowQuery監視設定済みなら実施しない(mysql)
# shell: grep '# SlowQuery監視' /etc/my.cnf.d/server.cnf
# register: SlowQuery_check
# failed_when: SlowQuery_check.rc not in [0, 1]
# changed_when: false
# - name: SlowQuery監視(mysql)
# blockinfile:
# dest: /etc/my.cnf.d/server.cnf
# insertafter: '\[mariadb\]' # この文字列の後に追加
# marker: '# SlowQuery監視' # 追加部分を示すマーカー(空行)
# block: |
# slow_query_log=1
# slow_query_log_file=/var/log/mysql-slow.log
# long_query_time=1
# when: SlowQuery_check.rc == 1
# - name: mysql-slow.logファイルのパーミッション変更
# file:
# path: /var/log/mysql-slow.log
# owner: mysql
# group: mysql
# state: touch
- name: munin-nodeを起動し、自動起動を有効化
service:
name: munin-node
state: started
enabled: yes
- name: 各種サービスの再起動
service:
name: "{{ item }}"
state: restarted
with_items:
- httpd
- mariadb
# templateファイル作成
$ vi munin-node/templates/id_rsa.pub
※ munin-serverの公開鍵情報を記載する
# templateファイル確認
$ cat munin-node/templates/status.conf
ExtendedStatus On
<Location /server-status>
SetHandler server-status
Require ip 127.0.0.1
</Location>
# 変数ファイル作成
$ cat munin-node/vars/main.yml
munin_server_RegExp: XX\.XX\.XX\.XX
hostname: XX.XX.XX.XX
db_user: XXXXXX
db_pass: XXXXXX
モジュールの使い方については、Ansible公式サイトに詳しく記載されています。
Module Index — Ansible Documentation
Playbookの実行
$ cd ansible_aws
# 構文チェック
$ ansible-playbook site.yml --syntax-check
playbook: site.yml # エラーが表示されないことを確認
# タスクの一覧を確認
$ ansible-playbook site.yml --list-task
playbook: site.yml
play #1 (web02): LAMP+WP環境構築(Amazon Linux 2 (CentOS7)) TAGS: []
tasks:
yum-update : yum update TAGS: []
repository : EPELリポジトリを追加 TAGS: []
repository : Remiリポジトリを追加(PHP7用) TAGS: []
swapファイルの有無を確認 TAGS: []
swapファイル作成 TAGS: []
swapファイルのパーミッション変更 TAGS: []
swap : fstab修正済みかを確認 TAGS: []
swap : fstab修正 TAGS: []
swappinessの設定済みなら実施しない TAGS: []
swappinessの設定追記(なるべくswapを使用しない) TAGS: []
swappinessの再読み込み TAGS: []
:
# Playbookの実行
$ ansible-playbook site.yml
PLAY [LAMP+WP環境構築(Amazon Linux 2 (CentOS7))] **************************************************************************************
TASK [Gathering Facts] ************************************************************************************************************
[WARNING]: Platform linux on host web02 is using the discovered Python interpreter at /usr/bin/python, but future installation of
another Python interpreter could change this. See
![]()
Interpreter Discovery — Ansible Documentation for more information.
ok: [web02]
TASK [yum-update : yum update] ****************************************************************************************************
ok: [web02]
TASK [repository : EPELリポジトリを追加] **************************************************************************************************
ok: [web02]
TASK [repository : Remiリポジトリを追加(PHP7用)] *******************************************************************************************
ok: [web02]
TASK [swapファイルの有無を確認] *************************************************************************************************************
ok: [web02]
TASK [swapファイル作成] *****************************************************************************************************************
skipping: [web02] => (item=dd if=/dev/zero of=/swapfile bs=1M count=1024)
skipping: [web02] => (item=mkswap /swapfile)
skipping: [web02] => (item=swapon /swapfile)
TASK [swapファイルのパーミッション変更] *********************************************************************************************************
skipping: [web02]
TASK [swap : fstab修正済みかを確認] *******************************************************************************************************
ok: [web02]
TASK [swap : fstab修正] *************************************************************************************************************
skipping: [web02]
TASK [swappinessの設定済みなら実施しない] *****************************************************************************************************
ok: [web02]
TASK [swappinessの設定追記(なるべくswapを使用しない)] ********************************************************************************************
skipping: [web02]
TASK [swappinessの再読み込み] ***********************************************************************************************************
skipping: [web02]
TASK [common : コマンドインストール] ********************************************************************************************************
ok: [web02]
TASK [common : タイムゾーンの変更] *********************************************************************************************************
[WARNING]: Consider using 'become', 'become_method', and 'become_user' rather than running sudo
changed: [web02]
TASK [apache24 : apacheインストール] ****************************************************************************************************
ok: [web02]
TASK [apache24 : httpdサービスを再起動し、自動起動を有効化] *****************************************************************************************
changed: [web02]
TASK [php73 : PHPインストール] **********************************************************************************************************
ok: [web02]
TASK [php73 : シンボリックリンク作成] ********************************************************************************************************
ok: [web02]
TASK [mariadb55_WP : MariaDBインストール] ***********************************************************************************************
ok: [web02]
TASK [mariadb55_WP : 各種サービスを再起動し、自動起動を有効化] ****************************************************************************************
changed: [web02] => (item=httpd)
changed: [web02] => (item=mariadb)
TASK [mariadb55_WP : ~/.my.cnfを配置(MySQL設定ファイル)] ***********************************************************************************
ok: [web02]
TASK [mariadb55_WP : MySQL rootユーザのパスワードを設定] **************************************************************************************
ok: [web02]
TASK [mariadb55_WP : MySQL DB作成] **************************************************************************************************
ok: [web02]
TASK [mariadb55_WP : MySQLユーザの作成及びDBへの権限付与] ***************************************************************************************
ok: [web02]
TASK [serverspec : amazon-linux-extras有効化(ruby2.6)済みなら実施しない] **********************************************************************
ok: [web02]
TASK [serverspec : amazon-linux-extras有効化(ruby2.6)] *******************************************************************************
skipping: [web02]
TASK [serverspec : Rubyインストール] ****************************************************************************************************
ok: [web02]
TASK [serverspec : Ruby Gems インストール] **********************************************************************************************
ok: [web02] => (item=bundler)
ok: [web02] => (item=rake)
ok: [web02] => (item=io-console)
ok: [web02] => (item=serverspec)
TASK [wordpress : WP-CLIコマンドのダウンロード] **********************************************************************************************
ok: [web02]
TASK [wordpress : WordPressがダウンロード済みなら実施しない] **************************************************************************************
ok: [web02]
TASK [wordpress : WordPressのダウンロード] ***********************************************************************************************
skipping: [web02]
TASK [wordpress : WordPressの所有者をapacheに変更] ****************************************************************************************
ok: [web02]
TASK [wordpress : /usr/share/httpd/.ansibleディレクトリ作成] ******************************************************************************
ok: [web02]
TASK [wordpress : /usr/share/httpd/.ansible/tmpディレクトリ作成] **************************************************************************
ok: [web02]
TASK [wordpress : configファイル設定済みなら実施しない] ******************************************************************************************
ok: [web02]
TASK [wordpress : configファイル設定(DB設定情報)] *******************************************************************************************
skipping: [web02]
TASK [wordpress : WordPressのインストール] ***********************************************************************************************
skipping: [web02]
TASK [wordpress : DocumentRoo等の変更] ************************************************************************************************
ok: [web02]
TASK [wordpress : httpdサービスの再起動] **************************************************************************************************
changed: [web02]
TASK [wordpress : wp-config.phpが変更済みかを確認] *****************************************************************************************
ok: [web02]
TASK [wordpress : wp-config.phpの変更(localhostから実際のIPアドレスに)] ************************************************************************
skipping: [web02]
TASK [wordpress : 実際のIPアドレスへのアクセスを許可] *********************************************************************************************
skipping: [web02]
TASK [wordpress : 設定を反映] **********************************************************************************************************
skipping: [web02]
TASK [wordpress : 親テーマのダウンロード(cocoon)] ********************************************************************************************
ok: [web02]
TASK [wordpress : 親テーマが展開済みなら実施しない(cocoon)] ***************************************************************************************
ok: [web02]
TASK [wordpress : 親テーマの展開(cocoon)] ************************************************************************************************
skipping: [web02]
TASK [wordpress : 子テーマのダウンロード(cocoon-child)] **************************************************************************************
ok: [web02]
TASK [wordpress : 子テーマが展開済みなら実施しない(cocoon-child)] *********************************************************************************
ok: [web02]
TASK [wordpress : 子テーマの展開(cocoon-child)] ******************************************************************************************
skipping: [web02]
TASK [wordpress : テーマ配下の所有権をapacheに変更] ********************************************************************************************
ok: [web02]
TASK [wordpress : テーマの有効化(cocoon-child)] ******************************************************************************************
skipping: [web02]
TASK [wordpress : プラグインのインストール] ***************************************************************************************************
changed: [web02] => (item=backwpup)
changed: [web02] => (item=broken-link-checker)
changed: [web02] => (item=contact-form-7)
changed: [web02] => (item=flickr-pick-a-picture)
changed: [web02] => (item=gmail-smtp)
changed: [web02] => (item=google-sitemap-generator)
changed: [web02] => (item=ml-slider)
changed: [web02] => (item=pixabay-images)
changed: [web02] => (item=polylang)
changed: [web02] => (item=siteorigin-panels)
changed: [web02] => (item=syntaxhighlighter)
changed: [web02] => (item=wordpress-importer)
changed: [web02] => (item=wp-multibyte-patch)
TASK [munin-server : Mininインストール] *************************************************************************************************
ok: [web02]
TASK [munin-server : SSH鍵作成] ******************************************************************************************************
ok: [web02]
TASK [munin-server : cron設定実施(※通常は自動作成される)] ***************************************************************************************
ok: [web02]
TASK [munin-server : ユーザー認証設定(手動で実施要★)] *******************************************************************************************
ok: [web02] => {
"msg": "htpasswd -m /etc/munin/munin-htpasswd munin"
}
TASK [munin-server : Muninディレクトリ移動済みかを確認] *****************************************************************************************
ok: [web02]
TASK [munin-server : Muninディレクトリ移動] ***********************************************************************************************
skipping: [web02]
TASK [munin-server : Muninディレクトリオーナー変更] *******************************************************************************************
changed: [web02]
TASK [munin-server : 設定ファイルのバックアップ] ***********************************************************************************************
ok: [web02]
TASK [munin-server : 設定ファイルの編集(localhost)] ****************************************************************************************
ok: [web02] => (item={'regexp': '#htmldir /var/www/html/munin', 'line': 'htmldir /var/www/wordpress/munin'})
ok: [web02] => (item={'regexp': '# \\[localhost\\]', 'line': '[localhost]'})
ok: [web02] => (item={'regexp': '^# address 127\\.0\\.0\\.1', 'line': ' address 127.0.0.1'})
ok: [web02] => (item={'regexp': '^# use_node_name yes', 'line': ' use_node_name yes'})
TASK [munin-server : メール設定済みなら実施しない] **********************************************************************************************
ok: [web02]
TASK [munin-server : メール設定] *******************************************************************************************************
skipping: [web02]
TASK [munin-server : 監視設定済みなら実施しない] ***********************************************************************************************
ok: [web02]
TASK [munin-server : 監視設定] ********************************************************************************************************
skipping: [web02]
TASK [munin-server : httpdをリロード] **************************************************************************************************
changed: [web02]
TASK [munin-node : Minin-nodeインストール] **********************************************************************************************
ok: [web02]
TASK [munin-node : 公開鍵の配置] ********************************************************************************************************
ok: [web02]
TASK [munin-node : 設定ファイルを編集(ホスト名設定)] *********************************************************************************************
ok: [web02]
TASK [munin-node : 設定ファイルを編集済みなら実施しない(MuninサーバIP設定)] ******************************************************************************
ok: [web02]
TASK [munin-node : 設定ファイルを編集(MuninサーバIP設定)] ***************************************************************************************
skipping: [web02]
TASK [munin-node : 監視設定済みなら実施しない(apache)] *****************************************************************************************
ok: [web02]
TASK [munin-node : 監視設定(apache)] **************************************************************************************************
skipping: [web02]
TASK [munin-node : 拡張情報の取得をON(apache)] ********************************************************************************************
ok: [web02]
TASK [munin-node : Apacheの監視] *****************************************************************************************************
ok: [web02] => (item={'src': '/usr/share/munin/plugins/apache_accesses', 'dest': '/etc/munin/plugins/apache_accesses'})
ok: [web02] => (item={'src': '/usr/share/munin/plugins/apache_processes', 'dest': '/etc/munin/plugins/apache_processes'})
ok: [web02] => (item={'src': '/usr/share/munin/plugins/apache_volume', 'dest': '/etc/munin/plugins/apache_volume'})
TASK [munin-node : MySQLの監視] ******************************************************************************************************
ok: [web02] => (item={'src': '/usr/share/munin/plugins/mysql_queries', 'dest': '/etc/munin/plugins/mysql_queries'})
TASK [munin-nodeを起動し、自動起動を有効化] ****************************************************************************************************
ok: [web02]
TASK [munin-node : 各種サービスの再起動] ****************************************************************************************************
changed: [web02] => (item=httpd)
changed: [web02] => (item=mariadb)
PLAY RECAP ************************************************************************************************************************
web02 : ok=58 changed=8 unreachable=0 failed=0 skipped=20 rescued=0 ignored=0
上記Playbookの実行は2回目なので、ほとんどがokやskippedになっています。
muninについては、以下にわかりやすい説明が記載されています。
https://wiki.archlinux.jp/index.php/Munin
# Basic認証の設定 $ sudo htpasswd -c /etc/munin/munin-htpasswd munin New password: Re-type new password: Adding password for user munin # httpdの再起動 $ sudo systemctl restart httpd
サーバ構成のテスト(serverspec)
作成中
