[ansible] 利用ansible自动化部署进行alisql5.6.32的安装

站在巨人的肩膀上

part.1 host配置

[root@localhost ansible]# cat /etc/ansible/hosts 
[testpc] 
192.168.123.103 ansible-ssh-user=root ansible-ssh-pass=3eno2017

part.2 依赖包

在这里我想完成的是,在没有外网的情况下进行安装,那么我需要得到非常完整的依赖包,那么,我先在有外网的情况下,利用利用yum安装对安装包进行下载,并下载相关的依赖包。

yum install    --downloadonly --downloaddir=path

下面是我在纯净环境下,无外网的情况下需要的依赖包列表。

autoconf-2.69-11.el7.noarch.rpm                perl-File-Temp-0.23.01-3.el7.noarch.rpm
bison-2.7-4.el7.x86_64.rpm                     perl-Filter-1.49-3.el7.x86_64.rpm
bison-devel-2.7-4.el7.x86_64.rpm               perl-Getopt-Long-2.40-2.el7.noarch.rpm
cmake-2.8.12.2-2.el7.x86_64.rpm                perl-Git-1.8.3.1-6.el7_2.1.noarch.rpm
cpp-4.8.5-11.el7.x86_64.rpm                    perl-HTTP-Tiny-0.033-3.el7.noarch.rpm
gcc-4.8.5-11.el7.x86_64.rpm                    perl-libs-5.16.3-291.el7.x86_64.rpm
gcc-c++-4.8.5-11.el7.x86_64.rpm                perl-macros-5.16.3-291.el7.x86_64.rpm
git-1.8.3.1-6.el7_2.1.x86_64.rpm               perl-parent-0.225-244.el7.noarch.rpm
glibc-2.17-157.el7_3.4.x86_64.rpm              perl-PathTools-3.40-5.el7.x86_64.rpm
glibc-common-2.17-157.el7_3.4.x86_64.rpm       perl-Pod-Escapes-1.04-291.el7.noarch.rpm
glibc-devel-2.17-157.el7_3.4.x86_64.rpm        perl-podlators-2.5.1-3.el7.noarch.rpm
glibc-headers-2.17-157.el7_3.4.x86_64.rpm      perl-Pod-Perldoc-3.20-4.el7.noarch.rpm
kernel-headers-3.10.0-514.21.2.el7.x86_64.rpm  perl-Pod-Simple-3.28-4.el7.noarch.rpm
libarchive-3.1.2-10.el7_2.x86_64.rpm           perl-Pod-Usage-1.63-3.el7.noarch.rpm
libgnome-keyring-3.8.0-3.el7.x86_64.rpm        perl-Scalar-List-Utils-1.27-248.el7.x86_64.rpm
libmpc-1.0.1-3.el7.x86_64.rpm                  perl-Socket-2.010-4.el7.x86_64.rpm
libstdc++-devel-4.8.5-11.el7.x86_64.rpm        perl-Storable-2.45-3.el7.x86_64.rpm
m4-1.4.16-10.el7.x86_64.rpm                    perl-TermReadKey-2.30-20.el7.x86_64.rpm
mpfr-3.1.1-4.el7.x86_64.rpm                    perl-Text-ParseWords-3.29-4.el7.noarch.rpm
ncurses-devel-5.9-13.20130511.el7.x86_64.rpm   perl-threads-1.87-4.el7.x86_64.rpm
perl-5.16.3-291.el7.x86_64.rpm                 perl-threads-shared-1.43-6.el7.x86_64.rpm
perl-Carp-1.26-244.el7.noarch.rpm              perl-Time-HiRes-1.9725-3.el7.x86_64.rpm
perl-constant-1.27-2.el7.noarch.rpm            perl-Time-Local-1.2300-2.el7.noarch.rpm
perl-Data-Dumper-2.145-3.el7.x86_64.rpm        rsync-3.0.9-17.el7.x86_64.rpm
perl-Encode-2.51-7.el7.x86_64.rpm              unzip-6.0-16.el7.x86_64.rpm
perl-Error-0.17020-2.el7.noarch.rpm            zlib-1.2.7-17.el7.x86_64.rpm
perl-Exporter-5.68-3.el7.noarch.rpm            zlib-devel-1.2.7-17.el7.x86_64.rpm
perl-File-Path-2.09-2.el7.noarch.rpm

part.3 copy.yml的编写

首先我们需要把我们的alisql和依赖包发送到我们的分机.

- name: sent dep
  hosts: all
  user: root
  gather_facts: false
  tasks:
  - name: sent dep
    copy: src=/tmp/gcc-c++ dest=/tmp/ force=yes
  - name: mkdir all
    shell: mkdir -p /etc/ansible/roles/mysql_install/
  - name: sent alisql
    copy: src=/etc/ansible/roles/mysql_install/files dest=/etc/ansible/roles/mysql_install/ force=yes

在这个yml中,copy模块的src指的是主机上的文件夹目录或者文件,dest指的是分机上的目录,然后完成拷贝工作。

part.4 yum_install.yml的编写

- name: instal yum
  hosts: all
  user: root
  gather_facts: false
  vars:
  - filespath: "/etc/ansible/roles/mysql_install/files/"
  - mysqlpath: "/usr/local/"
  tasks:
  - name: rpm mariadb
    shell: rpm -ev mariadb-libs-5.5.35-3.el7.x86_64 --nodeps || /usr/bin/true
    ignore_errors: yes
  - name: yum_install_local
    shell: cd /tmp/gcc-c++ && yum -y install *.rpm || /usr/bin/true
  - name: shutdown hugepage
    shell: echo never > /sys/kernel/mm/transparent_hugepage/enabled && echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled"
