首页 > 技术文章 > 工作记录 > 【文章】Ansible学习笔记之编译安装mysql+nginx+jdk+tomcat
2016
06-21

【文章】Ansible学习笔记之编译安装mysql+nginx+jdk+tomcat

概述:

之前用过salt对服务器环境进行安装配置,最近有时间研究了下ansible,看中的主要原因是client无需安装agent,比较适用于公司得环境,公司得很多服务器上面对于软件的安装管控很严格,所以学习记录一下,以便用于实际生产环境。

安装配置:

一、安装Ansible:

1. yum安装
  先按照EPLE源,安装命令:
  # rpm -ivh http://fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
  # yum install ansible -y
2.免密钥登录设置:
  2.1 主控端生成公钥/私钥
  # ssh-keygen -t rsa -P ”
  2.2 写入信任文件(将/root/.ssh/id_rsa.pub分发到其他服务器,并在所有被控服务器上执行如下指令):
  方法一:(被控的执行)
  # cat id_rsa.pub >> /root/.ssh/authorized_keys
  # chmod 600 /root/.ssh/authorized_keys
  方法二:(主控端执行)
  # ssh-copy-id -i ~/.ssh/id_rsa.pub 目标机器用户名@目标机器IP

二、基本配置使用:

1.基本目录结构
  软件安装完毕后,基本的目录结构如下
  # tree /etc/ansible/
  /etc/ansible/
  |– ansible.cfg   (主配置文件)
  |– hosts         (主机组定义文件)
  `– roles         (角色定义)
2.定义主机组
  # cp /etc/ansible/hosts{,.bak}            (备份原来的主机列表)
  # echo > /etc/ansible/hosts
  # vim /etc/ansible/hosts
    [test]     (主机组名)
    Ansible-minion01   ansible_ssh_port=222   (主机名或IP均可,如果客户机非默认22端口,需要添加ssh端口)
    Ansible-minion02   ansible_ssh_port=222
3. 命令行基本操作:
  场景1:需要在客户机上执行命令,命令如下:
  #test是主机组名称,可以用command模块,-m后是模块名称,-a 后是模块参数
  # ansible test -m command -a ‘uptime’
    Ansible-minion01 | SUCCESS | rc=0 >>
    16:03:56 up 19 min,  2 users,  load average: 0.08, 0.02, 0.01
    Ansible-minion02 | SUCCESS | rc=0 >>
    16:08:14 up 3 min,  3 users,  load average: 0.00, 0.00, 0.00
  场景2:需要执行客户机上的脚本,可以用shell模块,命令如下:
  #ansible test -m shell -a ‘/root/run.sh’
  场景3:脚本在主控端,但需要在客户机上执行,可以用script模块,命令如下:
  #主控端/root/下必须有local.sh脚本。
  #ansible test -m script -a ‘/root/local.sh’
  场景4:文件发布,这种需求很多,比如日常的配置文件更新,程序版本发布等,基本都会用到。
  #ansible test -m copy -a ‘src=/root/test.sh dest=/root/test.sh’
  场景5:想在客户机上用yum安装软件,可以用yum模块,这个用的还是挺多的,比如用ansible去链接低版本的centos时,就乎出现” ansible requires a json module, none found! “的错误,需要远程机安装samplejson包。
  # ansible test -m raw -a ‘yum -y install python-simplejson’
  raw模块是底层ssh的通讯,不依靠python的模块,所以如果碰到低版本的系统,如果command和shell模块无法使用,可以先用这条命令安装完需要的包。
  场景6:重启客户机服务,可以用service模块:
  #ansible webserves -m service -a “name=sshd state=restarted”
  场景7:添加用户
  # ansible webserves -m user -a “name=foo password=<crypted passwordhere>”
  场景8:下载git包
  # ansible web -m git -a “repo=git://foo.example.org/repo.git
  dest=/srv/myapp version=HEAD”
  场景9:启动服务
  # ansible web -m service -a “name=httpd state=started”
  场景10:并行执行10个重起命令
  # ansible lb -a “/sbin/reboot” -f 10
  场景11:查看远程主机的全部系统信息
  # ansible all -m setup

三、playbook安装实例(JDK+tomcat+mysql+nginx):

先上完成后的总目录结构:
[root@Ansible-master ansible]# tree
.
├── ansible.cfg
├── group_vars
├── hosts
├── roles
│   ├── JDK
│   │   ├── default
│   │   ├── files
│   │   │   └── jdk-1.7.0_79-linux-x64.gz
│   │   ├── handlers
│   │   ├── meta
│   │   ├── tasks
│   │   │   └── main.yml
│   │   ├── templates
│   │   └── vars
│   │       └── main.yml
│   ├── mysql
│   │   ├── default
│   │   ├── files
│   │   │   └── mysql-5.5.42.tar.gz
│   │   ├── handlers
│   │   │   └── main.yml
│   │   ├── meta
│   │   ├── tasks
│   │   │   └── main.yml
│   │   ├── templates
│   │   │   └── my.cnf.j2
│   │   └── vars
│   │       └── main.yml
│   ├── nginx
│   │   ├── default
│   │   ├── files
│   │   │   └── nginx-1.8.0.tar.gz
│   │   ├── handlers
│   │   │   └── main.yml
│   │   ├── meta
│   │   ├── tasks
│   │   │   ├── main.yml
│   │   │   └── main.yml.bak
│   │   ├── templates
│   │   │   ├── nginx
│   │   │   ├── nginx.conf
│   │   │   └── test.conf
│   │   └── vars
│   │       └── main.yml
│   └── tomcat
│       ├── default
│       ├── files
│       │   └── apache-tomcat-7.0.68.tar.gz
│       ├── handlers
│       ├── meta
│       ├── tasks
│       │   └── main.yml
│       ├── templates
│       │   └── tomcat.j2
│       └── vars
│           └── main.yml
└── webserver.yml
接下来说下具体操作步骤:
# cd /etc/ansible/
# mkdir group_vars
# touch webserver.yml
# cd roles
# mkdir -p  {JDK,tomcat,mysql,nginx}/{meta,vars,files,handlers,tasks,templates,default}
相关目录说明:
group_vars:这里面定义的变量,对全局role都生效
webserver.yml:总调文件
vars:这里面定义的变量,只对当前role有作用
role:在ansible可以将任务等模块化,方便重复使用
tasks:任务就是按配置文件的定义的去执行的操作。
handlers:当被操作的主机配置发生改变时要进行的操作,这里需要通知它,然后handlers定义的动作会被执行。
file:主要是存储一些服务器需要的系统配置文件
meta:定义role和role之间的依赖关系
templates:存放模板,对应task里面的模块template
default目录:为当前角色设定默认变量时使用此目录
注:以上目录非必须都创建,根据自己的需要创建即可,这里创建只是为了说明各个目录的作用。以下会对每个服务的安装进行解读,包括配置文件中一些参数的说明,只是在第一次出现时候说明,后面的不再叙述作用。
3-1 :nginx编译安装配置:
nginx安装目录结构:
[root@Ansible-master roles]# tree nginx/
nginx/
├── default
├── files
│   └── nginx-1.8.0.tar.gz  (源码包)
├── handlers
│   └── main.yml
├── meta
├── tasks
│   ├── main.yml
├── templates
│   ├── nginx       (启动脚本)
│   ├── nginx.conf  (主配置文件)
│   └── test.conf   (主机配置文件)
└── vars
    └── main.yml
nginx配置文件的具体内容:
1.[root@Ansible-master nginx]# cat tasks/main.yml
- name: Copy nginx_tar_gz to client
  copy: src=nginx-{{ nginx_version }}.tar.gz dest=/tmp/nginx-{{ nginx_version }}.tar.gz
# {{ nginx_version }}是在vars里定义的软件版本变量,这样的好处是,如果以后想安装不同版本的服务,只需要更改files目录里面的软件包版本,
# 然后更新vars里变量对应的变量即可。
- name: Install nginx package
  yum: name={{ item }} state=present     #(循环调用with_items里的软件包,采用yum方式安装)
  with_items:
    – zlib
    – zlib-devel
    – openssl
    – openssl-devel
    – pcre-devel
  ignore_errors: True
#(因为ansible执行时候如果一条命令报错,后面的命令不会再执行,这个参数的作用是忽略报错继续后面的安装)
- name: Add group “nginx”
  group: name=nginx               #(添加nginx用户组)
- name: Add user “nginx”
  user: name=nginx group=nginx home=/home/nginx shell=/sbin/nologin
#(添加nginx用户指定所属组,家目录,及shell不允许登录)
- name: tar nginx.tar.gz
  unarchive: src=/tmp/nginx-{{ nginx_version }}.tar.gz dest=/tmp/nginx-{{ nginx_version }} copy=no
 #(此参数用于将宿主机上的软件包传递到目标目录直接解压,copy=no 的意思是不负责软件包到目标机器上)
- name: configure nginx
  command: chdir=/tmp/nginx-{{ nginx_version }}  {{ configure  }}
#(此参数用于执行命令,chdir=是切换目录的意思,{{configure}}是vars定义的编译时候的参数变量,command只允许运行一条命令,不能使用shell中的管道或者&&等命令符)
- name: make nginx
  command: chdir=/tmp/nginx-{{ nginx_version }}  make
- name: make install nginx
  command: chdir=/tmp/nginx-{{ nginx_version }}  make install
- name: create vhost directory
  file: dest=/opt/nginx-{{ nginx_version }}/conf/vhost/ state=directory
  #(此参数是定义目录或者文件的属性,此处为创建一个目录)
- name: Copy host_config to client
  template: src=test.conf dest=/opt/nginx-{{ nginx_version }}/conf/vhost/test.conf mode=755
  notify: restart vhost_nginx
  #(将templates目录下面的test.conf主机模板拷贝到目标主机上,并且指定,当配置文件有改动时候重启nginx服务)
- name: Copy nginx_config to client
  template: src=nginx.conf dest=/opt/nginx-1.8.0/conf/nginx.conf owner=nginx group=nginx
  notify: restart conf_nginx
  #(此处也是重启nginx服务,之所以写不同的名称,是因为相同的名称只有第一个配置生效,即同时修改了两个配置文件,只有上面的会重启服务,下面的不会)
- name: copy nginx_script to client
  template: src=nginx dest=/etc/init.d/nginx mode=0755
- name: Start nginx service
  service: name=nginx state=started enabled=yes     #(启动服务,并设置将服务添加到启动服务里面设置开机自起)
2.[root@Ansible-master nginx]# cat vars/main.yml (定义上面文件中引用的变量)
nginx_version: 1.8.0
configure: ./configure –prefix=/opt/nginx-{{ nginx_version }}  –user=nginx  –group=nginx  –with-http_ssl_module  –with-http_gzip_static_module  –with-poll_module  –with-file-aio  –with-http_realip_module  –with-http_addition_module –with-http_random_index_module   –with-pcre   –with-http_stub_status_module
3.[root@Ansible-master nginx]# cat handlers/main.yml #(定义配置文件修改后触发的动作)
- name: restart vhost_nginx
  service: name=nginx state=restarted
- name: restart conf_nginx
  service: name=nginx state=restarted
4.[root@Ansible-master nginx]#cat templates/nginx.conf
#(在模板文件里面也是可以调用vars里面的参数的,下面的实例就用到了)
user  nginx;
worker_processes 1;
error_log  logs/nginx_error.log  notice;
pid        /var/run/nginx-{{ nginx_version }}/nginx.pid;
worker_rlimit_nofile 65535;
events
     {
              use epoll;
              worker_connections 65535;
      }
http
     {
              include       mime.types;
              default_type  application/octet-stream;
              charset  utf-8;
              server_names_hash_bucket_size 128;
              client_header_buffer_size 32k;
              large_client_header_buffers 4 32k;
              client_max_body_size 128m;
              sendfile on;
              tcp_nopush     on;
              keepalive_timeout 60;
              tcp_nodelay on;
              server_tokens off;
              client_body_buffer_size  512k;
              gzip on;
              gzip_min_length  1k;
              gzip_buffers     4 16k;
              gzip_http_version 1.1;
              gzip_comp_level 2;
              gzip_types      text/plain application/x-javascript text/css application/xml;
              gzip_vary on;
        log_format  main  ‘$remote_addr – $remote_user [$time_local] “$request” ‘
                              ‘$status $body_bytes_sent “$http_referer” ‘
                                ‘”$http_user_agent” “$http_x_forwarded_for” “$host”‘ ;
              include vhost/*.conf;
       }
5.[root@Ansible-master nginx]# cat templates/nginx
#!/bin/sh
#
# nginx – this script starts and stops the nginx daemon
#
# chkconfig:   – 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ “$NETWORKING” = “no” ] && exit 0
nginx=”/opt/nginx-{{ nginx_version }}/sbin/nginx”
prog=$(basename $nginx)
NGINX_CONF_FILE=”/opt/nginx-{{ nginx_version }}/conf/nginx.conf”
lockfile=/var/lock/subsys/nginx
make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep “configure arguments:” | sed ‘s/[^*]*–user=\([^ ]*\).*/\1/g’ -`
   if [ -z “`grep $user /etc/passwd`” ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep ‘configure arguments:’`
   for opt in $options; do
       if [ `echo $opt | grep ‘.*-temp-path’` ]; then
           value=`echo $opt | cut -d “=” -f 2`
           if [ ! -d “$value” ]; then
               # echo “creating” $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $”Starting $prog: “
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
stop() {
    echo -n $”Stopping $prog: “
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
reload() {
    configtest || return $?
    echo -n $”Reloading $prog: “
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
force_reload() {
    restart
}
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
    status $prog
}
rh_status_q() {
    rh_status >/dev/null 2>&1
}
case “$1″ in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $”Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}”
        exit 2
esac
6.[root@Ansible-master nginx]# cat templates/test.conf #(主机配置文件,所有访问全部代理到本机后端tomcat上)
server {
    listen 80;
    server_name www.test.com;
    access_log logs/www.test.access.log;
    error_log  logs/www.test.error.log;
    location /
    {
        proxy_set_header        Host  $host;
        proxy_set_header        X-Real-IP  $remote_addr;
        proxy_set_header        REMOTE-HOST $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        #rewrite_log             on;
        proxy_pass              http://127.0.0.1:8080;
    }
}
3-2: JDK安装配置:
[root@Ansible-master roles]# tree JDK/
JDK/
├── default
├── files
│   └── jdk-1.7.0_79-linux-x64.gz  (源码包)
├── handlers
├── meta
├── tasks
│   └── main.yml
├── templates
└── vars
    └── main.yml
1.[root@Ansible-master JDK]# cat tasks/main.yml
- name: Copy jdk_tar_gz to client
  unarchive: src=jdk-{{ jdk_version }}-linux-x64.gz  dest=/opt/
- name: Install jdk
  file: path=/opt/jdk{{ jdk_version }}/ owner=root group=root recurse=yes
- name: Configure jdk
  shell: /bin/echo {{ item }}  >> /etc/profile && source /etc/profile
  #(shell和command的作用一样,不同的是可以使用管道符号和&&,可以运行多条命令)
  with_items:
   – export JAVA_HOME=/opt/jdk{{ jdk_version }}
   – export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
   – export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
2.[root@Ansible-master JDK]# cat vars/main.yml
  jdk_version: 1.7.0_79
3-3: Tomcat安装配置:
[root@Ansible-master tomcat]# tree
.
├── default
├── files
│    └── apache-tomcat-7.0.68.tar.gz (源码包)
├── handlers
├── meta
├── tasks
│    └── main.yml
├── templates
│    └── tomcat.j2  (启动脚本)
└── vars
    └── main.yml
1.[root@Ansible-master tomcat]# cat tasks/main.yml
- name: Add group
  group: name=www
- name: Add user
  user: name=www group=www home=/home/www
- name: copy tomcat_tar_gz to client
  unarchive: src=apache-tomcat-{{ tomcat_version }}.tar.gz dest=/opt/
- name: Change ownership of Tomcat installation
  file: path=/opt/apache-tomcat-{{ tomcat_version }} owner=www group=www state=directory recurse=yes #(recurse=yes的意思是指定目录下面的所有文件和目录都赋予指定的权限或属主)
- name: Install Tomcat init script
  template: src=tomcat.j2 dest=/etc/init.d/tomcat mode=0755
  #(因为模板采用的是jinja2,所以以.j2为后缀更能表明这个模板的性质,但是经测试不写也不影响使用)
- name: Start Tomcat service
  service: name=tomcat state=started enabled=yes
2.[root@Ansible-master tomcat]# cat vars/main.yml
tomcat_version: 7.0.68
tomcat_root: /opt/apache-tomcat-{{ tomcat_version }}
3.[root@Ansible-master tomcat]cat templates/tomcat.j2
#!/bin/bash
#
# chkconfig: – 95 15
# description: Tomcat start/stop/status script
#Location of JAVA_HOME (bin files)
export JAVA_HOME=/opt/jdk1.7.0_79/
#Add Java binary files to PATH
export PATH=$JAVA_HOME/bin:$PATH
#CATALINA_HOME is the location of the configuration files of this instance of Tomcat
CATALINA_HOME={{ tomcat_root }}
#TOMCAT_USER is the default user of tomcat
TOMCAT_USER=www
#TOMCAT_USAGE is the message if this script is called without any options
TOMCAT_USAGE=”Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}”
#SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
SHUTDOWN_WAIT=20
tomcat_pid() {
        echo `ps -ef | grep $CATALINA_HOME | grep -v grep | tr -s ” “|cut -d” ” -f2`
}
start() {
  pid=$(tomcat_pid)
  if [ -n “$pid” ];then
    echo -e “\e[00;31mTomcat is already running (pid: $pid)\e[00m”
  else
    echo -e “\e[00;32mStarting tomcat\e[00m”
    if [ `user_exists $TOMCAT_USER` = “1” ];then
      su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh
    else
      $CATALINA_HOME/bin/startup.sh
    fi
    status
  fi
  return 0
}
status(){
  pid=$(tomcat_pid)
  if [ -n “$pid” ];then
    echo -e “\e[00;32mTomcat is running with pid: $pid\e[00m”
  else
    echo -e “\e[00;31mTomcat is not running\e[00m”
  fi
}
stop() {
  pid=$(tomcat_pid)
  if [ -n “$pid” ];then
    echo -e “\e[00;31mStoping Tomcat\e[00m”
        $CATALINA_HOME/bin/shutdown.sh
    let kwait=$SHUTDOWN_WAIT
    count=0;
    until [ `ps -p $pid | grep -c $pid` = ‘0’ ] || [ $count -gt $kwait ]
    do
      echo -n -e “\e[00;31mwaiting for processes to exit\e[00m\n”;
      sleep 1
      let count=$count+1;
    done
    if [ $count -gt $kwait ];then
      echo -n -e “\n\e[00;31mkilling processes which didn’t stop after $SHUTDOWN_WAIT seconds\e[00m”
      kill -9 $pid
    fi
  else
    echo -e “\e[00;31mTomcat is not running\e[00m”
  fi
  return 0
}
user_exists(){
  if id -u $1 >/dev/null 2>&1; then
    echo “1”
  else
    echo “0”
  fi
}
case $1 in
        start)
          start
        ;;
        stop)
          stop
        ;;
        restart)
          stop
          start
        ;;
        status)
    status
        ;;
        *)
    echo -e $TOMCAT_USAGE
        ;;
esac
exit 0
3-4 Mysql安装配置:
mysql目录结构:
[root@Ansible-master roles]# tree mysql/
mysql/
├── default
├── files
│   └── mysql-5.5.42.tar.gz (源码包)
├── handlers
│   └── main.yml
├── meta
├── tasks
│   └── main.yml
├── templates
│   └── my.cnf.j2   (主配置文件)
└── vars
    └── main.yml
1.[root@Ansible-master mysql]# cat tasks/main.yml
- name: copy mysql_tar_gz to client
  unarchive: src=mysql-{{ mysql_version }}.tar.gz dest=/tmp
- name: Add mysql group
  group: name=mysql
- name: Add mysql user
  user: name=mysql group=mysql shell=/sbin/nologin home=/home/mysql
- name:  Install mysql package
  yum: name={{ item }} state=present
  with_items:
    – zlib
    – zlib-devel
    – cmake
    – gcc
    – ncurses-devel
    – libxml2-devel
    – libtool-devel
    – gcc-c++
    – autoconf
    – automake
    – bison-devel
    – zlib-devel
  ignore_errors: True
- name: Install mysql
  shell: chdir=/tmp/mysql-{{ mysql_version }} {{ cmake }} && make && make install
- name: Change ownership of mysql installation
  file: path=/usr/local/mysql-{{ mysql_version }}  owner=mysql group=mysql state=directory recurse=yes
- name: Change ownership of mysql datadir
  file: path=/opt/mysql-{{ mysql_version }}  owner=mysql group=mysql state=directory recurse=yes
- name: Copy my.cnf to client
  template: src=my.cnf.j2 dest=/etc/my.cnf owner=mysql group=mysql
  notify: restart mysqld
- name: Create mysql commond soft link
  file: src=/usr/local/mysql-{{ mysql_version }}/bin/* dest=/usr/bin/* state=link force=yes
- name: Initialization mysql databases
  shell: /usr/local/mysql-{{ mysql_version }}/scripts/mysql_install_db  –basedir=/usr/local/mysql-{{ mysql_version }} –ldata=/opt/mysql-{{ mysql_version }} –user=mysql
- name: Install Tomcat init script
  shell: /bin/cp /usr/local/mysql-{{ mysql_version }}/support-files/mysql.server /etc/init.d/mysqld && chmod 755 /etc/init.d/mysqld
- name: start mysqld service
  service: name=mysqld state=started enabled=yes
- name: Enhance database security
  shell: /usr/bin/mysql {{ item }}
  with_items:
    – -e “update mysql.user set password=password(‘{{ passwd }}’) where host=’localhost’ and user=’root';”
    – -e “update mysql.user set password=password(‘{{ passwd }}’) where host=’127.0.0.1′ and user=’root';”
    – -e “delete from mysql.user where password=”;”
    – -e “flush privileges;”
2.[root@Ansible-master mysql]# cat templates/my.cnf.j2
[client]
port = 3306
socket = /opt/mysql-{{ mysql_version }}/mysqld.sock
[mysqld]
port = 3306
socket = /opt/mysql-{{ mysql_version }}/mysqld.sock
back_log = 50
max_connections = 100
max_connect_errors = 10
table_open_cache = 2048
max_allowed_packet = 16M
binlog_cache_size = 1M
max_heap_table_size = 64M
read_buffer_size = 2M
read_rnd_buffer_size = 16M
sort_buffer_size = 8M
join_buffer_size = 8M
thread_cache_size = 8
thread_concurrency = 8
query_cache_size = 64M
query_cache_limit = 2M
ft_min_word_len = 4
default-storage-engine = MYISAM
thread_stack = 192K
transaction_isolation = REPEATABLE-READ
tmp_table_size = 64M
log-bin=mysql-bin
binlog_format=mixed
slow_query_log
long_query_time = 2
server-id = 1
key_buffer_size = 32M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 2G
innodb_data_file_path = ibdata1:10M:autoextend
innodb_write_io_threads = 8
innodb_read_io_threads = 8
innodb_thread_concurrency = 16
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 8M
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 512M
sort_buffer_size = 512M
read_buffer = 8M
write_buffer = 8M
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
open-files-limit = 8192
3.[root@Ansible-master mysql]# cat handlers/main.yml
- name: restart mysqld
  service: name=mysqld state=restarted
4.[root@Ansible-master mysql]# cat vars/main.yml
mysql_version: 5.5.42
cmake: cmake  -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-{{ mysql_version }}   -DMYSQL_DATADIR=/opt/mysql-{{ mysql_version }}    -DMYSQL_UNIX_ADDR=/opt/mysql-{{ mysql_version }}/mysqld.sock   -DWITH_MYISAM_STORAGE_ENGINE=1   -DWITH_INNOBASE_STORAGE_ENGINE=1   -DWITHOUT_PARTITION_STORAGE_ENGINE=1   -DWITH_MEMORY_STORAGE_ENGINE=1   -DWITH_READLINE=1   -DENABLED_LOCAL_INFILE=1   -DMYSQL_USER=mysql  -DMYSQL_TCP_PORT=3306 -DEXTRA_CHARSETS=all -DENABLE_PROFILING=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_DEBUG=0
passwd: grd5@Dfv20o
3-5 总调文件:
[root@Ansible-master ansible]# cat webserver.yml
- hosts: test
  remote_user: root
  roles:
    – nginx
    – JDK
    – tomcat
    – mysql
实验结果:
执行下面命令,第一条是验证语法是否有错误,如果出现下面的文件表示没有问题,然后执行服务的下发和安装
[root@Ansible-master ansible]# ansible-playbook /etc/ansible/webserver.yml –syntax-check
playbook: /etc/ansible/webserver.yml
[root@Ansible-master ansible]# ansible-playbook /etc/ansible/webserver.yml
如果没有报错,则执行完毕,登录目标服务器可以查看安装的服务是否正常:
[root@Ansible-minion01 opt]# ll
total 16
drwxr-xr-x.  9 www   www   4096 Jun 20 16:30 apache-tomcat-7.0.68
drwxr-xr-x.  9 root  root  4096 Apr 11  2015 jdk1.7.0_79
drwxr-xr-x.  5 mysql mysql 4096 Jun 21 12:57 mysql-5.5.42
drwxr-xr-x. 11 root  root  4096 Jun 20 10:39 nginx-1.8.0
【文章】Ansible学习笔记之编译安装mysql+nginx+jdk+tomcat - 第1张  |
最后编辑:
作者:妙手空空
这个作者貌似有点懒,什么都没有留下。

留下一个回复

你的email不会被公开。