无法更改root帐户ansible的密码

时间:2016-07-18 09:35:43

标签: mysql ansible

我有以下错误:

{"failed": true, "invocation": {"module_args": 
 {"append_privs": false, "check_implicit_admin": true, "config_file": "/root/.my.cnf", "connect_timeout": 30, "encrypted": false, "host": "rothko", "host_all": false, "login_host": "localhost", "login_password": "", "login_port": 3306, "login_unix_socket": null, "login_user": "root", "name": "root", "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER", "priv": "*.*:ALL,GRANT", "sql_log_bin": true, "ssl_ca": null, "ssl_cert": null, "ssl_key": null, "state": "present", "update_password": "always", "user": "root"}, "module_name": "mysql_user"}, 
  "item": "rothko", 
   "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: NO)\")"}

这些是我的剧本说明和我尝试的内容:

#    - name: Create .my.cnf file with root password credentials
#      become: yes
#      template: src=".my.cnf.j2" dest="/root/.my.cnf" owner=root group=root mode=0600
#    - name: Restart mysql
#      service: name=mysqld state=restarted
#    - name: Change root user password on first run
#      mysql_user:
#        name=root
#        host=localhost
#        password={{ mysql_root_password }}
#        priv=*.*:ALL,GRANT
    - name: update mysql root password for all root accounts
      become: yes
      mysql_user:
        login_user=root
        login_password=''
        name=root
        host="{{ item }}"
        password="{{ mysql_root_password }}"
        check_implicit_admin=yes
        priv="*.*:ALL,GRANT"
      with_items:
        - "{{ ansible_hostname }}"
        - 127.0.0.1
        - ::1
        - localhost
      ignore_errors: true

这是一个众所周知的问题,我发现了一些问题,但我没有解决方案:

2 个答案:

答案 0 :(得分:2)

此处提供更多信息:Change mysql root password on Centos7

我尝试做一些安全的事情

    - name: Stop MySQL
      service: name=mysqld state=stopped
    - name: set environment variables
      shell: systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
    - name: Start MySQL
      service: name=mysqld state=started
    - name: sql query
      command:  mysql -u root --execute="UPDATE mysql.user SET authentication_string = PASSWORD('toto') WHERE User = 'root' AND Host = 'localhost';"
    - name: sql query flush
      command:  mysql -u root --execute="FLUSH PRIVILEGES"
    - name: Stop MySQL
      service: name=mysqld state=stopped
    - name: unset environment variables
      shell: systemctl unset-environment MYSQLD_OPTS
    - name: Start MySQL
      service: name=mysqld state=started

希望有所帮助

答案 1 :(得分:0)

完全归功于@glmrenard;这是我在Ubuntu 16.04上为Percona服务器5.7改编的版本:

---
#Reset MySQL root password
- name: Stop MySQL
  systemd:
    name: mysql
    state: stopped

- name: set environment variables
  shell: systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"

- name: Start MySQL
  systemd:
    name: mysql
    state: started

- name: Reset root PW
  command: mysql -u root --execute="UPDATE mysql.user SET authentication_string = PASSWORD('{{ mysql_rootpw }}') WHERE User = 'root' AND (Host = 'localhost' OR Host = '127.0.0.1');"

- name: Flush MySQL privileges
  command: mysql -u root --execute="FLUSH PRIVILEGES"

- name: Stop MySQL
  systemd:
    name: mysql
    state: stopped

- name: Unset environment variables
  shell: systemctl unset-environment MYSQLD_OPTS

- name: Start MySQL
  systemd:
    name: mysql
    state: started