Ansible-完成mariadb服务搭建

1.roles/mariadb/tasks/main.yml

# mariadb安装后,默认不需要密码,在此也不设置
# 安装数据库
yum -y install mariadb mariadb-server

# 启动数据库并加入开机自启动
systemctl start mariadb
systemctl enable mariadb

# mariadb数据库内创建zrlog、zh库,并授权用户和主机
create database zrlog;
create database zh;

grant all on zrlog.* to zrlog@'%' identified by '123456';
grant all on zh.* to zh@'%' identified by '123456';


flush privileges;

# 上传zrlog、zh 数据库文件至/tmp目录(过程略)
# 导入zrlog、zh 数据库文件至mariadb数据库中
mysql < zrlog.sql
mysql < zh.sql
- name: Install maridb
  yum:
    name: "{{ item }}"
    state: installed
  loop:
      - mariadb
      - mariadb-server

- name: Start mariadb
  systemd:
    name: mariadb
    state: started
    enabled: yes

- name: Create new databases with names 'zrlog' and 'zh'
  mysql_db:
    name: "{{ item }}"
    state: present
  loop:
      - zrlog
      - zh

- name: Create remote user zh and zrlog
  mysql_user:
    name: "{{ item.name }}"
    host: '%'
    password: "123456"
    priv: "{{ item.priv }}"
    state: present
  loop:
    - { name: zh , priv: 'zh.*:ALL' }
    - { name: zrlog , priv: 'zrlog.*:ALL' }

- name: Remote send database_data 'zh.sql and zrlog.sql'
  copy:
    src: "{{ item.src }}"
    dest: "{{ item.dest }}"
  loop:
    - { src: zh.sql.j2 , dest: /tmp/zh.sql }
    - { src: zrlog.sql.j2 , dest: /tmp/zrlog.sql }

- name: Restore database
  mysql_db:
    name: "{{ item.name }}"
    state: import
    target: "{{ item.target }}"
  loop:
    - { name: zh , target: /tmp/zh.sql }
    - { name: zrlog , target: /tmp/zrlog.sql }

你可能感兴趣的:(Ansible-完成mariadb服务搭建)