Ansible 访问因“root@localhost”错误而被拒绝

问题描述 投票:0回答:2

我正在尝试通过 Ansible playbook 创建一个 mysql 数据库。在阅读了这里的几个主题和问题后,我创建了这样的剧本 -

---
- name: "Install Packages"
  hosts: localhost
  connection: local
  become: yes
  vars:
    mysql_root_password: Test@1123

  tasks:

  - name: Install basic packages
    apt:
      name:
      - vim
      - curl
      - python3-pip
      - mysql-server
      - libmysqlclient-dev
      - python-pymysql
      state: latest
      cache_valid_time: 3600

  - name: start & enable mysql Server
    service:
      name=mysql
      state=started
      enabled=yes

  - name: checking RabbitMQ & mysql Server status
    command: systemctl status "{{ item }}"
    with_items:
    - mysql
    register: result
    ignore_errors: yes

  - name: showing mysql Server status
    debug:
     var: result

  - name: Install pip modules
    pip:
      name:
      - pymysql
      - virtualenv
      state: present
      executable: /usr/bin/pip3

  - name: create /root/.my.cnf with password credentials
    blockinfile:
      path: /root/.my.cnf
      block: |
        [client]
        user=root
        password={{ mysql_root_password }}

        [mysql]
        user=root
        password={{ mysql_root_password }}

        [mysqldump]
        user=root
        password={{ mysql_root_password }}

        [mysqldiff]
        user=root
        password={{ mysql_root_password }}
      create: yes

  - name: "Mysql Configuration - Resetting RootPassword"
    mysql_user:
      login_user: root
      login_password: ''
      name: root
      host_all: yes
      password: "{{mysql_root_password}}"

  - name: Create a new database with name 'test'
    mysql_db:
      login_user: root
      login_password: "{{mysql_root_password}}"
      login_unix_socket: /run/mysqld/mysqld.sock
      name: test
      state: present

  - name: Create database user with name 'test' and password 'Test@1123' with all database privileges
    mysql_user:
      name: test
      password: Test@1123
      priv: '*.test:ALL'
      state: present

运行此剧本时,我遇到以下错误 -

TASK [Mysql Configuration - Resetting RootPassword] *****************************************************************************************************************************************
[WARNING]: Module did not set no_log for update_password
fatal: [localhost]: 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: (1698, u\"Access denied for user 'root'@'localhost'\")"}

我已检查 /root/.my.cnf 文件已使用正确的用户名和密码进行更新。

我尝试跑步

mysql -u root -p
。没有任何密码,这已经成功了。但是在尝试运行
mysql -u 'root'@'localhost' -p
时,它不允许使用或不使用密码进入mysql。

我知道这可以通过多种方式手动修复。但是如何使用 Ansible 解决这个问题。也许我犯了一些愚蠢的错误。但无法弄清楚。我也尝试过没有 /root/.my.cnf 文件和套接字文件。但面临同样的问题。


更新:

我按照this

解决了这个问题
mysql ansible
2个回答
0
投票

请查看 https://docs.ansible.com/ansible/latest/modules/mysql_user_module.html#examples mysql_user_module 文档。

这可能是您想要实现的目标:

- name: "Mysql Configuration - Resetting RootPassword"
  mysql_user:
    login_user: root
    login_password: actual_root_mysql_password
    host: ''
    name: root
    password: "{{mysql_root_password}}"

0
投票

对于这些天偶然发现这个问题的人,这篇文章有一个解释和修复:https://stackoverflow.com/a/78373277/3332772

© www.soinside.com 2019 - 2024. All rights reserved.