• 欢迎访问V小白网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入V小白 暂无QQ群
  • 欢迎访问V小白网站,这是第二个滚动条

Ansible的MariaDB部署

其他分类 admin 2019年6月28日 10:36 49 0 个评论

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 ~]#

V小白 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Ansible的MariaDB部署
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到
0个小伙伴在吐槽