使用playbook实现一键部署mysql数据库

环境

主机名安装服务wanlan
db01mariadb10.0.0.51172.16.1.51

流程分析

1.安装ansible
2.优化ansible
3.推送公钥
4.开启防火墙
5.开启80 443 873 nfs等端口和服务白名单
6.关闭selinux
7.创建同一的用户
	1.安装数据库
	2.启动数据库
	3.创建数据库用户
	4.创建数据库

主机清单

[ ~]# vim /etc/ansible/hosts
[db_group]
db01 ansible_ssh_host=172.16.1.51 asible_ssh_user=root ansible_ssh_port=22

导出数据库数据

1.备份db01上的数据库
mysqldump -uroot -p‘1‘ -A wp > /root/wp.sql

2.将web01上备份的数据库拷贝至m01服务器上
scp wp.sql  :/root/ansible/lb/

yml

- hosts: all
  tasks:
    - name: install mariadb
      yum:
        name: mariadb-server
        state: present
      when: ansible_fqdn is match ‘lb*‘
        
    - name: start mariadb-server
      systemd:
        name: mariadb-server
        state: started
        enabled: yes
      when: ansible_fqdn is match ‘lb*‘

    - name: grant mysql user
      mysql_user:
      	#login_host: "localhost"
      	#login_user: "root"
     	#login_password: "123"
     	login_port: "3306"
      	name: "ty"
      	password: "123"
      	host: "%"
      	priv: "*.*:ALL,GRANT"
      	state: "present"
      when: ansible_fqdn is match ‘lb*‘

    - name: create a database
      mysql_db:
        #login_host: "127.0.0.1"
        #login_user: "root"
        #login_password: "123"
        login_port: "3306"
        name: "wp"
        encoding: "utf8"
        state: "present"   
      when: ansible_fqdn is match ‘lb*‘
        
    - name: copy wp.sql
      copy:
        src: "/root/ansible/lb/wp.sql"
        dest: "/root/"
      when: ansible_fqdn is match ‘lb*‘

    - name: import a database
      mysql_db:
        login_host: "127.0.0.1"
        login_user: "root"
        login_port: "3306"
        name: "wp"
        target: "/root/wp.sql"
        state: "import"
      when: ansible_fqdn is match ‘lb*‘

执行

1.执行base.yml
[ ~]# ansible-playbook ansible/base.yml 

2.执行rsync.yml
[ ~]# ansible-playbook ansible/nfs/nfs.yml -i /root/ansible/lb/hosts

相关推荐