[英]ansible mysql configuration in one file for ubuntu 14.04
我正在尝试为mysql安装编写一个单页ansible脚本,并设置一个新用户并创建一个空DB。 到目前为止我尝试过的-
hosts
文件
[mysql]
webapp ansible_ssh_host=xxx.xxx.xxx.xxx
mysql.yml
(所有任务/ vars / handelers的单个文件)(主机和mysql.yml都在同一目录中),我可以使用ssh登录远程系统
---
- hosts: mysql
vars:
- system_packages:
- build-essential
- python-dev
- python-pip
- libmysqlclient-dev
- mysql-server
- python-mysqldb
- root_password: root
tasks:
- name: Install MySQL
apt: pkg={{ item }} state=installed update-cache=yes
with_items: system_packages
tags:
- setup
- name: Start MySQL service
action: service name=mysql state=started enabled=yes
- name: Update mysql password for root account
mysql_user: name=root host={{ item }} password={{root_password}}
with_items:
- 127.0.0.1 #In case of distributed system how should I place Ip addr of this system
- localhost
- name: create db 'mydb'
action: mysql_db db=mydb state=present
- name: Creates database user 'eric' with password 'eric' for 'mydb' and grant all priveleges
action: mysql_user state=present name=eric password=eric priv=mydb.*:ALL
handlers:
- name: start mysql
service: name=mysql state=started
当我运行此脚本时,我得到此输出(+错误)
failed: [webapp] => (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
设置root密码之前,您无法连接到mysql服务器。 为此,请使用以下命令:
- name: Set password for root user
shell:
mysqladmin password "{{ root_password }}" -u root
在ubuntu上进行默认安装后,您只能以root用户身份使用密码“ root”登录到mysql。 因此,要更改密码,您将必须将mysql登录用户设置为root(如在下一个任务中一样)
- name: Update mysql password for root account
mysql_user:
name=root host={{ item }}
password={{root_password}}
login_user=root
login_password="root"
sudo: yes
sudo_user: root
在银河系中还有一个现成的解决方案
请将此任务添加到您的task / main.yml中
- name: Copy the root credentials as .my.cnf file
template: src=root.cnf.j2 dest=~/.my.cnf mode=0600
这将是您的root.cnf.j2
[client]
user=root
password={{ root_password }}
它将要做的是,从没有密码的root用户连接mysql并执行这些任务。 您可以在运行所有任务后将其删除,也可以像这样保留它,因为它在根目录下并且具有严格的权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.