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
上面只是供參考,有些細節 不懂可以留言,請勿轉載