問題描述
我想在 AWS 上設(shè)置一個(gè) MySQL 服務(wù)器,使用 Ansible 進(jìn)行配置管理.我使用的是 Amazon (ami-3275ee5b) 的默認(rèn) AMI,它使用 yum
進(jìn)行包管理.
I want to setup a MySQL server on AWS, using Ansible for the configuration management.
I am using the default AMI from Amazon (ami-3275ee5b), which uses yum
for package management.
當(dāng)執(zhí)行下面的 Playbook 時(shí),一切順利.但是當(dāng)我第二次運(yùn)行它時(shí),任務(wù) Configure the root credentials
失敗了,因?yàn)?MySQL 的舊密碼不再匹配,因?yàn)樗言谖疑洗芜\(yùn)行此 Playbook 時(shí)更新.
When the Playbook below is executed, all goes well. But when I run it for a second time, the task Configure the root credentials
fails, because the old password of MySQL doesn't match anymore, since it has been updated the last time I ran this Playbook.
這使得 Playbook 非冪等,我不喜歡.我希望能夠根據(jù)需要多次運(yùn)行 Playbook.
This makes the Playbook non-idempotent, which I don't like. I want to be able to run the Playbook as many times as I want.
- hosts: staging_mysql
user: ec2-user
sudo: yes
tasks:
- name: Install MySQL
action: yum name=$item
with_items:
- MySQL-python
- mysql
- mysql-server
- name: Start the MySQL service
action: service name=mysqld state=started
- name: Configure the root credentials
action: command mysqladmin -u root -p $mysql_root_password
解決這個(gè)問題的最佳方法是什么,這意味著使 Playbook 具有冪等性?提前致謝!
What would be the best way to solve this, which means make the Playbook idempotent? Thanks in advance!
推薦答案
用于安全安裝 MySQL 的 Ansible 版本.
mysql_secure_installation.yml
- hosts: staging_mysql
user: ec2-user
sudo: yes
tasks:
- name: Install MySQL
action: yum name={{ item }}
with_items:
- MySQL-python
- mysql
- mysql-server
- name: Start the MySQL service
action: service name=mysqld state=started
# '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 }}
with_items:
- "{{ ansible_hostname }}"
- 127.0.0.1
- ::1
- localhost
- name: copy .my.cnf file with root password credentials
template: src=templates/root/my.cnf.j2 dest=/root/.my.cnf owner=root mode=0600
- name: delete anonymous MySQL server user for $server_hostname
action: mysql_user user="" host="{{ server_hostname }}" state="absent"
- name: delete anonymous MySQL server user for localhost
action: mysql_user user="" state="absent"
- name: remove the MySQL test database
action: mysql_db db=test state=absent
templates/root/my.cnf.j2
[client]
user=root
password={{ mysql_root_password }}
參考文獻(xiàn)
- Lorin Hochstein 的原始答案
- https://github.com/gaspaio/ansible-devbox/blob/master/roles/mysql/tasks/server.yml
這篇關(guān)于Ansible 冪等 MySQL 安裝 Playbook的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!