在ubuntu上安装带有ansible的MySQL

在ubuntu上安装带有ansible的MySQL,mysql,ubuntu,vagrant,ansible,Mysql,Ubuntu,Vagrant,Ansible,我在一个流浪的ubuntu上安装带有ansible的MySQL时遇到问题 这是我的MySQL部分 --- - name: Install MySQL apt: name: "{{ item }}" with_items: - python-mysqldb - mysql-server - name: copy .my.cnf file with root password credentials template: src: templates/r

我在一个流浪的ubuntu上安装带有ansible的MySQL时遇到问题

这是我的MySQL部分

---
- name: Install MySQL
  apt:
    name: "{{ item }}"
  with_items:
    - python-mysqldb
    - mysql-server

- name: copy .my.cnf file with root password credentials
  template: 
    src: templates/root/.my.cnf
    dest: ~/.my.cnf
    owner: root
    mode: 0600

- name: Start the MySQL service
  service: 
    name: mysql 
    state: started
    enabled: true

  # 'localhost' needs to be the last item for idempotency, see
  # http://ansible.cc/docs/modules.html#mysql-user
- name: update mysql root password for all root accounts
  mysql_user: 
    name: root 
    host: "{{ item }}" 
    password: "{{ mysql_root_password }}" 
    priv: "*.*:ALL,GRANT"
  with_items:
    - "{{ ansible_hostname }}"
    - 127.0.0.1
    - ::1
    - localhost 
我有这个错误

failed: [default] => (item=vagrant-ubuntu-trusty-64) => {"failed": true, "item": "vagrant-ubuntu-trusty-64"}
msg: unable to connect to database, check login_user and login_password are correct or ~/.my.cnf has the credentials
failed: [default] => (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
failed: [default] => (item=::1) => {"failed": true, "item": "::1"}
msg: unable to connect to database, check login_user and login_password are correct or ~/.my.cnf has the credentials
failed: [default] => (item=localhost) => {"failed": true, "item": "localhost"}
msg: unable to connect to database, check login_user and login_password are correct or ~/.my.cnf has the credentials
我的.我的.cnf是

[client]
user=root
password={{ mysql_root_password }}
当复制到服务器上时

[client]
user=root
password=root
我不明白为什么会创建~/.my.cnf

计划


谢谢

mysql服务器
无头安装时,没有密码。因此,要使
.my.cnf
工作,它应该有一个空白密码行。以下是我对
.my.cnf
的测试:

[client]
user=root
password=
.my.cnf
放在您的
vagrant
用户目录中也有点奇怪,它由root所有,并且只能作为root读取

在确保
.my.cnf
中的密码为空后,我能够在这四个上下文中为root用户正确设置密码。请注意,此后它将无法运行,因为需要更新
.my.cnf
,因此它无法通过幂等性测试

ansible mysql_用户模块页面上有一条提示,建议先写密码,然后写
.my.cnf
文件。如果这样做,您需要一个
where
子句来执行
mysql\u用户
操作(可能在此之前有一个文件stat)

更优雅的是使用
check\u implicit\u admin
以及
login\u user
login\u password
。这是美丽的幂等式

作为第三种方法,也许
check\u implicit\u admin
更容易实现

下面是我成功的剧本,展示了以上内容,并用几个新服务器进行了测试。有点自豪。注意:所有这些都不需要.my.cnf

---
- hosts: mysql
  vars:
    mysql_root_password: fart
  tasks:
  - name: Install MySQL
    apt: name={{ item }} update_cache=yes cache_valid_time=3600 state=present
    sudo: yes
    with_items:
    - python-mysqldb
    - mysql-server
  #- name: copy cnf
  #  copy: src=.my.cnf dest=~/.my.cnf owner=ubuntu mode=0644
  #  sudo: yes
  - name: Start the MySQL service
    sudo: yes
    service: 
      name: mysql 
      state: started
      enabled: true
  - name: update mysql root password for all root accounts
    sudo: yes
    mysql_user: 
      name: root 
      host: "{{ item }}" 
      password: "{{ mysql_root_password }}"
      login_user: root
      login_password: "{{ mysql_root_password }}"
      check_implicit_admin: yes
      priv: "*.*:ALL,GRANT"
    with_items:
      - "{{ ansible_hostname }}"
      - 127.0.0.1
      - ::1
      - localhost 

(编辑-删除my.cnf)

这是我完整的MySQL工作角色,可能会对您有所帮助

变量/main.yml

mysql_root_pass: mypassword #MySQL Root Password
---
 - name: Install the MySQL packages
   apt: name={{ item }} state=installed update_cache=yes
   with_items:
     - mysql-server-5.6
     - mysql-client-5.6
     - python-mysqldb
     - libmysqlclient-dev

 - name: Update MySQL root password for all root accounts
   mysql_user: name=root host={{ item }} password={{ mysql_root_pass }} state=present
   with_items:
     - "{{ ansible_hostname }}"
     - 127.0.0.1
     - ::1
     - localhost

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

 - name: Ensure Anonymous user(s) are not in the database
   mysql_user: name='' host={{ item }} state=absent
   with_items:
     - localhost
     - "{{ ansible_hostname }}"

 - name: Remove the test database
   mysql_db: name=test state=absent
   notify:
     - Restart MySQL
询问/main.yml

mysql_root_pass: mypassword #MySQL Root Password
---
 - name: Install the MySQL packages
   apt: name={{ item }} state=installed update_cache=yes
   with_items:
     - mysql-server-5.6
     - mysql-client-5.6
     - python-mysqldb
     - libmysqlclient-dev

 - name: Update MySQL root password for all root accounts
   mysql_user: name=root host={{ item }} password={{ mysql_root_pass }} state=present
   with_items:
     - "{{ ansible_hostname }}"
     - 127.0.0.1
     - ::1
     - localhost

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

 - name: Ensure Anonymous user(s) are not in the database
   mysql_user: name='' host={{ item }} state=absent
   with_items:
     - localhost
     - "{{ ansible_hostname }}"

 - name: Remove the test database
   mysql_db: name=test state=absent
   notify:
     - Restart MySQL
模板/root.cnf.j2

[client]
user=root
password={{ mysql_root_pass }}
handlers/main.yml

---
 - name: Restart MySQL
   service: name=mysql state=restarted
---
- hosts: all
  become: yes
  gather_facts: yes
  roles:
    - mysql
site.yml

---
 - name: Restart MySQL
   service: name=mysql state=restarted
---
- hosts: all
  become: yes
  gather_facts: yes
  roles:
    - mysql

如果您需要任何帮助,请查看此github。感谢ubuntu 20.04,现在需要登录unix套接字:

- name: setting default root password
  mysql_user:
    name: root
    password: "{{ mysql_root_password }}"
    check_implicit_admin: true
    login_unix_socket: /var/run/mysqld/mysqld.sock

干杯

@ant您是在尝试新服务器还是在部分安装完成的服务器上?如果是后者,请确保
.my.cnf
不存在,然后手动验证根密码。我正在执行vagrant Provision对吗,您是否
vagrant销毁了它?您是否删除了
.my.cnf
?您是否手动验证了根密码?请尝试删除上例中带有
login\u user
login\u password
行的字符串。由此看来,如果指定了凭据,则即使在上述工作手册中的ansible、s/sudo/Been\u user/g的当前版本中使用
check\u implicit\u admin=yes
,也会使用这些凭据!