環境說一下mysql
192.168.30.21 ansiblenginx
192.168.30.25 client1sql
192.168.30.26 client2shell
這裏個人ansible環境已經部署好了,因此沒有重複寫到這裏,若是不知道怎麼部署ansible的小夥伴能夠看我上篇隨筆(這裏不演示了 )數據庫
1.被管理端都須要配置yum源vim
配置本地yum倉庫就能夠ide
2.配置數據庫角色spa
[root@ansible ~]# mkdir -pv /etc/ansible/roles/mariadb/{files,tasks,handlers}rest
mkdir: 已建立目錄 "/etc/ansible/roles/mariadb"orm
mkdir: 已建立目錄 "/etc/ansible/roles/mariadb/files"
mkdir: 已建立目錄 "/etc/ansible/roles/mariadb/tasks"
mkdir: 已建立目錄 "/etc/ansible/roles/mariadb/handlers"
[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# vim /etc/ansible/mariadb.yml
- hosts: cloud
remote_user: root
roles:
- mariadb
[root@ansible ansible]# cd /etc/ansible/roles/mariadb/
[root@ansible mariadb]# ls
files handlers tasks
[root@ansible mariadb]# cd tasks/
[root@ansible tasks]# vim main.yml
- name: install mariadb
yum: name=mariadb-server state=present
- name: move config file
shell: "[ -e /etc/my.cnf ] && mv /etc/my.cnf /etc/my.cnf.bak"
- name: provide a new config file
copy: src=my.cnf dest=/etc/my.cnf
- name: reload mariadb
shell: systemctl restart mariadb
- name: create database testdb
shell: mysql -u root -e "create database aaa;grant all privileges on aaa.* to 'cloud'@'192.168.30.%' identified by 'test123';flush privileges;"
notify:
- restart mariadb
[root@ansible tasks]# cd ../
[root@ansible mariadb]# ls
files handlers tasks
[root@ansible mariadb]# cd handlers/
[root@ansible handlers]# vim main.yml
- name: restart mariadb
service: name=mariadb state=restarted
[root@ansible handlers]# cd ../files/
[root@ansible files]# cp /etc/my.cnf /etc/ansible/roles/mariadb/files/
[root@ansible files]# ls
my.cnf
[root@ansible files]# cd /etc/ansible/
[root@ansible ansible]# ls
ansible.cfg hosts mariadb.yml nginx.retry nginx.yaml roles zabbix-agent.yml
[root@ansible ansible]# cd
預執行;查看有沒有報錯
[root@ansible ~]# ansible-playbook -C /etc/ansible/mariadb.yml
[root@ansible ~]# ansible-playbook /etc/ansible/mariadb.yml
PLAY [cloud] **********************************************************************************************
TASK [Gathering Facts] ************************************************************************************
ok: [192.168.30.26]
ok: [192.168.30.25]
TASK [mariadb : install mariadb] **************************************************************************
ok: [192.168.30.25]
ok: [192.168.30.26]
TASK [mariadb : move config file] *************************************************************************
changed: [192.168.30.25]
changed: [192.168.30.26]
TASK [mariadb : provide a new config file] ****************************************************************
changed: [192.168.30.25]
changed: [192.168.30.26]
TASK [mariadb : reload mariadb] ***************************************************************************
changed: [192.168.30.26]
changed: [192.168.30.25]
TASK [mariadb : create database testdb] *******************************************************************
changed: [192.168.30.25]
changed: [192.168.30.26]
RUNNING HANDLER [mariadb : restart mariadb] ***************************************************************
changed: [192.168.30.25]
changed: [192.168.30.26]
PLAY RECAP ************************************************************************************************
192.168.30.25 : ok=7 changed=5 unreachable=0 failed=0
192.168.30.26 : ok=7 changed=5 unreachable=0 failed=0
- ansible端驗證
[root@ansible ~]# ansible cloud -m shell -a 'mysql -u root -e "show databases;"'
192.168.30.26 | SUCCESS | rc=0 >>
Database
information_schema
aaa
mysql
performance_schema
192.168.30.25 | SUCCESS | rc=0 >>
Database
information_schema
aaa
mysql
performance_schema
client端驗證
[root@client1 ~]# mysql
MariaDB [(none)]> show grants for cloud@'192.168.30.%';
+-----------------------------------------------------------------------------------------------------------------+
| Grants for cloud@192.168.30.% |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'cloud'@'192.168.30.%' IDENTIFIED BY PASSWORD '*676243218923905CF94CB52A3C9D3EB30CE8E20D' |
| GRANT ALL PRIVILEGES ON `aaa`.* TO 'cloud'@'192.168.30.%' |
+-----------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
修改mariadb的密碼(腳本完成以後沒有設置密碼,這裏我是單獨設置的密碼)
[root@client1 ~]# mysql
MariaDB [(none)]> use mysql;
MariaDB [mysql]> update user set password=password('123456789') where user='root';
MariaDB [mysql]> flush privileges;
MariaDB [mysql]> \q
[root@client1 ~]# mysql -u root -p123456789
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>