Featured image of post 【Ansible備忘録】MySQLをインストールするplaybook

【Ansible備忘録】MySQLをインストールするplaybook

785文字

概要

前提

Point
  • Ansibleインストール済み
  • AWSのEC2サーバーを想定
  • 対象サーバーへの公開鍵設定済み

参考

関連記事 【脱魔境インフラ】AWSのEC2にAnsibleをインストールしてみよう 2018/02/26 2612文字 約6分で読めます IT Ansible AWS プログラミング 便利ツール 関連記事 【失われしベストプラクティスを求めて】Ansibleでの特殊な動きをするディレクトリについて 2018/03/13 1640文字 約4分で読めます IT Ansible AWS Mac セキュリティ

ディレクトリ構造

ディレクトリ構造
├── inventories
│   └── local
│       ├── group_vars
│       │   └── all.yml
│       ├── host_vars
│       │   └── 127.0.0.1.yml
│       └── hosts
├── mysql-install.yml
├── roles
│   └── mysql
│       └── aws
│           ├── init
│           │   └── tasks
│           │       └── main.yml
│           └── install
│               └── tasks
│                   └── main.yml
└── templates
    └── local
        └── mysql

ファイル群

inventories

local/hosts

inventories/local/hosts
[all-servers:children]
local-servers

[local-servers:children]
local

[local]
127.0.0.1:10000

local/group_vars

inventories/local/group_vars/all.yml
---
#------------------------------------------
# 環境
#------------------------------------------
ENV: "local"

local/host_vars

inventories/local/host_vars/127.0.0.1.yml
#------------------------------------------
# MySQL
#------------------------------------------
MYSQL:
  SERVICE:
    NAME: mysqld
  DEFAULT_FILE: /root/.my.cnf
  ROOT:
    PASSWORD: {ROOTユーザーパスワード}
  SERVER:
    YUM:
      NAME: mysql56-server
  PYTHON:
    YUM:
      NAME: MySQL-python27

roles

MySQL
install

roles/mysql/aws/install/tasks/main.yml
- name: yum install
  yum: name={{ item }}
  with_items:
    - "{{ MYSQL.SERVER.YUM.NAME }}"
    - "{{ MYSQL.PYTHON.YUM.NAME }}"

- name: start mysql
  service:
    name: "{{ MYSQL.SERVICE.NAME }}"
    state: started
    enabled: yes

init

roles/mysql/aws/init/tasks/main.yml
- name: check init
  shell: |
    mysqladmin \
    --defaults-file={{ MYSQL.DEFAULT_FILE }}  \
    ping | grep alive
  ignore_errors: true
  register: init_flag

- name: modify root password
  mysql_user:
    name: "root"
    host: "{{ item }}"
    password: "{{ MYSQL.ROOT.PASSWORD }}"
  with_items:
    - 127.0.0.1
    - ::1
    - localhost.localdomain
    - localhost
  when: init_flag.rc == 1

- name: create cnf
  template:
    src: "{{ ENV }}/mysql/.my.cnf.j2"
    dest: "{{ MYSQL.DEFAULT_FILE }}"
    owner: root
    group: root
    mode: 0600
  when: init_flag.rc == 1

templates

MySQL
templates/local/mysql/.my.cnf.j2
[client]
port=3307
user=root
password="{{ MYSQL.ROOT.PASSWORD }}"

playbook

mysql-install.yml
- hosts: all-servers
  become: yes
  roles:
    - mysql/aws/install
    - mysql/aws/init

実行

playbook実行コマンド
ansible-playbook --private-key={秘密鍵へのパス} -u {実行ユーザー} -i inventories/local mysql-install.yml -K


※実行ユーザーのパスワードの入力が求められます

実行ログ
SUDO password:

PLAY [all-servers] *****************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************
ok: [127.0.0.1]

TASK [mysql/aws/install : yum install] *********************************************************************************************************************
changed: [127.0.0.1] => (item=[u'mysql56-server', u'MySQL-python27'])

TASK [mysql/aws/install : start mysql] *********************************************************************************************************************
changed: [127.0.0.1]

TASK [mysql/aws/init : check init] *************************************************************************************************************************
fatal: [127.0.0.1]: FAILED! => {"changed": true, "cmd": "mysqladmin --defaults-file=/root/.my.cnf ping | grep alive", "delta": "0:00:00.121704", "end": "2018-04-23 07:43:59.540812", "msg": "non-zero return code", "rc": 1, "start": "2018-04-23 07:43:59.419108", "stderr": "Could not open required defaults file: /root/.my.cnf\nFatal error in defaults handling. Program aborted", "stderr_lines": ["Could not open required defaults file: /root/.my.cnf", "Fatal error in defaults handling. Program aborted"], "stdout": "", "stdout_lines": []}
...ignoring

TASK [mysql/aws/init : modify root password] ***************************************************************************************************************
changed: [127.0.0.1] => (item=127.0.0.1)
changed: [127.0.0.1] => (item=::1)
changed: [127.0.0.1] => (item=localhost.localdomain)
changed: [127.0.0.1] => (item=localhost)

TASK [mysql/aws/init : create cnf] *************************************************************************************************************************
changed: [127.0.0.1]

PLAY RECAP *************************************************************************************************************************************************
127.0.0.1                  : ok=0    changed=8    unreachable=0    failed=0

確認

正常パスワード

command
mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.6.39 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

不正パスワード

command
mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)