在Ubuntu上使用Ansible安装MySQL [英] Install MySQL with ansible on ubuntu
问题描述
我在无所事事的ubuntu上安装带有ansible的MySQL时遇到问题,
I have a problem installing MySQL with ansible on a vagrant ubuntu,
这是我的MySQL部分
This is my MySQL part
---
- 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
我有这个错误
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
我的.my.cnf是
[client]
user=root
password={{ mysql_root_password }}
以及在服务器上复制时
[client]
user=root
password=root
我不明白为什么创建〜/.my.cnf
I don't understand why, ~/.my.cnf is created
项目 Github
谢谢
推荐答案
无头安装mysql-server
时,没有密码.因此,要使.my.cnf
工作,它应该有一个空白的密码行.这是我测试的.my.cnf
的结果:
When mysql-server
is installed headlessly, there's no password. Therefore to make .my.cnf
work, it should have a blank password line. Here's what I tested with for a .my.cnf
:
[client]
user=root
password=
将.my.cnf
置于root用户所有且只能由root读取的vagrant
用户目录中也有点奇怪.
It's also slightly strange to put .my.cnf
in your vagrant
user directory as owned by root and only readable as root.
确保.my.cnf
中的密码为空后,我能够在这四个上下文中正确设置root的密码.请注意,此后它无法运行,因为.my.cnf
需要更新,因此它无法通过幂等性测试.
After ensuring the password was blank in .my.cnf
, I was able to properly set the password for root in those four contexts. Note that it fails to run after that, since .my.cnf
would need to be updated, so it fails the idempotency test.
在一个有趣的mysql_user模块页面上有一条注释,建议写密码,然后然后写.my.cnf
文件.如果这样做,则mysql_user
动作需要一个where
子句(可能在此子句之前带有文件stat).
There's a note on the ansible mysql_user module page that suggests writing the password and then writing the .my.cnf
file. If you do that, you need a where
clause to the mysql_user
action (probably with a file stat before that).
更优雅的是将check_implicit_admin
与login_user
和login_password
一起使用.真是幂等.
Even more elegant is to use check_implicit_admin
along with login_user
and login_password
. That's beautifully idempotent.
第三种方式,也许check_implicit_admin
使其变得更加容易.
As a third way, perhaps check_implicit_admin
makes it even easier.
这是我成功的剧本,上面显示了上述内容,并通过一些新服务器进行了测试. Kinda为此感到自豪.注意.my.cnf
对于所有这些都是不必要的.
Here's my successful playbook showing the above, tested with a few fresh servers. Kinda proud of this. Note .my.cnf
is unnecessary for all of this.
---
- 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
(编辑-删除my.cnf)
(edit- removed my.cnf)
这篇关于在Ubuntu上使用Ansible安装MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!