ansible 安装mysql --playbook

下面先定义MySQL的vars

---
# vars file for mysql
 user: "mysql"
 group: "mysql"
 Boot_Software: "boost_1_59_0"
 SoftwareName: "mysql-5.7.22"
 SoftwareDir: "/usr/local/software"
 SoftwareInstallDir: "/usr/local/application"
 MySQl_Data: "/data/sqldata"
 Conf_Name: "my.cnf"
 Conf_Dir: "/etc"
 Log_Dir: "/var/log/mysql"
 

第二步,编写tasks

 

---
# tasks file for mysql
 - name: groupadd mysql   #创建组,存在则忽略,group模块   - name:说明
   group:
     name: "{{ group }}"
     gid: 888

 - name: useradd mysql   #创建用户,存在则忽略,user模块
   user:
     name: "{{ user }}"
     group: "{{ group }}"
     uid: 888
     createhome: no
     shell: /sbin/nologin
 - name: Install Software
   yum: name={{ item }} state=latest
   with_items:
    - cmake
    - make 
    - bison 
    - ncurses 
    - ncurses-devel
 - name: Copy MySQL file
   copy: src="{{ item }}.tar.gz" dest="{{ SoftwareDir }}/{{ item }}.tar.gz" owner=root group=root
   with_items:
    - "{{ SoftwareName }}"
    - "{{ Boot_Software }}"
 - name: Unzip NYsql.tar.gz
   unarchive: src="{{ SoftwareDir }}/{{ item }}.tar.gz" dest={{ SoftwareDir }} remote_src=yes
   with_items:
    - "{{ SoftwareName }}"
    - "{{ Boot_Software }}"
 - name: Make Mysql Data Dir
   file: path={{ item }} state=directory mode=0755
   with_items:
    - "{{ MySQl_Data }}"
    - "{{ Log_Dir }}"
 - name: ./configure
   shell: cd {{ SoftwareDir }}/{{ SoftwareName }}; cmake . -DCMAKE_INSTALL_PREFIX={{ SoftwareInstallDir }}/{{ SoftwareName }} -DMYSQL_DATADIR={{ MySQl_Data }} -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DWITH_BOOST=../boost_1_59_0/;make -j {{ ansible_processor_vcpus }} && make install
 - name: Init Mysql
   shell: "{{ SoftwareInstallDir }}/{{ SoftwareName }}/bin/mysqld --initialize-insecure --user={{ user }} --basedir={{ SoftwareInstallDir }}/{{ SoftwareName }} --datadir={{ MySQl_Data }}"
   when: result is success
   ignore_errors: True 
 - name: Copy Mysql Start Shell 
   copy: src={{ SoftwareInstallDir }}/{{ SoftwareName }}/support-files/mysql.server dest=/etc/init.d/mysqld remote_src=yes owner=root group=root mode=0755
 - name: Set Mysql User Dir  Privileges
   file: path={{ item }} owner={{ user }} group={{ group }}  recurse=yes mode=0755
   with_items:
    - "{{ MySQl_Data }}"
    - "{{ SoftwareInstallDir }}/{{ SoftwareName }}"
    - "{{ Log_Dir }}"
 - name: Copy  Mysql Config
   template: src={{ Conf_Name }} dest={{ Conf_Dir }}/{{ Conf_Name }} owner=root group=root mode=0644
 - name: Make Mysql Link
   file: src={{ SoftwareInstallDir }}/{{ SoftwareName }}/bin/{{ item }} dest=/usr/bin/{{ item }} state=link force=yes
   with_items:
    - "mysql"
    - "mysqld"
    - "mysqlbinlog"
    - "mysqldump"
    - "mysqld_safe"
 - name: Start Mysql Service
   service: name=mysqld enabled=yes state=started

上面只是供参考,有些细节 不懂可以留言,请勿转载

你可能感兴趣的:(自动化运维)