ねこじゃすり
猫を魅了する魔法の装備品!
Anker PowerCor
旅行には必須の大容量モバイルバッテリー!
【最新機種】GoPro hero11 Black
最新機種でVlogの思い出を撮影しよう!
[ノースフェイス] THE NORTH FACE メンズ アウター マウンテンライトジャケット
防水暴風で耐久性抜群なので旅行で大活躍です!
モンスターエナジー 355ml×24本 [エナジードリンク]
脳を活性化させるにはこれ!
MOFT X 【新型 ミニマム版】 iPhone対応 スマホスタンド
Amazon一番人気のスマホスタンド!カード類も収納出来てかさ張らないのでオススメです!
サンディスク microSD 128GB
スマホからSwitchまで使える大容量MicroSDカード!
スポンサーリンク
目次
概要
前提
- Ansibleインストール済み
- AWSのEC2サーバーを想定
- 対象サーバーへの公開鍵設定済み
参考
ディレクトリ構造
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
├── 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
1 2 3 4 5 6 7 8 |
[all-servers:children] local-servers [local-servers:children] local [local] 127.0.0.1:10000 |
local/group_vars
1 2 3 4 5 |
--- #------------------------------------------ # 環境 #------------------------------------------ ENV: "local" |
local/host_vars
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#------------------------------------------ # 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
1 2 3 4 5 6 7 8 9 10 11 |
- 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
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 |
- 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
1 2 3 4 |
[client] port=3307 user=root password="{{ MYSQL.ROOT.PASSWORD }}" |
playbook
1 2 3 4 5 |
- hosts: all-servers become: yes roles: - mysql/aws/install - mysql/aws/init |
実行
1 |
ansible-playbook --private-key={秘密鍵へのパス} -u {実行ユーザー} -i inventories/local mysql-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 |
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 |
確認
正常パスワード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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> |
不正パスワード
1 2 3 |
mysql -u root -p Enter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) |