gpt4 book ai didi

Ansible设置mysql root密码

转载 作者:行者123 更新时间:2023-12-03 01:26:34 25 4
gpt4 key购买 nike

你们中有人有想法构建一个用于 mysql 更新 root 密码和授予权限的 yml 吗?我已经创建了我的剧本,并且在全新安装时它按预期工作,完全没有问题。但是,当我再次进行 vagrant 配置时,它现在无法设置 root 密码,并且出现错误。下面是我的代码

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 }}

默认/main.yml

---
dbhost: localhost
dbname: mydb
dbuser: root
dbpass: root

如果全新安装,我可以做所有事情,但第二次运行它会出现以下错误

enter image description here

最佳答案

似乎您在尝试通过下一个任务更改密码之前使用密码更新了.my.cnf

并且您可能需要在更新密码时使用host_all选项,因为with_items多次运行模块,并且有可能出现相同的错误:第一次更改密码项目,但无法连接第二个项目。

关于Ansible设置mysql root密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43415961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com