我正在尝试写一个mysql安装和设置一个新用户和创建一个空数据库的单页ansible脚本。到目前为止我试过的是-
hosts文件
[mysql]
webapp ansible_ssh_host=xxx.xxx.xxx.xxxmysql.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当我运行这个脚本时,我得到这样的输出(+error)
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发布于 2014-12-07 06:07:46
在设置root密码之前,您无法连接到mysql服务器。要执行此操作,请使用以下命令:
- name: Set password for root user
shell:
mysqladmin password "{{ root_password }}" -u root发布于 2014-12-07 20:04:20
在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在ansible galaxy中也有一个现成的解决方案
发布于 2014-12-24 19:57:48
请将此任务添加到任务/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并执行这些任务。您可以在运行完所有任务后将其移除,也可以将其保留为这样,因为它位于root下,并且具有严格的权限。
https://stackoverflow.com/questions/27334123
复制相似问题