This is my 13th day of the August Challenge.

The paper

Ansible Automation: LAMP Environment Deployment implements the automatic installation of LAMP basic components through playbook orchestration, and its data layer in the o&M framework also has specific practices. Here we will introduce: Install and initialize mysql-5.6.22 on ansible-Playbook. After that, we only need to start the database and build the database.

Configuration idea

Configuration roadmap for ansible-Playbook:

  1. Use the configuration variable main.yml in Vars to store and install the source code.
  2. Transfer the source file to the source directory on the remote server via copy.yml in Tasks
  3. Install mysql into the installation directory defined in variables by invoking the template mysql_install.sh from install.yml in tasks.
  4. Call the Copy module and install module from main.yml in Tasks;
  5. Use mysql.yml to call playbook: mysql_install for automatic deployment;

The directory structure

The Playbook directory is organized as follows:

  • Files: Stores source files and configuration files that need to be synchronized to a remote server.
  • Handlers: Operations to be performed when resources change. If there is no such directory, do not create it or leave it empty.
  • Meta: Role definition can be left blank.
  • Tasks: Tasks that need to be performed during mysql installation;
  • Templates: a template file used to execute the mysql installation.
  • Vars: variables defined in this installation.
[root@test ansible]# cd /etc/ansible/
[root@test ansible]# mkdir -p roles/mysql_install/{files,handlers,meta,tasks,templates,vars}
[root@test ansible]# tree /etc/ansible├ ─ ─ ansible. CFG ├ ─ ─ hosts ├ ─ ─ mysql. Yml ├ ─ ─ roles │ └ ─ ─ mysql_install │ ├ ─ ─ files │ │ ├ ─ ─ my. The CNF │ │ └ ─ ─ mysql -5.622.. Tar. Gz │ ├ ─ ─ handlers │ ├ ─ ─ meta │ ├ ─ ─ the tasks │ │ ├ ─ ─ copy. Yml │ │ ├ ─ ─ the yml │ │ └ ─ ─ the main, yml │ ├ ─ ─ templates │ │ └ ─ ─ mysql_install. Sh │ └ ─ ─vars│ └ ─ ─ the main ymlCopy the code

The specific implementation

1. Create a mysql role file to invoke mysql_install

[root@test  ansible]# vim mysql.yml
- hosts: test
  remote_user: root
  gather_facts: False
  roles:
    - mysql_install
Copy the code

2. Create a variable file

# Create variable
[root@test ansible]# cd /etc/ansible/roles/mysql_install/vars
vim main.yml
mysql_version: mysql-5.622.
source_dir: /home/ap/src
install_dir: /home/ap/mysql
data_dir: /home/ap/mysql/data
Copy the code

The installation directory and data directory can be customized.

3. Create task files


[root@test ansible]# cd /etc/ansible/roles/mysql_install/tasks
[root@test ansible]# vim copy.yml
# Copy source code to target server
- name: copy mysql source code to client
  copy: src={{mysql_version}}.tar.gz dest={{source_dir}} owner=root group=root
Copy the configuration file to the target server
- name: copy my.cnf to client
  copy: src=my.cnf dest=/etc/my.cnf owner=root group=root
Copy template files to target server
- name: copy mysql install script to client
  template: src=mysql_install.sh dest={{source_dir}} owner=root group=root mode=0775
[root@test ansible]# vim install.yml
Execute the template file to install
- name: install mysql
  shell: bash {{source_dir}}/mysql_install.sh
[root@test ansible]# vim main.yml
# Reference the copy and install modules
- include: copy.yml
- include: install.yml
Copy the code

Note:

  • Copy module to copy directory, need to add recurse, recurse;
  • The copy module copies directories. No directories will be created on the target server.
  • If the copy module copies the file to a directory on the target server, add the dest parameter /home/ap/src/ instead of /home/ap/src. otherwise, ansible will copy the file to SRC instead of the SRC directory.

4. Write a template script

In addition to compiling and installing mysql, this script also initializes the mysql database, modifies the my.cnf configuration file, sets the password, and starts the database.

#! /bin/bash
INSTALL_DIR={{install_dir}}
DATADIR={{data_dir}}
INNODB_DIR=$DATADIR/innodb
VERSION='{{mysql_version}}'
SOURCE_DIR={{source_dir}}
#export LANG=zh_CN.UTF-8

#Source function library.
. /etc/init.d/functions

