1. 安装Ansible工具
- 安装Ansible工具
[root@test01 ~]# cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core)
[root@test01 ~]# yum install ansible -y
- 配置Ansible工具
[root@test01 ~]# cat /etc/ansible/hosts
[mariadb]
192.168.1.31
[mariadb:vars]
ansible_ssh_pass='123456'
# 修改两个参数
[root@test01 ~]# vim /etc/ansible/ansible.cfg
gathering = explicit
host_key_checking = False
- 测试Ansible工具
[root@test01 ~]# ansible mariadb -m ping -o
192.168.1.31 | SUCCESS => {"changed": false, "ping": "pong"}
2. 编写业务roles
- 创建目录结构
[root@test01 ~]# mkdir -p /etc/ansible/roles/mariadb/{mkdir,tasks,handlers,templates,files,vars,defaults,meta}
[root@test01 ~]# tree /etc/ansible/roles/
/etc/ansible/roles/
└── mariadb
├── defaults
├── files
│ └── test.sql
├── handlers
│ └── main.yaml
├── meta
├── mkdir
├── tasks
│ └── main.yaml
├── templates
└── vars
└── main.yaml
9 directories, 4 files
[root@test01 ~]#
- 各个目录的内容
[root@test01 ~]# cat /etc/ansible/site.yaml
---
- hosts: mariadb
roles:
- { role: mariadb, tags: mariadb }
...
[root@test01 ~]# cat /etc/ansible/roles/mariadb/tasks/main.yaml
---
- name: Install MariaDB Package
yum: name={{ item }} state=present
with_items:
- mariadb-server
- mariadb
- MySQL-python
- name: Start MariaDB Service
service: name=mariadb state=started enabled=yes
- name: Copy MariaDB Dump.sql
copy: src=test.sql dest=/tmp/ansible_test.sql
- name: Import MariaDB Databases
mysql_db: name={{ dbname }} state=import target=/tmp/ansible_test.sql
notify:
- remove ansible_test.sql
- name: Create MariaDB User
mysql_user: name={{ dbuser }} password={{ dbpasswd }} priv={{ dbname }}.*:ALL host='%' state=present
#mysql_user: name={{ dbuser }} password={{ dbpasswd }} priv=*.*:ALL host='%' state=present
#- name: Restart MariaDB Service
# template: src=my.cnf.j2 dest=/etc/my.cnf
# notify: restart mariadb
# - restart mariadb
...
[root@test01 ~]# cat /etc/ansible/roles/mariadb/handlers/main.yaml
---
- name: restart mariadb
service: name=mariadb state=restarted
- name: remove ansible_test.sql
file: path=/tmp/ansible_test.sql state=absent
...
[root@test01 ~]# cat /etc/ansible/roles/mariadb/vars/main.yaml
---
dbname: ansible_db_test
dbuser: ansible_db_user
dbpasswd: ansible_db_password
...
数据库备份命令:
mysqldump -u ansible_db_user -h 192.168.1.31 -pansible_db_password ansible_db_test > test.sql
3. 部署测试
[root@test01 ~]# ansible-playbook /etc/ansible/site.yaml --tags mariadb
PLAY [mariadb] ************************************************************************************************************************************************************************************
TASK [mariadb : Install MariaDB Package] **********************************************************************************************************************************************************
changed: [192.168.1.31] => (item=[u'mariadb-server', u'mariadb', u'MySQL-python'])
TASK [mariadb : Start MariaDB Service] ************************************************************************************************************************************************************
changed: [192.168.1.31]
TASK [mariadb : Copy MariaDB Dump.sql] ************************************************************************************************************************************************************
changed: [192.168.1.31]
TASK [mariadb : Import MariaDB Databases] *********************************************************************************************************************************************************
changed: [192.168.1.31]
TASK [mariadb : Create MariaDB User] **************************************************************************************************************************************************************
changed: [192.168.1.31]
RUNNING HANDLER [mariadb : remove ansible_test.sql] ***********************************************************************************************************************************************
changed: [192.168.1.31]
PLAY RECAP ****************************************************************************************************************************************************************************************
192.168.1.31 : ok=6 changed=6 unreachable=0 failed=0
[root@test01 ~]#