>> /etc/rc.local && chmod +x /etc/rc.d/rc.local && systemctl disable tuned
  - name: unzip AliSQL
    unarchive: src=/etc/ansible/roles/mysql_install/files/AliSQL-5.6.32.zip dest=/usr/local copy=no
  - name: rename
    shell: mv /usr/local/alibaba-AliSQL-cc84a09 /usr/local/mysql
  - name: new group and user
    shell: groupadd mysql && useradd -g mysql mysql || /usr/bin/true
    ignore_errors: yes
  - name: cmake
    shell: cmake                                -DCMAKE_BUILD_TYPE="Release"          -DCMAKE_INSTALL_PREFIX="/usr/local/mysql"  -DWITH
_EMBEDDED_SERVER=0              -DWITH_EXTRA_CHARSETS=all             -DWITH_MYISAM_STORAGE_ENGINE=1        -DWITH_INNOBASE_STORAGE_ENG
INE=1      -DWITH_PARTITION_STORAGE_ENGINE=1     -DWITH_CSV_STORAGE_ENGINE=1           -DWITH_ARCHIVE_STORAGE_ENGINE=1       -DWITH_BLA
CKHOLE_STORAGE_ENGINE=1     -DWITH_FEDERATED_STORAGE_ENGINE=1     -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 /usr/local/mysql
  - name: make and insatll
    shell: make -j4 && make install
  - name: copy mysql configure
    copy: src=/etc/ansible/roles/mysql_install/files/my.cnf dest=/etc/my.cnf force=yes
 name: chmod scripts
    file: path=scripts/mysql_install_db mode=0755
  - name: mkdir tmp
    file: path=/usr/local/mysql/tmp state=directory
  - name: chown all file in dic mysql
    shell: chown -R mysql:mysql /usr/local/mysql
  - name: execute scripts
    shell: scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
  - name: copy scripts
    shell: cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld && chmod 755 /etc/init.d/mysqld
  - name: chkconfig on
    shell: chkconfig mysqld on
  - name: add mysql PATH to profile
    shell: echo "export PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile && source /etc/profile
  - name: start mysqld
    shell: /etc/init.d/mysqld start && /usr/local/mysql/bin/mysql --protocol tcp

上面这个脚本其实就是按着我原来手动安装alisql的步骤改写的,下面附上传送门,方便对照看。centos7---alisql5.6.32的安装
当然我这个脚本到最后只是把mysql这个服务起来,并没有进行密码配置,这些需要使用者自己添加,设置。

part.5 运行结果

运行 copy.yml

[root@localhost ansible---mysql]# ansible-playbook copy.yml 

PLAY [sent dep] ***********************************************************************************************************************

TASK [sent dep] ***********************************************************************************************************************
changed: [192.168.123.103]

TASK [mkdir all] **********************************************************************************************************************
 [WARNING]: Consider using file module with state=directory rather than running mkdir

changed: [192.168.123.103]

TASK [sent alisql] ********************************************************************************************************************
ok: [192.168.123.103]

PLAY RECAP ****************************************************************************************************************************
192.168.123.103            : ok=3    changed=2    unreachable=0    failed=0   

运行 yum_install.yml

[root@localhost ansible---mysql]# ansible-playbook  yum_install.yml 

PLAY [instal yum] **********************************************************************************************************************************************************************************************************************************************

TASK [rpm mariadb] *********************************************************************************************************************************************************************************************************************************************
 [WARNING]: Consider using yum, dnf or zypper module rather than running rpm

changed: [192.168.123.103]

TASK [yum_install_local] ***************************************************************************************************************************************************************************************************************************************
changed: [192.168.123.103]

TASK [shutdown hugepage] ***************************************************************************************************************************************************************************************************************************************
changed: [192.168.123.103]

TASK [unzip AliSQL] ********************************************************************************************************************************************************************************************************************************************
changed: [192.168.123.103]

TASK [rename] **************************************************************************************************************************************************************************************************************************************************
changed: [192.168.123.103]

TASK [new group and user] **************************************************************************************************************************************************************************************************************************************
changed: [192.168.123.103]

TASK [cmake] ***************************************************************************************************************************************************************************************************************************************************
changed: [192.168.123.103]

TASK [make and insatll] ****************************************************************************************************************************************************************************************************************************************
changed: [192.168.123.103]

TASK [copy mysql configure] ************************************************************************************************************************************************************************************************************************************
ok: [192.168.123.103]

TASK [chmod scripts] *******************************************************************************************************************************************************************************************************************************************
changed: [192.168.123.103]

TASK [mkdir tmp] ***********************************************************************************************************************************************************************************************************************************************
changed: [192.168.123.103]

TASK [chown all file in dic mysql] *****************************************************************************************************************************************************************************************************************************
 [WARNING]: Consider using file module with owner rather than running chown

changed: [192.168.123.103]

TASK [execute scripts] *****************************************************************************************************************************************************************************************************************************************
changed: [192.168.123.103]

TASK [copy scripts] ********************************************************************************************************************************************************************************************************************************************
changed: [192.168.123.103]

TASK [chkconfig on] ********************************************************************************************************************************************************************************************************************************************
changed: [192.168.123.103]

TASK [add mysql PATH to profile] *******************************************************************************************************************************************************************************************************************************
changed: [192.168.123.103]

TASK [start mysqld] ********************************************************************************************************************************************************************************************************************************************
changed: [192.168.123.103]

PLAY RECAP *****************************************************************************************************************************************************************************************************************************************************
192.168.123.103            : ok=16   changed=16  unreachable=0    failed=0

part.6 运行结束后

最后检查即可~

你可能感兴趣的:([ansible] 利用ansible自动化部署进行alisql5.6.32的安装)