Ajo*_*uve 29 mysql ubuntu vagrant ansible
我在使用ansible在流氓ubuntu上安装MySQL时遇到问题,
这是我的MySQL部分
---
- name: Install MySQL
apt:
name: "{{ item }}"
with_items:
- python-mysqldb
- mysql-server
- name: copy .my.cnf file with root password credentials
template:
src: templates/root/.my.cnf
dest: ~/.my.cnf
owner: root
mode: 0600
- name: Start the MySQL service
service:
name: mysql
state: started
enabled: true
# 'localhost' needs to be the last item for idempotency, see
# http://ansible.cc/docs/modules.html#mysql-user
- name: update mysql root password for all root accounts
mysql_user:
name: root
host: "{{ item }}"
password: "{{ mysql_root_password }}"
priv: "*.*:ALL,GRANT"
with_items:
- "{{ ansible_hostname }}"
- 127.0.0.1
- ::1
- localhost
Run Code Online (Sandbox Code Playgroud)
我有这个错误
failed: [default] => (item=vagrant-ubuntu-trusty-64) => {"failed": true, "item": "vagrant-ubuntu-trusty-64"}
msg: unable to connect to database, check login_user and login_password are correct or ~/.my.cnf has the credentials
failed: [default] => (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 ~/.my.cnf has the credentials
failed: [default] => (item=::1) => {"failed": true, "item": "::1"}
msg: unable to connect to database, check login_user and login_password are correct or ~/.my.cnf has the credentials
failed: [default] => (item=localhost) => {"failed": true, "item": "localhost"}
msg: unable to connect to database, check login_user and login_password are correct or ~/.my.cnf has the credentials
Run Code Online (Sandbox Code Playgroud)
我的.my.cnf是
[client]
user=root
password={{ mysql_root_password }}
Run Code Online (Sandbox Code Playgroud)
并在服务器上复制时
[client]
user=root
password=root
Run Code Online (Sandbox Code Playgroud)
我不明白为什么,〜/ .my.cnf被创建了
Github项目
谢谢
ted*_*r42 37
什么时候mysql-server
无头安装,没有密码.因此,为了使.my.cnf
工作,它应该有一个空白的密码行.这是我测试的一个.my.cnf
:
[client]
user=root
password=
Run Code Online (Sandbox Code Playgroud)
将.my.cnf
您的vagrant
用户目录放入root用户目录并且只能以root身份读取时,这也有点奇怪.
在确保密码为空后.my.cnf
,我能够在这四个上下文中正确设置root密码.请注意,之后无法运行,因为.my.cnf
需要更新,因此它无法进行幂等性测试.
在ansible mysql_user模块页面上有一个注释,建议写入密码然后编写.my.cnf
文件.如果你这样做,你需要一个动作的where
子句mysql_user
(可能之前有一个文件stat).
更优雅是使用check_implicit_admin
沿着login_user
和login_password
.这是非常幂等的.
作为第三种方式,也许check_implicit_admin
使它更容易.
这是我成功的剧本,展示了上述内容,并使用一些新服务器进行了测试.有点为此感到自豪..my.cnf
所有这些都不需要注意.
---
- hosts: mysql
vars:
mysql_root_password: fart
tasks:
- name: Install MySQL
apt: name={{ item }} update_cache=yes cache_valid_time=3600 state=present
sudo: yes
with_items:
- python-mysqldb
- mysql-server
#- name: copy cnf
# copy: src=.my.cnf dest=~/.my.cnf owner=ubuntu mode=0644
# sudo: yes
- name: Start the MySQL service
sudo: yes
service:
name: mysql
state: started
enabled: true
- name: update mysql root password for all root accounts
sudo: yes
mysql_user:
name: root
host: "{{ item }}"
password: "{{ mysql_root_password }}"
login_user: root
login_password: "{{ mysql_root_password }}"
check_implicit_admin: yes
priv: "*.*:ALL,GRANT"
with_items:
- "{{ ansible_hostname }}"
- 127.0.0.1
- ::1
- localhost
Run Code Online (Sandbox Code Playgroud)
(编辑 - 删除my.cnf)
Arb*_*zar 13
这是我完整的MySQL工作角色,可能对您有所帮助.
vars/main.yml:
mysql_root_pass: mypassword #MySQL Root Password
Run Code Online (Sandbox Code Playgroud)
问/ main.yml:
---
- name: Install the MySQL packages
apt: name={{ item }} state=installed update_cache=yes
with_items:
- mysql-server-5.6
- mysql-client-5.6
- python-mysqldb
- libmysqlclient-dev
- name: Update MySQL root password for all root accounts
mysql_user: name=root host={{ item }} password={{ mysql_root_pass }} state=present
with_items:
- "{{ ansible_hostname }}"
- 127.0.0.1
- ::1
- localhost
- name: Copy the root credentials as .my.cnf file
template: src=root.cnf.j2 dest=~/.my.cnf mode=0600
- name: Ensure Anonymous user(s) are not in the database
mysql_user: name='' host={{ item }} state=absent
with_items:
- localhost
- "{{ ansible_hostname }}"
- name: Remove the test database
mysql_db: name=test state=absent
notify:
- Restart MySQL
Run Code Online (Sandbox Code Playgroud)
模板/ root.cnf.j2
[client]
user=root
password={{ mysql_root_pass }}
Run Code Online (Sandbox Code Playgroud)
处理器/ main.yml
---
- name: Restart MySQL
service: name=mysql state=restarted
Run Code Online (Sandbox Code Playgroud)
site.yml
---
- hosts: all
become: yes
gather_facts: yes
roles:
- mysql
Run Code Online (Sandbox Code Playgroud)
如果您需要任何帮助,请查看此github 链接.谢谢
归档时间: |
|
查看次数: |
42630 次 |
最近记录: |