Vagrant Ansible CentOS 7如何安装MySQL 5.7并更改默认密码

时间:2016-07-22 09:42:07

标签: vagrant ansible ansible-playbook centos7

我在CentOS 7上使用Vagrant和Ansble。 我正在尝试安装MySQL 5.7,但在尝试更新MySQL密码时遇到了问题。

- name: Install MySQL 5.7 repo
  yum: name=http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm state=present

- name: Install MySQL 5.7
  yum: pkg={{ item }}
  with_items:
    - mysql-community-server
    - MySQL-python

- name: Start the MySQL service
  service: name=mysqld state=started enabled=true

- name: update mysql root passwd
  mysql_user: name=root
          host={{ item }}
          password='PassW0rd'
          check_implicit_admin=yes
          login_user=root
          login_password=''
          state=present
  with_items:
    - 127.0.0.1
    - ::1
    - localhost

我研究过并发现MySQL 5.7自动生成默认密码,因此我的脚本失败了。有人帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:7)

---
tasks:
  - name: Install MySQL 5.7 repo
    yum: name=http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm state=present

  - name: Install MySQL 5.7
    yum: pkg={{ item }}
    with_items:
    - mysql-community-server
    - mysql-community-client
    - MySQL-python

  - name: Start the MySQL service
    service: name=mysqld state=started enabled=true

  - name: Change mysql root password and keep track in 
    shell: |
      password_match=`awk '/A temporary password is generated for/ {a=$0} END{ print a }' /var/log/mysqld.log | awk '{print $(NF)}'`
      echo $password_match
      mysql -uroot -p$password_match --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'PassW0rd'; flush privileges; "
      echo "[client]"
      user=root
      password=PassW0rd > /root/.my.cnf
    args:
      creates: /root/.my.cnf
    register: change_temp_pass
    notify: restart mysqld

  - meta: flush_handlers
  - debug:
      var: change_temp_pass

handlers:
  - name: restart mysqld
      service: 
      name: mysqld 
      state: restarted