第一部分:安装apache

一、准备工作

1、下载httpd、php、mysql 、apr、apr-util、libiconv 本次使用的版本如下:

httpd 2.4.17、php 5.6.15、mysql 5.6.19(二进制)、apr 1.5.2、apr-util 1.5.4、libiconv 1.14

2、在ansible服务端开始安装

编译安装apr

#cd /usr/local/src

#tar xf apr-1.5.2.tar.gz

#cd apr-1.5.2

#./configure --prefix=/usr/local/apr  --如果提示如下错误(不知道忽略这种错误会如何,没测试):

error info:rm: cannot remove `libtoolT': No such file or directory

解决方法:

#vim configure --找到$RM "$cfgfile" 这行, 将其注释或者删除

#$RM "$cfgfile" ,然后重新执行configure 就不会报错了

#make && make install

编译安装apr-util

#cd /usr/local/src

#tar xf apr-util-1.5.4.tar.gz

#cd apr-util-1.5.4

#./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/

#make && make install

编译安装libiconv

#tar xf libiconv-1.14.tar.gz

#cd libiconv-1.14

#./configure --prefix=/usr/local/

#make && make install

然后拷贝libiconv的so文件到一个目录

#mkdir /usr/local/libiconv

#cd /usr/local/libiconv

#cp /usr/local/lib/libiconv.la ./

#cp /usr/local/lib/libiconv.so.2.5.1 ./

#cp /usr/local/lib/preloadable_libiconv.so ./

创建软连接

#ln -sv libiconv.so.2.5.1 libiconv.so

#ln -sv libiconv.so.2.5.1 libiconv.so.2

然后打包:

#tar -zcf libiconv.tar.gz ./*

#mv libiconv.tar.gz /tmp

安装依赖包:

#yum install pcre pcre-devel -y

编译安装httpd

#tar xf httpd-2.4.17.tar.gz

#cd httpd-2.4.17

#./configure --prefix=/usr/local/apache  --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/

#make

#make install

在httpd.conf配置文件中添加pid文件,找到# least PidFile,添加其下:

PidFile  "/var/run/httpd.pid"

User www     --修改为www

Group www    --修改为www

为httpd提供SysV服务脚本/etc/rc.d/init.d/httpd,如下:

#!/bin/bash

# httpd        Startup script for the Apache HTTP Server

# chkconfig: - 85 15

# description: Apache is a World Wide Web server.  It is used to serve \

#              HTML files and CGI.

# processname: httpd

# config: /etc/httpd/conf/httpd.conf

# config: /etc/sysconfig/httpd

# pidfile: /var/run/httpd.pid

# Source function library.

. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/httpd ]; then

        . /etc/sysconfig/httpd

fi

# Start httpd in the C locale by default.

HTTPD_LANG=${HTTPD_LANG-"C"}

# This will prevent initlog from swallowing up a pass-phrase prompt if

# mod_ssl needs a pass-phrase from the user.

INITLOG_ARGS=""

# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server

# with the thread-based "worker" MPM; BE WARNED that some modules may not

# work correctly with a thread-based MPM; notably PHP will refuse to start.

# Path to the apachectl script, server binary, and short-form for messages.

apachectl=/usr/local/apache/bin/apachectl

httpd=${HTTPD-/usr/local/apache/bin/httpd}

prog=httpd

pidfile=${PIDFILE-/var/run/httpd.pid}

lockfile=${LOCKFILE-/var/lock/subsys/httpd}

RETVAL=0

start() {

        echo -n $"Starting $prog: "

        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS

        RETVAL=$?

        echo

        [ $RETVAL = 0 ] && touch ${lockfile}

        return $RETVAL

}


stop() {

        echo -n $"Stopping $prog: "

        killproc -p ${pidfile} -d 10 $httpd

        RETVAL=$?

        echo

        [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}

}

reload() {

    echo -n $"Reloading $prog: "

    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then

        RETVAL=$?

        echo $"not reloading due to configuration syntax error"

        failure $"not reloading $httpd due to configuration syntax error"

    else

        killproc -p ${pidfile} $httpd -HUP

        RETVAL=$?

    fi

    echo

}

# See how we were called.

case "$1" in

  start)

        start

        ;;

  stop)

        stop

        ;;

  status)

        status -p ${pidfile} $httpd

        RETVAL=$?

        ;;

  restart)

        stop

        start

        ;;

  condrestart)

        if [ -f ${pidfile} ] ; then

                stop

                start

        fi

        ;;

  reload)

        reload

        ;;

  graceful|help|configtest|fullstatus)

        $apachectl $@

        RETVAL=$?

        ;;

  *)

        echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"

        exit 1

esac

exit $RETVAL

赋予执行权限

#chmod +x /etc/init.d/httpd

启动httpd

#service httpd start

然后在浏览器测试

#http://ip   --出现It works! 的网页,表示httpd安装成功了。

压缩apache目录:

#cd /usr/local/

#tar -zcf httpd-2.4.17.tar.gz apache/

#mv httpd-2.4.17.tar.gz /tmp

压缩apr目录:

#tar -zcf apr.tar.gz apr/

#mv apr.tar.gz /tmp

压缩apr-util目录:

#tar -zcf apr-util.tar.gz apr-util/

#mv apr-util.tar.gz /tmp

二、配置playbook ,通过ansible安装配置远端服务器的httpd

1、规划(实现安装和删除httpd)

安装:

#cd /etc/ansible/roles

#mkdir apache_install/{files,handlers,meta,tasks,templates,vars} 

#cd apache_install

#mv /tmp/httpd.tar.gz files/

#mv /tmp/libiconv.tar.gz files/

#mv /tmp/apr.tar.gz files/

#mv /tmp/apr-util.tar.gz files/

配置meta

#vim  meta/main.yml

galaxy_info:

  author: liguang xing

  description: Install Apache

  license: MIT

  min_ansible_version: 1.9.4

  platforms:

  - name: CentOS

    version:

    - 6

  categories:

  - Service

  dependencies: []

配置vars变量

#vim vars/main.yml

apr_version: 1.5.2

apr_util_version: 1.5.4

libiconv_version: 1.14

apache_version: 2.4.17

apache_web_dir: /usr/local/apache/htdocs

apache_log: /usr/local/apache/logs/

apache_vhost: /usr/local/apache/conf/vhost

apache_port: 80

apache_user: www

配置tasks

#cat tasks/copy.yml

- name: Copy Apache software to client

  copy: src=` item ` dest=/tmp/ owner=root group=root

  with_items:

    - httpd-` apache_version `.tar.gz

    - libiconv.tar.gz

    - apr.tar.gz

    - apr-util.tar.gz

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: create apache user in client

  user: name=` apache_user ` state=present createhome=no shell=/sbin/nologin

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: uncompression apache software to client

  shell: tar xf /tmp/httpd-` apache_version `.tar.gz -C /usr/local/

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: uncompression apr software to client

  shell: tar xf /tmp/apr.tar.gz -C /usr/local/

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: uncompression apr-util software to client

  shell: tar xf /tmp/apr-util.tar.gz -C /usr/local/

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: copy apache sysv start service script to client

  template: src=httpd dest=/etc/init.d/httpd owner=root group=root mode=0755

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: copy apache config to Client

  template: src=httpd.conf dest=/usr/local/apache/conf/httpd.conf owner=` apache_user ` group=` apache_user ` mode=0644

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6    

注:when参数不配置也可以

#cat  tasks/delete.yml 

- name: delete apache compression software in client

  shell: rm -f /tmp/httpd-` apache_version `.tar.gz /tmp/libiconv.tar.gz /tmp/apr.tar.gz /tmp/apr-util.tar.gz

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

#cat tasks/install.yml 

- name: create lib install dir

  file: dest=/usr/local/lib state=directory

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: check apache Iconv in client

  shell: find / -name "libiconv.so.2"|grep -Ev "tmp|soft"|wc -l

  register: apache_iconv

  ignore_errors: True

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: install apache iconv in client

  shell: tar xf /tmp/libiconv.tar.gz -C /usr/local/lib/

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6 and apache_iconv|int ==0

- name: check lib in config in client

  shell: grep -c /usr/local/lib/ /etc/ld.so.conf

  register: apache_lib

  ignore_errors: True

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: install apache Iconv in client

  shell: echo "/usr/local/lib/" >> /etc/ld.so.conf;/sbin/ldconfig

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6 and apache_iconv|int ==0

- name: create apache Dir

  file: dest=` item ` state=directory

  with_items:

    - "` apache_vhost `"

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: start apache service in client

  shell: /etc/init.d/httpd start

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: add boot start apache service in client

  shell: chkconfig --add httpd;chkconfig httpd on

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

# cat tasks/main.yml 

- include: copy.yml

- include: install.yml

- include: delete.yml

将httpd启动脚本和httpd.conf配置文件copy一份到templates目录

#cp /etc/rc.d/init.d/httpd templates/
#cp /usr/local/apache/conf/httpd.conf templates/

template目录文件

# tree templates/

templates/

├── httpd

└── httpd.conf

2、配置common

#cd /etc/ansible/roles

#mkdir -pv common/{meta,tasks}

#cat meta/main.yml

galaxy_info:

  author: liguang xing

  description: Install initializtion Software

  license: MIT

  min_ansible_version: 1.9.4

  platforms:

  - name: CentOS

    versions:

    - 5

    - 6

  - name: Ubuntu

    versions:

    - precise

  categories:

  - system

dependencies: []

#cat tasks/main.yml 

- name: install initializtion require software

  shell: yum -y install make cmake bc gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel nss_ldap openldap openldap-devel openldap-clients libxslt-devel libevent-devel ntp libtool-ltdl bison libaio libtool vim-enhanced wget readline-devel libyaml-devel patch telnet pcre pcre-devel

  poll: 0

注:libaio rpm包一定要安装,mysql启动要依赖着包 

3、创建apache_install.yml安装脚本

#cd /etc/ansible/roles

#cat apache_install.yml

--- 

- hosts: "`host`"

  remote_user: "`user`"

  gather_facts: True

  roles:

    - common

    - apache_install

检查语法:

# ansible-playbook apache_install.yml --syntax-check

playbook: apache_install.yml

没错误开始执行

#ansible-playbook apache_install.yml --extra-vars "host=web user=root"

PLAY [web] ******************************************************************** 

GATHERING FACTS *************************************************************** 

ok: [10.0.90.24]

ok: [10.0.90.25]

TASK: [common | install initializtion require software] *********************** 

changed: [10.0.90.24]

changed: [10.0.90.25]

TASK: [apache_install | Copy Apache software to client] *********************** 

changed: [10.0.90.25] => (item=httpd-2.4.17.tar.gz)

changed: [10.0.90.24] => (item=httpd-2.4.17.tar.gz)

changed: [10.0.90.24] => (item=libiconv.tar.gz)

changed: [10.0.90.25] => (item=libiconv.tar.gz)

changed: [10.0.90.24] => (item=apr.tar.gz)

changed: [10.0.90.25] => (item=apr.tar.gz)

changed: [10.0.90.25] => (item=apr-util.tar.gz)

changed: [10.0.90.24] => (item=apr-util.tar.gz)

TASK: [apache_install | create apache user in client] ************************* 

changed: [10.0.90.25]

changed: [10.0.90.24]

TASK: [apache_install | uncompression apache software to client] ************** 

changed: [10.0.90.24]

changed: [10.0.90.25]

TASK: [apache_install | uncompression apr software to client] ***************** 

changed: [10.0.90.24]

changed: [10.0.90.25]

TASK: [apache_install | uncompression apr-util software to client] ************ 

changed: [10.0.90.24]

changed: [10.0.90.25]

TASK: [apache_install | copy apache sysv start service script to client] ****** 

changed: [10.0.90.25]

changed: [10.0.90.24]

TASK: [apache_install | copy apache config to Client] ************************* 

changed: [10.0.90.24]

changed: [10.0.90.25]

TASK: [apache_install | create lib install dir] ******************************* 

ok: [10.0.90.24]

ok: [10.0.90.25]

TASK: [apache_install | check apache Iconv in client] ************************* 

changed: [10.0.90.24]

changed: [10.0.90.25]

TASK: [apache_install | install apache iconv in client] *********************** 

changed: [10.0.90.24]

changed: [10.0.90.25]

TASK: [apache_install | check lib in config in client] ************************ 

failed: [10.0.90.24] => {"changed": true, "cmd": "grep -c /usr/local/lib/ /etc/ld.so.conf", "delta": "0:00:00.002203", "end": "2015-11-23 18:12:47.338012", "rc": 1, "start": "2015-11-23 18:12:47.335809", "warnings": []}

stdout: 0

...ignoring

failed: [10.0.90.25] => {"changed": true, "cmd": "grep -c /usr/local/lib/ /etc/ld.so.conf", "delta": "0:00:00.002412", "end": "2015-11-23 18:12:47.349201", "rc": 1, "start": "2015-11-23 18:12:47.346789", "warnings": []}

stdout: 0

...ignoring

TASK: [apache_install | install apache Iconv in client] *********************** 

changed: [10.0.90.24]

changed: [10.0.90.25]

TASK: [apache_install | create apache Dir] ************************************ 

changed: [10.0.90.24] => (item=/usr/local/apache/conf/vhost)

changed: [10.0.90.25] => (item=/usr/local/apache/conf/vhost)

TASK: [apache_install | start apache service in client] *********************** 

changed: [10.0.90.24]

changed: [10.0.90.25]

TASK: [apache_install | add boot start apache service in client] ************** 

changed: [10.0.90.24]

changed: [10.0.90.25]

TASK: [apache_install | delete apache compression software in client] ********* 

changed: [10.0.90.24]

changed: [10.0.90.25]

PLAY RECAP ******************************************************************** 

10.0.90.24                 : ok=18   changed=16   unreachable=0    failed=0  

10.0.90.25                 : ok=18   changed=16   unreachable=0    failed=0   

然后可以到90.24和90.25这两台客户端服务器上检查apache的安装情况,进行测试。

4、删除httpd

#cd /etc/ansible/roles

#mkdir -pv apache_delete/{meta,tasks,vars}

配置yml文件

#cat apache_delete/meta/main.yml

galaxy_info:

  author: liguang xing

  description: Delete Apache

  license: MIT

  min_ansible_version: 1.9.4

  platforms:

  - name: CentOS

    versions:

    - 6

  categories:

  - Service

dependencies: []

# cat apache_delete/tasks/delete.yml 

- name: stop httpd service in client

  service: name=httpd state=stopped

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: delete boot start in client

  shell: chkconfig --del httpd

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: delete apache Dir in client

  shell: rm -rf /usr/local/apache

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: delete apr Dir in client

  shell: rm -rf /usr/local/apr

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: delete apr-util Dir in client

  shell: rm -rf /usr/local/apr-util

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: delete apache service script in client

  shell: rm -f /etc/init.d/httpd

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: delete apache user 

  shell: userdel www

  ignore_errors: yes

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

# cat apache_delete/tasks/main.yml 

- include: delete.yml

5、创建apache_delete.yml删除脚本

#cd /etc/ansible/roles

#cat apache_delete.yml

--- 

- hosts: "`host`"

  remote_user: "`user`"

  gather_facts: True

  roles:

    - apache_delete

检查语法:

# ansible-playbook apache_delete.yml --syntax-check

playbook: apache_delete.yml

没有错误,开始执行:

# ansible-playbook apache_delete.yml --extra-vars "host=web user=root"

PLAY [web] ******************************************************************** 

GATHERING FACTS *************************************************************** 

ok: [10.0.90.25]

ok: [10.0.90.24]

TASK: [apache_delete | stop httpd service in client] ************************** 

changed: [10.0.90.25]

changed: [10.0.90.24]

TASK: [apache_delete | delete boot start in client] *************************** 

changed: [10.0.90.25]

changed: [10.0.90.24]

TASK: [apache_delete | delete apache Dir in client] *************************** 

changed: [10.0.90.24]

changed: [10.0.90.25]

TASK: [apache_delete | delete apr Dir in client] ****************************** 

changed: [10.0.90.24]

changed: [10.0.90.25]

TASK: [apache_delete | delete apr-util Dir in client] ************************* 

changed: [10.0.90.25]

changed: [10.0.90.24]

TASK: [apache_delete | delete apache service script in client] **************** 

changed: [10.0.90.25]

changed: [10.0.90.24]

TASK: [apache_delete | delete apache user] ************************************ 

changed: [10.0.90.24]

changed: [10.0.90.25]

PLAY RECAP ******************************************************************** 

10.0.90.24                 : ok=8    changed=7    unreachable=0    failed=0 

10.0.90.25                 : ok=8    changed=7    unreachable=0    failed=0 

第二部分: 安装mysql

一、首先下载mysql二进制安装包

下载地址;http://dev.mysql.com/downloads/mysql/

我使用的是mysql-5.6.19-linux-glibc2.5-x86_64.tar.gz

1、将安装过程整合到ansible

#cd /usr/local/src

#tar xf mysql-5.6.19-linux-glibc2.5-x86_64.tar.gz

#mv mysql-5.6.19-linux-glibc2.5-x86_64 mysql

再次打包

#tar -zcf mysql-5.6.19.tar.gz mysql/

#mv mysql-5.6.19.tar.gz /tmp

2、创建目录并配置yml文件:

#cd /etc/ansible/roles

#mkdir -pv mysql_install/{files,meta,tasks,templates,vars}

#cd mysql_install

#mv /tmp/mysql-5.6.19.tar.gz  files/

meta目录:

#cat meta/main.yml(参照apache_install/meta/目录中的main.yml,将description:修改下即可)

tasks目录:

# cat tasks/copy.yml 

- name: copy mysql software to in client

  copy: src=mysql-5.6.19.tar.gz dest=/tmp/mysql-5.6.19.tar.gz owner=root group=root

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: create mysql user In  client

  user: name=` mysql_user ` state=present createhome=no shell=/sbin/nologin

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6 and ansible_distribution_version|int >=6

- name: Copy Mysql Start Script To Redhat Client

  template: src=mysqld dest=/etc/init.d/mysqld owner=root group=root mode=0755

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: Copy Install Mysql Script  To Redhat Client

  template: src=install_mysql.sh dest=/tmp/ owner=root group=root mode=0755

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: Copy Mysql Config To Redhat Client

  template: src=my.cnf dest=/tmp/ owner=root group=root mode=0644

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: Copy Mysql Security Script To Redhat Client

  template: src=mysql_security.sh dest=/tmp/ owner=root group=root mode=0755

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

# cat tasks/delete.yml 

- name: Delete Mysql compression Software In Redhat Client

  shell: rm -f /tmp/mysql-5.6.19.tar.gz /tmp/mysql_security.sh

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

# cat tasks/install.yml 

- name: Create Mysql Install Dir

  file: dest=/data/mysql state=directory

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: Uncompression Mysql Software To Redhat Client

  shell: tar zxf /tmp/mysql-5.6.19.tar.gz -C /usr/local/

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: Modify Mysql Dir Permission In Redhat Client

  file: path=` item ` owner=` mysql_user ` group=` mysql_user ` mode=0755

  with_items:

    - "` mysql_datadir `"

    - "` mysql_basedir `"

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: Install Mysql Script In Redhat Client

  shell: /bin/bash /tmp/install_mysql.sh

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: Start Myql Security Script In Redhat Client

  shell: /bin/bash /tmp/mysql_security.sh

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: Add Boot Start Mysql Service In Redhat Client

  shell: chkconfig --add mysqld;chkconfig mysqld on

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

#cat tasks/main.yml 

- include: copy.yml

- include: install.yml

- include: delete.yml

3|、templates目录:

#cd /etc/ansible/roles/mysql_install

#cat templates/install_mysql.sh --脚本内容:

#!/bin/bash

mv /tmp/my.cnf /etc/my.cnf

chown -R ` mysql_user `:` mysql_user ` ` mysql_datadir ` ` mysql_basedir `

####init mysql DB##

cd ` mysql_basedir `

./scripts/mysql_install_db --defaults-file=/etc/my.cnf --datadir=` mysql_datadir ` --user=` mysql_user ` >> /dev/null 2>&1 &

sleep 3

service mysqld start

##add mysql env##

echo 'MANPATH  /usr/local/mysql/man' >> /etc/man.config

ln -sv /usr/local/mysql/include /usr/include/mysql

echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf

ldconfig

##

chkconfig --add mysqld

chkconfig mysqld on 

rm -f /tmp/$(basename $0)


拷贝mysql的配置文件到templates目录(也可以根据自己需求配置my.cnf,如下:)

# cat templates/my.cnf 

[client] 

port    = ` mysql_port `

socket  = ` mysql_sock `

[mysqld] 

character-set-server = utf8

port      = ` mysql_port `

socket    = ` mysql_sock `

datadir   = ` mysql_datadir `

log-error = ` mysql_datadir `/mysql-error.log 

pid-file  = ` mysql_datadir `/mysql.pid 

log-bin   = ` mysql_datadir `/mysql-bin

skip-external-locking 

skip-name-resolve 

skip-symbolic-links 

server-id = 1 

binlog_format=mixed 

binlog_cache_size=8M 

max_binlog_cache_size=24M 

max_binlog_size=1G 

expire_logs_days=7 

default_storage_engine = innodb

key_buffer_size = 384M 

sort_buffer_size = 2M 

read_buffer_size = 2M 

read_rnd_buffer_size = 16M 

join_buffer_size = 2M 

thread_cache_size = 32 

thread_concurrency = 16 

explicit_defaults_for_timestamp=true

interactive_timeout = 120 

wait_timeout = 120 

table_open_cache = 4096 

open_files_limit = 10240 

back_log=600 

max_connections = 5000 

max_connect_errors = 5000 

max_allowed_packet = 16M 

tmp_table_size = 256M 

max_heap_table_size = 512M 

bulk_insert_buffer_size = 64M 

myisam_sort_buffer_size = 64M 

myisam_repair_threads =1 

myisam-recover-options

long_query_time = 2 

slow_query_log 

slow_query_log_file = ` mysql_datadir `/slow.log 

innodb_buffer_pool_size = 1G

innodb_data_file_path = ibdata1:1G:autoextend 

innodb_data_home_dir = ` mysql_datadir `

innodb_log_group_home_dir = ` mysql_datadir `

innodb_file_io_threads = 4 

innodb_thread_concurrency = 8 

innodb_flush_log_at_trx_commit = 2 

innodb_log_buffer_size = 256M 

innodb_log_file_size = 512M 

innodb_log_files_in_group = 3 

innodb_max_dirty_pages_pct = 90 

innodb_lock_wait_timeout = 120 

innodb_file_per_table = 1 

innodb_support_xa=0  

innodb_flush_method = O_DIRECT 

[mysqldump] 

quick 

max_allowed_packet = 32M 

[mysql] 

no-auto-rehash 

[myisamchk] 

key_buffer_size = 512M 

sort_buffer_size = 512M 

read_buffer = 8M 

write_buffer = 8M 

[mysqlhotcopy] 

interactive-timeout 

拷贝mysqld启动脚本:

#cp /usr/local/src/mysql/support-files/mysql.server templates/

设置密码脚本:

# cat templates/mysql_security.sh 

#!/bin/bash

` mysql_basedir `/bin/mysql -h localhost -u root -e "set password for 'root'@'localhost' = password('mima');"

templates目录结构:

# tree templates/

templates/

├── install_mysql.sh

├── my.cnf

├── mysqld

└── mysql_security.sh

vars 目录:

# cat vars/main.yml 

mysql_basedir: /usr/local/mysql

mysql_datadir: /data/mysql

mysql_user: mysql

mysql_database_user: root

mysql_passwd: 'mima'

mysql_port: 3306

mysql_sock: /data/mysql/mysql.sock

mysql_charset: utf8

mysql_collation: utf8_general_ci

mysql_version: mysql-5.6.19-linux-glibc2.5-x86_64.tar.gz

3、创建mysql_install.yml安装文件

#cd /etc/ansible/roles

# cat mysql_install.yml 

--- 

- hosts: "`host`"

  remote_user: "`user`"

  gather_facts: True

  roles:

    - common

    - mysql_install

4、开始执行

#ansible-playbook mysql_install.yml --syntax-check   --检测语法

playbook: mysql_install.yml

没错误,开始执行:

# ansible-playbook mysql_install.yml --extra-vars "host=web user=root"

PLAY [web] ******************************************************************** 

GATHERING FACTS *************************************************************** 

ok: [10.0.90.24]

TASK: [common | install initializtion require software] *********************** 

changed: [10.0.90.24]

TASK: [mysql_install | copy mysql software to in client] ********************** 

ok: [10.0.90.24]

TASK: [mysql_install | create mysql user In  client] ************************** 

changed: [10.0.90.24]

TASK: [mysql_install | Copy Mysql Start Script To Redhat Client] ************** 

changed: [10.0.90.24]

TASK: [mysql_install | Copy Install Mysql Script  To Redhat Client] *********** 

changed: [10.0.90.24]

TASK: [mysql_install | Copy Mysql Config To Redhat Client] ******************** 

changed: [10.0.90.24]

TASK: [mysql_install | Copy Mysql Security Script To Redhat Client] *********** 

changed: [10.0.90.24]

TASK: [mysql_install | Create Mysql Install Dir] ****************************** 

changed: [10.0.90.24]

TASK: [mysql_install | Uncompression Mysql Software To Redhat Client] ********* 

changed: [10.0.90.24]

TASK: [mysql_install | Modify Mysql Dir Permission In Redhat Client] ********** 

changed: [10.0.90.24] => (item=/data/mysql)

changed: [10.0.90.24] => (item=/usr/local/mysql)

TASK: [mysql_install | Install Mysql Script In Redhat Client] ***************** 

changed: [10.0.90.24]

TASK: [mysql_install | Start Myql Security Script In Redhat Client] *********** 

changed: [10.0.90.24]

TASK: [mysql_install | Add Boot Start Mysql Service In Redhat Client] ********* 

changed: [10.0.90.24]

TASK: [mysql_install | Delete Mysql compression Software In Redhat Client] **** 

changed: [10.0.90.24]

PLAY RECAP ******************************************************************** 

10.0.90.24                 : ok=15   changed=13   unreachable=0    failed=0   

成功之后到90.24服务器上查看mysql是否安装成功!我测试没问题

二、删除mysql

1、创建目录:

#cd /etc/ansible/roles

#mkdir -pv mysql_delete/{meta,tasks,vars}

# cat meta/main.yml (参照apache_install/meta/目录中的main.yml,将description:修改下即可) 

# cat tasks/delete.yml 

- name: Stop Mysql Service

  service: name=mysqld state=stopped

  ignore_errors: yes

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: Delete Mysql Boot Start Script

  shell: chkconfig --del mysqld

  ignore_errors: yes

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: Delete Mysql Dir And Socket

  shell: rm -rf ` mysql_basedir ` ` mysql_datadir ` 

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: Delete Mysql User

  shell: userdel ` mysql_user `

  ignore_errors: yes

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: Delete Mysql Service Start Script

  shell: rm -f /etc/init.d/mysqld

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: Delete Mysql lib

  shell: rm -f /etc/ld.so.conf.d/mysql.conf

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: Delete Mysql include file

  shell: rm -f /usr/include/mysql 

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

- name: Delete Mysql config file

  shell: rm -f /etc/my.cnf

  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

#cat tasks/main.yml 

- include: delete.yml

#cat vars/main.yml 

mysql_basedir: /usr/local/mysql

mysql_datadir: /data/mysql

mysql_user: mysql

2、创建mysql_delete.yml文件:

#cd /etc/ansible/roles

#cat mysql_delete.yml 

---

- hosts: "`host`"

  remote_user: "`user`"

  gather_facts: True

  roles:

    - mysql_delete

3、检测语法并执行:

# ansible-playbook mysql_delete.yml --syntax-check

playbook: mysql_delete.yml  

没有报错,开始执行

# ansible-playbook mysql_delete.yml --extra-vars "host=web user=root"

PLAY [web] ******************************************************************** 

GATHERING FACTS *************************************************************** 

ok: [10.0.90.24]

TASK: [mysql_delete | Stop Mysql Service] ************************************* 

changed: [10.0.90.24]

TASK: [mysql_delete | Delete Mysql Boot Start Script] ************************* 

changed: [10.0.90.24]

TASK: [mysql_delete | Delete Mysql Dir And Socket] **************************** 

changed: [10.0.90.24]

TASK: [mysql_delete | Delete Mysql User] ************************************** 

changed: [10.0.90.24]

TASK: [mysql_delete | Delete Mysql Service Start Script] ********************** 

changed: [10.0.90.24]

TASK: [mysql_delete | Delete Mysql lib] *************************************** 

changed: [10.0.90.24]

TASK: [mysql_delete | Delete Mysql include file] ****************************** 

changed: [10.0.90.24]

TASK: [mysql_delete | Delete Mysql config file] ******************************* 

changed: [10.0.90.24]

PLAY RECAP ******************************************************************** 

10.0.90.24                 : ok=9    changed=8    unreachable=0    failed=0 

第三部分:安装php

一、在ansible服务端安装php或者其他服务器上(服务器架构和ansible客户端保持一致)

注:本文档编译安装的php没有编译太多模块,仅供参考,如果需要其他模块,请自行编译加入!

1、下载php源码,地址:http://php.net/downloads.php

本次下载的是php-5.6.15.tar.gz

2、开始编译安装php

如果想让编译的php支持mcrypt扩展,需要下载(http://www.rpmfind.net) 如下两个rpm包并安装之:

libmcrypt-2.5.8-9.el6.x86_64.rpm

libmcrypt-devel-2.5.8-9.el6.x86_64.rpm

安装libmcrypt:

#cd /usr/local/src

# rpm -ivh libmcrypt-2.5.8-9.el6.x86_64.rpm libmcrypt-devel-2.5.8-9.el6.x86_64.rpm 

Preparing...                ########################################### [100%]

   1:libmcrypt              ########################################### [ 50%]

   2:libmcrypt-devel        ########################################### [100%]

安装php:

#tar xf php-5.6.15.tar.gz

#cd php-5.6.15

#./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config \

--enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml  \

--with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt --with-iconv=/usr/local/

#make 

#make test

#make install

3、为php提供配置文件

#mkdir /etc/php

#cp php.ini-production /etc/php/php.ini

4、编辑apache配置文件httpd.conf,以使apache支持php

 # vim /usr/local/apache/conf/httpd.conf

(1)在AddType处添加如下两行

   AddType application/x-httpd-php  .php

   AddType application/x-httpd-php-source  .phps

(2)定位至DirectoryIndex index.html 

添加index.php:

    DirectoryIndex  index.php  index.html

    

测试是否可以正常解析显示php格式的网页:

#cd /usr/local/apache/htdocs

#cat index.php

<html><body><h1>

php is works!

</h1></body></html>

<?php

phpinfo();

?>

重启httpd

#service httpd restart

在浏览器中测试:

http://ip   --如果网页显示php测试页面,表示已经支持php网页。(我测试是可以正常打开php测试页面的)

注:如果无法显示php的网页,查看selinux和iptables,selinux需要disabled或者permissive状态!!!

5、测试php和mysql的连接情况

#vim index.php

<html><body><h1>

php is works!

</h1></body></html>

<?php

$conn=mysql_connect('localhost:/data/mysql/mysql.sock','root','123456');

if($conn)

  echo "Success...";

else

  echo "Failure...";

mysql_close();

?>

注:我的mysql是编译安装的,sock文件在/data/mysql/目录,如果sock文件在/tmp目录,localhost后面就不需要指定sock路径。

然后再浏览器测试:

http://ip      --出现如下页面,表示php与mysql连接正常

php is works!

Success...

二、整合php并通过ansible安装到客户端

1、创建目录

#cd /etc/ansible/roles

#mkdir -pv php_install/{files,meta,tasks,templates,vars}

2、压缩php目录并拷贝文件

#cd /usr/local/

#tar -zcf php-5.6.15.tar.gz php/

#mv php-5.6.15.tar.gz /etc/ansible/roles/php_install/files

拷贝libmcrypt文件

#cd /usr/local/src

#cp libmcrypt-*.rpm /etc/ansible/roles/php_install/files

拷贝libphp5.so文件(没有这个so文件,apache将无法解析php页面)

#cp /usr/local/apache/modules/libphp5.so  /etc/ansible/roles/php_install/files

拷贝httpd.conf文件

#cd /etc/ansible/roles/php_install

#cp /usr/local/apache/conf/httpd.conf templates/

拷贝php.ini文件

#cp /etc/php/php.ini templates/

创建install_lib.sh脚本

#vim templates/install_lib.sh

#!/bin/bash

cd /tmp

rpm -ivh libmcrypt-2.5.8-9.el6.x86_64.rpm libmcrypt-devel-2.5.8-9.el6.x86_64.rpm

3、创建yml文件

#cat meta/main.yml  (参考apache_install/meta/目录中的main.yml,将description:修改下即可)

#cat tasks/copy.yml 

- name: copy php software to client

  copy: src=php-5.6.15.tar.gz dest=/tmp/ owner=root group=root

- name: copy php libphp5.so file  to client

  copy: src=libphp5.so dest=/usr/local/apache/modules/ owner=root group=root mode=0755

- name: uncompression php software to client

  shell: tar xf /tmp/php-5.6.15.tar.gz -C /usr/local/

- name: copy install lib script to client

  template: src=install_lib.sh dest=/tmp/ owner=root group=root mode=0755

- name: copy libmcrypt package to client

  copy: src=libmcrypt-2.5.8-9.el6.x86_64.rpm dest=/tmp/ owner=root group=root

- name: copy libmcrypt-devel package to client

  copy: src=libmcrypt-devel-2.5.8-9.el6.x86_64.rpm dest=/tmp/ owner=root group=root

- name: create php config  Dir

  file: dest=/etc/php state=directory

- name: copy php config file to client

  template: src=php.ini dest=/etc/php owner=root group=root

- name: copy httpd.conf config file to client

  template: src=httpd.conf dest=/usr/local/apache/conf/ owner=root group=root

#cat tasks/delete.yml

- name: delete php software and libmcrypt in client

  shell: rm -f /tmp/php-5.6.15.tar.gz /tmp/libmcrypt-*.rpm /tmp/install_lib.sh

#cat tasks/install.yml 

- name: install libmcrypt package in client

  shell: /bin/bash /tmp/install_lib.sh

#cat tasks/main.yml 

- include: copy.yml

- include: install.yml

- include: delete.yml

templates 目录下文件:

#tree templates/

templates/

├── httpd.conf

├── install_lib.sh

└── php.ini

files目录下文件

#tree files

files

├── libmcrypt-2.5.8-9.el6.x86_64.rpm

├── libmcrypt-devel-2.5.8-9.el6.x86_64.rpm

├── libphp5.so

└── php-5.6.15.tar.gz

4、创建php_install.yml文件

#cd /etc/ansible/roles

# cat php_install.yml 

--- 

- hosts: "`host`"

  remote_user: "`user`"

  gather_facts: True

  roles:

    - common

    - php_install

5、检测语法并执行:

#ansible-playbook php_install.yml --syntax-check

执行:

#ansible-playbook php_install.yml --extra-vars "host=web user=root" 

PLAY [web] ******************************************************************** 

GATHERING FACTS *************************************************************** 

ok: [10.0.90.24]

TASK: [common | install initializtion require software] *********************** 

changed: [10.0.90.24]

TASK: [php_install | copy php software to client] ***************************** 

ok: [10.0.90.24]

TASK: [php_install | copy php libphp5.so file  to client] ********************* 

changed: [10.0.90.24]

TASK: [php_install | uncompression php software to client] ******************** 

changed: [10.0.90.24]

TASK: [php_install | copy install lib script to client] *********************** 

ok: [10.0.90.24]

TASK: [php_install | copy libmcrypt package to client] ************************ 

ok: [10.0.90.24]

TASK: [php_install | copy libmcrypt-devel package to client] ****************** 

ok: [10.0.90.24]

TASK: [php_install | create php config  Dir] ********************************** 

ok: [10.0.90.24]

TASK: [php_install | copy php config file to client] ************************** 

changed: [10.0.90.24]

TASK: [php_install | copy httpd.conf config file to client] ******************* 

ok: [10.0.90.24]

TASK: [php_install | install libmcrypt package in client] ********************* 

ok: [10.0.90.24] 

PLAY RECAP ******************************************************************** 

10.0.90.24                 : ok=11   changed=10    unreachable=0    failed=0 


三、删除php

1、创建目录

#cd /etc/ansible/roles

#mkdir -pv php_delete/{meta,tasks}

2、创建yml文件

#cd php_delete

#cat meta/main.yml (参考apache_install/meta/目录中的main.yml文件,将description:修改下即可)

# cat tasks/delete.yml 

- name: delete php directory in client

  shell: rm -rf /usr/local/php 

- name: delete php config file in client

  shell: rm -f /etc/php/php.ini

#cat tasks/main.yml 

- include: delete.yml

3、创建php_delete.yml文件

#cd /etc/ansible/roles

# cat php_delete.yml 

---

- hosts: "`host`"

  remote_user: "`user`"

  gather_facts: True

  roles:

    - php_delete

4、检测语法并执行

#ansible-playbook php_delete.yml --syntax-check

执行:

#ansible-playbook php_delete.yml --extra-vars "host=web user=root"

PLAY [web] ******************************************************************** 

GATHERING FACTS *************************************************************** 

ok: [10.0.90.24]

TASK: [php_delete | delete php directory in client] *************************** 

changed: [10.0.90.24]

TASK: [php_delete | delete php config file in client] ************************* 

changed: [10.0.90.24]

PLAY RECAP ******************************************************************** 

10.0.90.24                 : ok=3    changed=2    unreachable=0    failed=0 

第四部分:整合ansible安装LAMP环境

1、创建安装lamp的lamp_install.yml文件

#cd /etc/ansible/roles
#vim lamp_install.yml

---

- hosts: "`host`"

  remote_user: "`user`"

  gather_facts: True

  roles:

    - apache_install

    - mysql_install

    - php_install

测试:

# ansible-playbook lamp_install.yml --syntax-check

playbook: lamp_install.yml    --无报错,执行:

# ansible-playbook lamp_install.yml --extra-vars "host=web user=root"

PLAY [web] ******************************************************************** 

……………………   太多了,省略

PLAY RECAP ******************************************************************** 

10.0.90.24                 : ok=41   changed=38   unreachable=0    failed=0  

2、创建删除lamp的lamp_delete.yml文件

#cd /etc/ansible/roles

#vim lamp_delete.yml

--- 

- hosts: "`host`"

  remote_user: "`user`"

  gather_facts: True

  roles:

    - apache_delete

    - mysql_delete

    - php_delete

测试:

# ansible-playbook lamp_delete.yml --syntax-check

playbook: lamp_delete.yml   --无报错,执行:
# ansible-playbook lamp_delete.yml --extra-vars "host=web user=root"

…………………………省略

PLAY RECAP ******************************************************************** 

10.0.90.24                 : ok=18   changed=17   unreachable=0    failed=0   

   


注:参考链接:http://dl528888.blog.51cto.com/2382721/1533086 

本人也是刚开始研究ansible,不足之处,请多多指出!