ansible mysql root 密码初始化_Ansible设置MySQL root密码

bd96500e110b49cbb3cd949968f18be7.png

anyone of you have an idea to build a yml for mysql updating of root password and granting privileges? I have created my playbook and on the fresh install its working as expected and no issue at all. But when I do vagrant provision again it now fails to set the root password and I'm getting an error. Below are my codes

mysql.yml

---

- name: Install the MySQL packages

apt: name={{ item }} state=installed update_cache=yes

with_items:

- mysql-server

- mysql-client

- python-mysqldb

- libmysqlclient-dev

- name: drop database {{ dbname }}

mysql_db:

name: "{{ dbname }}"

login_user: "{{ dbuser }}"

login_password: "{{ dbpass }}"

state: absent

delegate_to: "{{ dbhost }}"

run_once: true

- name: create database {{ dbname }}

mysql_db:

name: "{{ dbname }}"

login_user: "{{ dbuser }}"

login_password: "{{ dbpass }}"

state: present

delegate_to: "{{ dbhost }}"

run_once: true

- name: ensure mysql is running and starts on boot

service: name=mysql state=started enabled=true

- name: copy .my.cnf file with root password credentials

template: src=my.cnf.j2 dest=/root/.my.cnf owner=root mode=0600

- name: update mysql root password for all root accounts "{{ dbpass }}"

mysql_user: name={{ dbuser }} host={{ item }} password="{{ dbpass }}" priv="{{ dbname }}.*:ALL,GRANT"

with_items:

- localhost

- 127.0.0.1

- name: grant privilege on "{{ dbname }}" to "{{ dbuser }}"

mysql_user:

name: "{{ item.user }}"

host: "{{ item.host }}"

password: "{{ dbpass }}"

login_user: "{{ dbuser }}"

login_password: "{{ dbpass }}"

priv: "{{ dbname }}.*:ALL"

state: present

with_items:

- { user: "{{ dbuser }}" , host: localhost }

- { user: "{{ dbuser }}" , host: 127.0.0.1 }

delegate_to: "{{ dbhost }}"

run_once: true

- name: ensure anonymous users are not in the database

mysql_user: name='' host={{ item }} state=absent

with_items:

- 127.0.0.1

- localhost

- name: remove the test database

mysql_db: name=test state=absent

my.cnf.j2

[client]

user=root

password={{ dbpass }}

defaults/main.yml

---

dbhost: localhost

dbname: mydb

dbuser: root

dbpass: root

I'm able to do everything just fine if its fresh install but running it the second time around gives me the error below

3TyyP.png

解决方案

Seems like you update .my.cnf with new password just before you attempt to change it with the next task.

And you may want to use host_all option when updating password, because with_items run module several times, and there is a possibility of same error: change password on the first item and can't connect on the second item.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值