このようなタスクにする。
- name: yum install mysql
yum: name={{ item }} state=latest
with_items:
- mysql.x86_64
- mysql-devel.x86_64
- mysql-embedded.x86_64
- mysql-server.x86_64
- MySQL-python
- name: add /etc/my.cnf
template: src=my.cnf.j2 dest=/etc/my.cnf force=no
notify: restart mysqld
- name: chkconfig mysqld on
service: name=mysqld enabled=yes state=started
- name: set password for root
mysql_user:
name: root
host: "{{ item }}"
password: "{{ mysqld.root_password }}"
with_items:
- 127.0.0.1
- localhost
- name: root ユーザ接続設定ファイル作成
template: src=.my.cnf.j2 dest=/etc/.my.cnf force=no
CentOS6でMySQL5.1の環境なので、デフォルトのmy.cnfで起動してしまうとlatin1なデータベースが出来てしまう。 なので、以下の順番で記述している。
- yumでmysqld関連パッケージをインストール
- InnoDBで各種charsetをUTF-8化した/etc/my.cnfを配置する
- mysqldをchkconfigに登録して、なおかつサービスを開始する
- rootユーザのパスワードを設定する
- ansibleのmysql_userが認証に使う/root/.my.cnfを配置する
4.と5.がくっそ重要で、一度5.を設定せずplaybook実行してから再度試したら以下のようにアクセスできない、とエラーが出た。
TASK [db : set password for root] **********************************************
failed: [ansible2] (item=127.0.0.1) => {"failed": true, "item": "127.0.0.1", "msg": "unable to connect to database, check login_user and login_password are correct or /root/.my.cnf has the credentials. Exception message: (1045, \"Access denied for user 'root'@'localhost' (using password: NO)\")"}
failed: [ansible2] (item=localhost) => {"failed": true, "item": "localhost", "msg": "unable to connect to database, check login_user and login_password are correct or /root/.my.cnf has the credentials. Exception message: (1045, \"Access denied for user 'root'@'localhost' (using password: NO)\")"}
to retry, use: --limit @site.retry
結局必ずタスクが爆発するようになったので、サーバ上に手動で/root/.my.cnfを作成して回避したあとに、playbookを流して爆発しないのを確認するとかいうめっちゃ良くない解決方法をとりました。 失敗しなきゃええんやな、って感じ。