首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Ansible为MySQL/MariaDB根用户设置和保存随机密码?

问如何使用Ansible为MySQL/MariaDB根用户设置和保存随机密码?
EN

Stack Overflow用户
提问于 2018-05-28 10:04:53
回答 1查看 2.7K关注 0票数 0

如何使用Ansible为MariaDB / MySQL数据库设置随机(32个字符的字母数字)根密码,并将其保存到~/.my.cnf文件(允许命令查找此密码)?

它只应该配置它一次,而不是每次都更改密码,如果游戏手册是多次运行。

这使用变量中的密码。

如果我使用此方法,每次运行剧本时,它都会更改密码:(它无法保存密码-如果在此任务后中断了游戏手册,密码就丢失了)

代码语言:javascript
复制
- name: "Change database root user password"
  mysql_user:
    name: root
    password: "{{ lookup('password','/dev/null chars=ascii_letters,digits length=32') }}"
    host: "{{ item }}"
    check_implicit_admin: yes
    priv: "*.*:ALL,GRANT"
    state: present
  when: mysql_root_result.stat.exists == False
  with_items:
  - localhost
  - "::1"
  - 127.0.0.1
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-28 10:04:53

一些假设:

  • 根用户已被'root'@'localhost'充分覆盖,其他本地根用户需要删除。
  • 任务必须是容错的。如果被打断,应该有可能在合理的可能范围内再次运行。
  • 当root - ~/.my.cnf扩展到/root/.my.cnf时,就会运行不可见的任务。
  • 所有其他需要密码的东西都可以从~/.my.cnf获得(只要它们作为正确的用户运行,在本例中是root )。

概述:

  • 这将使用这生成根密码。
  • 它将其保存到~/.my.cnf.new (以确保在设置它之前在服务器上可用)。这允许(手动)恢复(如果中断)
  • 它在数据库中设置密码。
  • 它重命名文件(在两个任务中的功能 -一个硬链接和删除) (link和unlink)
  • 它清除任何其他类似的用户,如'root'@'127.0.0.1'、'root'@'::1'、'root'@'<hostname>' (使用一些事实来确保大多数可能性被击中)。

密码配置模板:(相对于角色目录,“Template /my_passwd.cnf.j2”)

代码语言:javascript
复制
[client]
user={{ item.user }}
password={{ item.password }}

任务:

代码语言:javascript
复制
# MariaDB: Set up secure root password
# Set up (and save) secure root password
# Check for /root/.my.cnf
# All the other things are skipped if this file already exists
- name: "Check if we already have a root password config"
  stat:
    path: /root/.my.cnf
  register: mysql_root_result

# Generate password
# This uses https://docs.ansible.com/ansible/latest/plugins/lookup/password.html
# to generate a 32 character random alphanumeric password
- name: "Generate database root password if needed"
  set_fact:
    mysql_root_passwd: "{{ lookup('password','/dev/null chars=ascii_letters,digits length=32') }}"
  when: mysql_root_result.stat.exists == False

# Generate /root/.my.cnf.new
# A temporary file is used to keep it from breaking further commands
# It also ensures that the password is on the server if the critical
# parts are interrupted
- name: "Save new root password in temporary file"
  template:
    src: my_passwd.cnf.j2
    dest: /root/.my.cnf.new
    owner: root
    group: root
    mode: 0400
  when: mysql_root_result.stat.exists == False
  with_items:
  - user: root
    password: "{{ mysql_root_passwd }}"

# START of area that you don't want to interrupt
# If this is interrupted after the first task
# it can be fixed by manually running this on the server
# mv /root/.my.cnf.new /root/.my.cnf
# If the playbook is reran before that. The password would be lost!
# Add DB user
- name: "Add database root user"
  mysql_user:
    name: root
    password: "{{ mysql_root_passwd }}"
    host: "{{ item }}"
    check_implicit_admin: yes
    priv: "*.*:ALL,GRANT"
    state: present
  when: mysql_root_result.stat.exists == False
  with_items:
  - localhost

# Now move the config in place
- name: "Rename config with root password to correct name - Step 1 - link"
  file:
    state: hard
    src: /root/.my.cnf.new
    dest: /root/.my.cnf
    force: yes
  when: mysql_root_result.stat.exists == False
# END of area that you don't want to interrupt

# Interrupting before this task will leave a temporary file around
# Everything will work as it should though
- name: "Rename config with root password to correct name - Step 2 - unlink"
  file:
    state: absent
    path: /root/.my.cnf.new
  when: mysql_root_result.stat.exists == False

# Remove additional root users - these don't have the password set
# You might want to ensure that none of these variables are `localhost`
# All return somewhat different values on my test system
- name: "Clean up additional root users"
  mysql_user:
    name: root
    host: "{{ item }}"
    check_implicit_admin: yes
    state: absent
  with_items:
  - "::1"
  - 127.0.0.1
  - "{{ ansible_fqdn }}"
  - "{{ inventory_hostname }}"
  - "{{ ansible_hostname }}"
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50563845

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档