简体   繁体   English

ubuntu 14.04的一个文件中的ansible mysql配置

[英]ansible mysql configuration in one file for ubuntu 14.04

I'm trying to write a single page ansible script for mysql installation and setup a new user and create a empty DB. 我正在尝试为mysql安装编写一个单页ansible脚本,并设置一个新用户并创建一个空DB。 what I've tried so far - 到目前为止我尝试过的-

hosts file hosts文件

[mysql]
webapp ansible_ssh_host=xxx.xxx.xxx.xxx

mysql.yml (The single file for all tasks/vars/handelers) (both hosts and mysql.yml in same directory) and I can login in remote system using ssh 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

when I run this script I get this output(+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

You can't connect to mysql server before you will set root password. 设置root密码之前,您无法连接到mysql服务器。 To do this use the following: 为此,请使用以下命令:

- name: Set password for root user
  shell:
    mysqladmin password "{{ root_password }}" -u root

After a default installation on ubuntu, you only can login to your mysql as root with password "root". 在ubuntu上进行默认安装后,您只能以root用户身份使用密码“ root”登录到mysql。 So to change your password you will have to set your mysql login user to root (as you did in the next task) 因此,要更改密码,您将必须将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

There is also a ready-to-go solution in ansible galaxy 银河系中还有一个现成的解决方案

Please add this task inside your task/main.yml 请将此任务添加到您的task / main.yml中

- name: Copy the root credentials as .my.cnf file
   template: src=root.cnf.j2 dest=~/.my.cnf mode=0600

and this will be your root.cnf.j2 这将是您的root.cnf.j2

[client]
user=root
password={{ root_password }}

What it will do is, to connect the mysql from the root user without password and perform these task. 它将要做的是,从没有密码的root用户连接mysql并执行这些任务。 You can remove it after running all the task or leave it like this because it is under root and have tight permission. 您可以在运行所有任务后将其删除,也可以像这样保留它,因为它在根目录下并且具有严格的权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM