• [Ansible实战]-ansible初始化mysql数据库


     

    0.目录结构

    $ tree mysql
    mysql
    ├── files
    │   └── web.sql
    ├── handlers
    │   └── main.yml
    ├── tasks
    │   └── main.yml
    ├── templates
    │   ├── client.cnf.j2
    │   ├── my.cnf.j2
    │   ├── mysql-clients.cnf.j2
    │   └── server.cnf.j2
    └── vars
    

    1.tasks内容

    - name: 01-install
      yum:
        name:  "{{ item }}"
        state: installed
      loop:
        - MySQL-python
        - mariadb
        - mariadb-server
      notify: 
        - remove_user 
        - remove_test
        - set_password 
        - create_user
    
    - name: 02-start
      service:
        name: mariadb
        state: started
    
    - name: 03-copy_conf
      template:
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
      loop:
        - { src: my.cnf.j2,            dest: /etc/my.cnf }
        - { src: client.cnf.j2,        dest: /etc/my.cnf.d/client.cnf }
        - { src: server.cnf.j2,        dest: /etc/my.cnf.d/server.cnf }
        - { src: mysql-clients.cnf.j2, dest: /etc/my.cnf.d/mysql-clients.cnf }
      notify:
        - restart_mysql
    
    - name: 04-copy_sql
      copy: 
        src: web.sql
        dest: /tmp/web.sql
      notify:
        - import_sql
    
    

    2.handlers内容

    - name: remove_user
      mysql_user:
        name: ''
        host_all: yes
        state: absent
    
    - name: remove_test 
      mysql_db: 
        name: test
        state: absent
    
    - name: set_password
      mysql_user:
        name: root
        password: '123456'
    
    - name: create_user
      mysql_user:
        login_user: root
        login_password: '123456'
        name: web
        password: oldboy123
        host: '172.16.1.%'
        priv: '*.*:ALL,GRANT'
        state: present
    
    - name: restart_mysql
      service:
        name: mariadb
        state: restarted 
    
    - name: import_sql
      mysql_db:
        login_user: root
        login_password: '123456'
        name: all
        state: import
        target: /tmp/web.sql
    
    
    老男孩Linux教育欢迎咨询免费试听 联系方式: QQ:526195417
    系统化运维博客
  • 相关阅读:
    Windows核心编程笔记:同步设备I/O与异步设备I/O 200404
    最顶层窗口
    Windows环境变量
    如何使用Beyond Compare比较两个文件夹的差异
    c++生成缩略图
    MD5加密算法
    c++读写注册表
    通过WMI获取网卡MAC地址、硬盘序列号、主板序列号、CPU ID、BIOS序列号
    Hive中HSQL中left semi join和INNER JOIN、LEFT JOIN、RIGHT JOIN、FULL JOIN区别
    postgreSQL格式化时间的函数详解
  • 原文地址:https://www.cnblogs.com/nsthink-666/p/12744666.html
Copyright © 2020-2023  润新知