Ansible设置mysql root密码 [英] Ansible setup mysql root password
问题描述
你们中有人有一个想法可以为mysql更新root密码和授予特权建立一个yml吗?我已经创建了剧本,并在全新安装后按预期工作,完全没有问题.但是,当我再次执行无业游民的准备时,它现在无法设置root密码,并且出现错误.下面是我的代码
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
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
my.cnf.j2
[client]
user=root
password={{ dbpass }}
defaults/main.yml
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
推荐答案
似乎您尝试在下一个任务中更改密码之前,先用 new 密码更新.my.cnf
.
Seems like you update .my.cnf
with new password just before you attempt to change it with the next task.
您可能需要在更新密码时使用host_all
选项,因为with_items
会多次运行模块,并且可能会出现相同的错误:在第一项上更改密码,而在第二项上无法连接
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.
这篇关于Ansible设置mysql root密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!