[Ansible实战]-ansible初始化mysql数据库
0.目录结构
$ tree mysql
mysql
├── files
│ └── web.sql
├── handlers
│ └── main.yml
├── tasks
│ └── main.yml
├── templates
│ ├── client.cnf.j2
│ ├── my.cnf.j2
│ ├── mysql-clients.cnf.j2
│ └── server.cnf.j2
└── vars
1.tasks内容
- name: 01-install
yum:
name: "{{ item }}"
state: installed
loop:
- MySQL-python
- mariadb
- mariadb-server
notify:
- remove_user
- remove_test
- set_password
- create_user
- name: 02-start
service:
name: mariadb
state: started
- name: 03-copy_conf
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
loop:
- { src: my.cnf.j2, dest: /etc/my.cnf }
- { src: client.cnf.j2, dest: /etc/my.cnf.d/client.cnf }
- { src: server.cnf.j2, dest: /etc/my.cnf.d/server.cnf }
- { src: mysql-clients.cnf.j2, dest: /etc/my.cnf.d/mysql-clients.cnf }
notify:
- restart_mysql
- name: 04-copy_sql
copy:
src: web.sql
dest: /tmp/web.sql
notify:
- import_sql
2.handlers内容
- name: remove_user
mysql_user:
name: ''
host_all: yes
state: absent
- name: remove_test
mysql_db:
name: test
state: absent
- name: set_password
mysql_user:
name: root
password: '123456'
- name: create_user
mysql_user:
login_user: root
login_password: '123456'
name: web
password: oldboy123
host: '172.16.1.%'
priv: '*.*:ALL,GRANT'
state: present
- name: restart_mysql
service:
name: mariadb
state: restarted
- name: import_sql
mysql_db:
login_user: root
login_password: '123456'
name: all
state: import
target: /tmp/web.sql
老男孩Linux教育欢迎咨询免费试听 联系方式: QQ:526195417