# camke install mysql5.6. X
install_mysql(){
        #read -p "please input a password for root: " PASSWD
    PASSWD='core2017'
        if[ ! -d $DATADIR ]; then mkdir -p $DATADIR fiif[ ! -d $INNODB_DIR ]; then mkdir -p $INNODB_DIR fi yum install cmake make gcc gcc-c++ ncurses-devel bison-devel -yid mysql &>/dev/null
        if [ $? -ne 0]; then useradd mysql -s /sbin/nologin -M fi#useradd mysql -s /sbin/nologin -M
        #change datadir owner to mysql
        chown -R mysql.mysql $DATADIR
        cd $SOURCE_DIR
        tar xf $VERSION.tar.gz
        cd $VERSION
        cmake . -DCMAKE_INSTALL_PREFIX=$INSTALL_DIR \
        -DMYSQL_DATADIR=$DATADIR \
        -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
        -DDEFAULT_CHARSET=utf8 \
        -DDEFAULT_COLLATION=utf8_general_ci \
        -DWITH_INNOBASE_STORAGE_ENGINE=1 \
        -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
        -DWITH_FEDERATED_STORAGE_ENGINE=1 \
        -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
        -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
        -DWITH_PARTITION_STORAGE_ENGINE=1 \
        -DEXTRA_CHARSETS=all \
        -DMYSQL_TCP_PORT=3306

        make && make install
        if [ $? -ne 0]; then action"install mysql is failed!"  /bin/false
                exit $?
        fi
        sleep 2

        #copy config and start file
        #/bin/cp /usr/local/mysql/support-files/my-small.cnf /etc/my.cnf
        #modify /etc/my.cnf
        sed -i "s:mysqld =:mysqld = $INSTALL_DIR/bin/mysqld_safe:g" /etc/my.cnf
        sed -i "s:mysqladmin =:mysqladmin = $INSTALL_DIR/bin/mysqladmin:g" /etc/my.cnf
        sed -i "s:datadir =:datadir = $DATADIR:g" /etc/my.cnf
        sed -i "s:slow_query_log_file=:slow_query_log_file=$DATADIR:g" /etc/my.cnf
        sed -i "s:log-error=:log-error=$DATADIR:g" /etc/my.cnf
        sed -i "s:innodb_data_home_dir =:innodb_data_home_dir = $INNODB_DIR:g" /etc/my.cnf
        sed -i "s:innodb_log_group_home_dir =:innodb_log_group_home_dir = $INNODB_DIR:g" /etc/my.cnf
        cp $INSTALL_DIR/support-files/mysql.server /etc/init.d/mysqld
        chmod 700 /etc/init.d/mysqld
        #init mysql
        $INSTALL_DIR/scripts/mysql_install_db  --basedir=$INSTALL_DIR --datadir=$DATADIR --user=mysql
        if [ $? -ne 0]; then action"install mysql is failed!"  /bin/false
                exit $?
        fi
        #check mysql
        /etc/init.d/mysqld start
        if [ $? -ne 0]; then action"mysql start is failed!"  /bin/false
                exit $?
        fi
        chkconfig --add mysqld
        chkconfig mysqld on
        $INSTALL_DIR/bin/mysql -e "update mysql.user set password=password('$PASSWD') where host='localhost' and user='root';"
        $INSTALL_DIR/bin/mysql -e Update mysql. User set password=password('$PASSWD') where host='127.0.0.1' and user='root';
        #$INSTALL_DIR/bin/mysql -e "delete from mysql.user where password='';"
        $INSTALL_DIR/bin/mysql -e "flush privileges;"
        #/usr/local/mysql/bin/mysql -e "select version();" >/dev/null 2>&1
        if [ $? -eq 0]; then echo"+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +"
                echo "+------mysql installation complete --------+"
                echo "+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +"
        fi
        #/etc/init.d/mysqld stop
        #add path
        echo "export PATH=$PATH:$INSTALL_DIR/bin" >> /etc/profile
        source /etc/profile
}

install_mysql

Copy the code

5. Customize the installation

You can modify parameters in Vars /main.yml to customize the source directory, installation directory, and data directory as required.

vim main.yml
mysql_version: mysql-5.622.
source_dir: /home/ap/src
install_dir: /home/ap/mysql
data_dir: /home/ap/mysql/data
Copy the code

Deployment of 6.

# Check files
[root@test ansible]# ansible-playbook -C mysql.yml
# to perform the playbook
[root@test ansible]# ansible-playbook mysql.yml
Copy the code