使用Ansible安装和配置mysql

时间:2018-07-09 17:40:41

标签: mysql ansible

我已使用以下ansible-playbook安装了MySql,nginx和PHP:

---
- hosts: all
  become: true
  tasks:
    - name: Make Sure we can connect to hosts
      ping:
    - name: Install PHP
      yum: name={{item}} state=present update_cache=yes
      with_items:
        - php
        - php-fpm
        - php-mysql
        - php-xml
    - name: add repository nginx-release (CentOS6/CentOS7)
      yum: name="http://nginx.org/packages/centos/{{ansible_distribution_major_version}}/noarch/RPMS/nginx-release-centos-{{ansible_distribution_major_version}}-0.el{{ansible_distribution_major_version}}.ngx.noarch.rpm"
    - name: Install nginx
      yum: name=nginx state=installed enablerepo=nginx
    - name: Download MySQL Community Repo
      command: /usr/bin/rpm -ivh /tmp/mysql-community-release-el7-7.noarch.rpm
    - name: Install MySql
      yum: name={{item}} state=installed
      with_items:
        - mysql-server
        - MySQL-python

我正在尝试通过以下ansible剧本使用ansible配置mysql:

---
- hosts: ansible-ctl
  become: true
  tasks:
    - name: Make Sure we can connect to hosts
      ping:
    - name: Start MySQL Server and enable it
      service: name=mysqld state=started enabled=yes
    - name: Generate new root password
      command: openssl rand -hex 7 creates=/root/.my.cnf
      register: mysql_new_root_pass
    - name: Create my.cnf
      template: src=templates/mysql/my.cnf dest=/root/.my.cnf
      when: mysql_new_root_pass.changed
    - name: Remove anonymous users
      mysql_user: name='' host_all=yes state=absent
      when: mysql_new_root_pass.changed
    - name: Remove test database
      mysql_db: name=test state=absent
      when: mysql_new_root_pass.changed
    - name: Output new root password
      debug: msg="New root password is {{mysql_new_root_pass.stdout}}"
      when: mysql_new_root_pass.changed
    - name: Update root password
      mysql_user: name=root host={{item}} password={{mysql_new_root_pass.stdout}}
      with_items:
        - "{{ ansible_hostname }}"
        - 127.0.0.1
        - ::1
        - localhost
      when: mysql_new_root_pass.changed

my.cnf模板文件位于/ templates / mysql /位置:

[client]
user=root
password={{ mysql_new_root_pass.stdout }}

/root/.my.cnf文件正在使用密码生成。

首先,我正在启动mysql服务。然后使用openssl创建一个新密码并将其存储在mysql_new_root_pass中。然后基于/root/.my.cnf文件的存在,我尝试执行诸如删除匿名用户,删除测试数据库和更新root密码之类的任务。但是我的剧本在步骤Remove Anonymous Users上抛出错误。以下是我收到的错误:

fatal: [ansible-ctl]: FAILED! => {"changed": false, "msg": "unable to connect to database, check login_user and login_password are correct or /root/.my.cnf has the credentials. Exception message: (1045, \"Access denied for user 'root'@'localhost' (using password: YES)\")"}

我正在关注从Apress-Ansible Beginner到Pro的书以及以下链接Installing MySQL on Centos 7 server using Ansible。我的ansible控制器是在VM上运行Centos 7 Minimal的Macbook Pro。

生成的.my.cnf:

[client]
user=root
password=<generated password>

任何人都可以解释这里出了什么问题以及如何删除匿名用户吗?

1 个答案:

答案 0 :(得分:0)

这失败了,因为一旦您创建文件/root/.my.cnf,它将用于连接到mysql,但是您尚未更新root用户的密码。 将任务name: Update root password移动到任务name: Create my.cnf之前