This is my 12th day of the August Challenge.

Playbooks is Ansible’s configuration, deployment, and orchestration language. They can be described as a scenario that requires commands to be executed by a remote host, or a set of commands to be run by an IT program.

In short, Playbooks is our current automation solution. With Playbooks, we can customize all kinds of automation on demand.

In this article, we describe the automatic deployment of a LAMP environment, breaking it down into the following phases:

  • Lamp (Apache + PHP and extensions)
  • Initial configuration (automatically load PHP module, modify MPM, log round storage, etc.);
  • Manually modify site files as needed;

Configuration idea

The ansible-Playbook configuration roadmap is as follows:

  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. Call the template lamp_install.sh from install.yml in tasks to install lamp into the installation directory defined in the variable.
  4. Call the Copy module and install module from main.yml in Tasks;
  5. Call playbook (lamp. Yml) : lamp_install to automate deployment;

The directory structure

We organize the directory structure by Playbook, where:

  • 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: The lamp installation process is defined as the tasks to be performed.
  • Templates: The template files used to execute the LAMP installation, usually scripts;
  • Vars: variables defined in this installation.
[root@test ansible]# cd /etc/ansible/
[root@test ansible]# mkdir -p roles/lamp_install/{files,handlers,meta,tasks,templates,vars}
[root@test ansible]# tree /etc/ansible├ ─ ─ ansible. CFG ├ ─ ─ hosts ├ ─ ─ lamp. Yml ├ ─ ─ the log │ └ ─ ─ ansible. Log ├ ─ ─ roles │ ├ ─ ─ lamp_install │ │ ├ ─ ─ files │ │ │ ├ ─ ─ Apache │ ├─ Bass -1.5. 0.tar.gz │ │ ├─ Bass exercises -1.53..tar.gz │ │ ├─ ├─ HTTPD -2.47.. Tar. Gz │ │ │ │ ├ ─ ─ HTTPD - MPM. Conf │ │ │ │ ├ ─ ─ pcre -8.36. Tar. Gz │ │ │ │ ├ ─ ─. Rewrite the conf │ │ │ │ └ ─ ─ symfony.zip│ │ ├── Memcached │ ├─ ├─ Libevent -2.022.-Stables. Tar.gz │ ├─ ├─ Heavy Metal -0.5.tar.gz │ │ ├ ── memcached-1.422.. Tar. Gz │ │ │ └ ─ ─ PHP │ │ │ ├ ─ ─ start - start - 42067 ac. Tar. Gz │ │ │ ├ ─ ─ libmcrypt -2.57.-1.2│ │ ├── LibmCrypt-Devel -2.57.-1.2.el6.x86_64. RPM │ │ ├── memcached_extension │ │ ├── libmemcached-1.018..tar. Tar │ │ ├ ── memcached-2.2. 0.tgz │ │ ├── memcache_extension │ │ ├─ memcache-2.27.TGZ │ ├─ Heavy Metal Exercises -1.210..pdf │ ├ ─ PDF -5.422.. Tar. Gz │ │ ├ ─ ─ handlers │ │ ├ ─ ─ meta │ │ ├ ─ ─ the tasks │ │ │ ├ ─ ─ copy. Yml │ │ │ ├ ─ ─ the yml │ │ │ └ ─ ─ the main, yml │ │ ├ ─ ─ Templates │ │ │ ├ ── lamp_install.sh │ │ ├ ─vars│ │ └ ─ ─ the main ymlCopy the code

The specific implementation

1. Create a LAMP role file to call lamp_install

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

2. Create a variable file

The variable file is used to extract variables to meet the requirements of a defined deployment.

[root@test ansible]# cd /etc/ansible/roles/lamp_install/vars
[root@test ansible]#vim main.yml
# Source directory
source_dir: /home/ap/src/lamp/
# Source installation directory
install_dir: /home/ap/
Copy the code

3. Create task files

The purpose of the task file is to synchronize the various source code to the remote machine to do the preparation work before installation.

[root@test ansible]# cd /etc/ansible/roles/mysql_install/tasks
[root@test ansible]# vim copy.yml
Copy the PHP component to the target server
- name: copy php dir to client
  copy: src=php dest={{source_dir}} owner=root group=root
# Copy the Apache component to the target server
- name: copy apache dir to client
  copy: src=apache dest={{source_dir}} owner=root group=root
Copy the memcached component to the target server
- name: copy memcached dir to client
  copy: src=memcached dest={{source_dir}} owner=root group=root
Copy template files to target server
- name: copy lamp script to client
  template: src=lamp_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 lamp
  shell: bash {{source_dir}}/lamp_install.sh
[root@test ansible]# vim main.yml
# Reference the copy and install modules
- include: copy.yml
- include: install.yml
Copy the code

Note:

  • The copy module is used to copy the directory.
  • The copy module copies directories. No directories will be created on the target server.
  • The copy module needs to add /home/ap/src/lamp to the dest parameter instead of /home/ap/lamp to copy files to a directory on the target server. Otherwise, Ansible will copy files to lamp instead of the LAMP directory.

4. Write a template script

A template script is used to install, deploy, and configure components as follows:

  • Install Apache and related components
  • Install memcached and Magent
  • Install PHP and related extensions mogo, memcached, memcache, SOAP, Gd, MBString, exif, eAccelerator, and add them to php.ini. Reference PHP in Apache
  • Modify http.conf, including adding the rewrite module and modifying AllowOverride
  • Modify HTTP – MPM. Conf
  • Add a rewrite. Conf
  • Add a site configuration file
[root@test ansible]#cd /etc/ansible/roles/lamp_install/templates
#! /bin/bash
#author:yanggd
#comment: Lamp is deployed
source_dir={{source_dir}}
apache=$source_dir/apache
php=$source_dir/php
memcached=$source_dir/memcached
install_dir={{install_dir}}
#Source function library.
. /etc/init.d/functions
# to install apache
cd $apache
tar -zxvf apr-1.5. 0.tar.gz
cd apr-1.5. 0
./configure --prefix=$install_dir/apr
make && make install
if [ $? -ne 0]; then action"install apr is failed!"  /bin/false
  exit $?
fi
cd ..
#
tar -zxvf apr-util-1.53..tar.gz
cd apr-util-1.53.
./configure --prefix=$install_dir/apr-util --with-apr=$install_dir/apr
make && make install
if [ $? -ne 0]; then action"install apr-util is failed!"  /bin/false
  exit $?
fi
cd ..
#
tar -zxvf pcre-8.36.tar.gz
cd pcre-8.36
./configure --prefix=$install_dir/pcre
make && make install
if [ $? -ne 0]; then action"install pcre is failed!"  /bin/false
  exit $?
fi
#
cd ..
tar -zxvf httpd-2.47..tar.gz 
cd httpd-2.47.
./configure --prefix=$install_dir/apache --with-apr=$install_dir/apr --with-apr-util=$install_dir/apr-util --with-pcre=$install_dir/pcre --enable-modules=mall --enable-rewrite --enable-mpms-shared=all --with-mpm=event --enable-v4-mapped --enable-so
make && make install
if [ $? -ne 0]; then action"install httpd is failed!"  /bin/false
  exit $?
fi
cd ..
#
unzip symfony.zip -d $install_dir
cd ..

# memcached installation
cd $memcached
tar -zxvf libevent-2.022.-stable.tar.gz
cd libevent-2.022.-stable
./configure --prefix=$install_dir/libevent
make && make install
if [ $? -ne 0]; then action"install libevent is failed!"  /bin/false
  exit $?
fi
cd ..
tar -zxvf memcached-1.422..tar.gz
cd memcached-1.422.
./configure --prefix=$install_dir/memcached --with-libevent=$install_dir/libevent
make && make install
if [ $? -ne 0]; then action"install memcached is failed!"  /bin/false
  exit $?
fi
# installation magent
cd ..
ln -s $install_dir/libevent/lib/libevent-2.0.so. 5 /lib64/libevent-2.0.so. 5
tar -zxf magent-0.5.tar.gz
sed -i "s#LIBS = -levent#LIBS = -levent -lm -L$install_dir/libevent/lib#g" Makefile
sed -i "/LIBS/a INCLUDE=-I$install_dir/libevent/include" Makefile
sed -i "1i\#ifndef SSIZE_MAX" ketama.h
sed -i "4i\#define SSIZE_MAX 32676" ketama.h
sed -i '$i\#endif' ketama.h
# compiler
make
mkdir -p $install_dir/magent/bin
mv magent $install_dir/magent/bin/

cd ..
# installing PHP
yum install -y libxml2-devel libjpeg-devel libpng-devel freetype-devel openssl-devel libcurl-devel unzip
cd $php
rpm -ivh libmcrypt-2.57.-1.2.el6.rf.x86_64.rpm
rpm -ivh libmcrypt-devel-2.57.-1.2.el6.rf.x86_64.rpm
tar -zxvf php-5.422..tar.gz
cd php-5.422.
./configure --prefix=$install_dir/php --with-apxs2=$install_dir/apache/bin/apxs --with-openssl --with-mcrypt --with-zlib --with-libxml-dir --enable-xml --with-freetype-dir --with-curl --enable-sockets --with-config-file-path=$install_dir/php/etc --with-mysql --with-mysqli --with-pdo-mysql
make && make install
if [ $? -ne 0]; then action"install php is failed!"  /bin/false
  exit $?
fi
cp php.ini-production $install_dir/php/etc/php.ini
sed -i '/mime.types/a\ AddType application/x-httpd-php .php' $install_dir/apache/conf/httpd.conf
sed -i '/mime.types/a\ AddType application/x-httpd-php-source .phps' $install_dir/apache/conf/httpd.conf
sed -i '/; date.timezone/a date.timezone = "Asia/Shanghai"' $install_dir/php/etc/php.ini
sed -i '/; date.timezone/a date.timezone = PRC'$install_dir/php/etc/php.ini cd .. /Install the PHP extension
Install the Mono extension
tar -zxvf mongo-1.210..tgz
cd mongo-1.210.
$install_dir/php/bin/phpize
./configure --with-php-config=$install_dir/php/bin/php-config 
make && make install
sed -i '/php_bz2.dll/a extension=mongo.so' $install_dir/php/etc/php.ini
cd ..
Install the memcached extension
cd memcached_extension
tar -zxvf libmemcached-1.018..tar.tar
cd libmemcached-1.018.
./configure --prefix=$install_dir/libmemcached --with-memcached
make && make install
cd ..
tar -zxvf memcached-2.2. 0.tgz
cd memcached-2.2. 0
$install_dir/php/bin/phpize
./configure --enable-memcached --with-php-config=$install_dir/php/bin/php-config --with-libmemcached-dir=$install_dir/libmemcached --disable-memcached-sasl
make && make install
sed -i '/php_bz2.dll/a extension=memcached.so'$install_dir/php/etc/php.ini cd .. /.. /Install the Memcache extension
cd memcache_extension
tar -zxvf memcache-2.27..tgz
cd memcache-2.27.
$install_dir/php/bin/phpize
./configure --with-php-config=$install_dir/php/bin/php-config --enable-memcache
make && make install
sed -i '/php_bz2.dll/a extension=memcache.so'$install_dir/php/etc/php.ini cd .. /.. /# installed soap
cd php-5.422./ext/soap/
$install_dir/php/bin/phpize
./configure --enable-soap  --with-php-config=$install_dir/php/bin/php-config
make && make install
sed -i '/php_bz2.dll/a extension=soap.so'$install_dir/php/etc/php.ini cd .. /Install the GD extension
cd gd
$install_dir/php/bin/phpize
./configure --with-png-dir --with-freetype-dir --with-jpeg-dir --with-gd --with-php-config=$install_dir/php/bin/php-config
make && make install
sed -i '/php_bz2.dll/a extension=gd.so' $install_dir/php/etc/php.ini
cd ..
Install the MBString extension
cd mbstring
$install_dir/php/bin/phpize
./configure --with-php-config=$install_dir/php/bin/php-config
make && make install
sed -i '/php_bz2.dll/a extension=mbstring.so' $install_dir/php/etc/php.ini
cd ..
Install the exIF extension
cd exif
$install_dir/php/bin/phpize
./configure --with-php-config=$install_dir/php/bin/php-config
make && make install
sed -i '/php_bz2.dll/a extension=exif.so'$install_dir/php/etc/php.ini cd .. /.. /.. /Install the eAccelerator extension
tar -zxvf eaccelerator-eaccelerator-42067ac.tar.gz
cd eaccelerator-eaccelerator-42067ac
$install_dir/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=$install_dir/php/bin/php-config
make && make install
sed -i '/php_bz2.dll/a extension=eaccelerator.so' $install_dir/php/etc/php.ini
mkdir -p $install_dir/eaccelerator_cache
chmod -R 777 $install_dir/eaccelerator_cache
cat >> $install_dir/php/etc/php.ini <<EOF
eaccelerator.shm_size="64"
eaccelerator.cache_dir="$install_dir/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=! "" *.yml.php"
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"
eaccelerator.allowed_admin_path = "/web/smallcity.cityhouse.cn"
EOF

##prepare apache
# modify HTTP. Conf
sed -i "s/#LoadModule rewrite_module/LoadModule rewrite_module/g" $install_dir/apache/conf/httpd.conf
sed -i "s/#ServerName www.example.com:80/ServerName localhost:80/g" $install_dir/apache/conf/httpd.conf
sed -i "s/AllowOverride None/AllowOverride All/g" $install_dir/apache/conf/httpd.conf
sed -i "s/AllowOverride none/AllowOverride All/g" $install_dir/apache/conf/httpd.confz
sed -i "s/Require all denied/Require all granted/g" $install_dir/apache/conf/httpd.conf
sed -i "s#logs/access_log#| $install_dir/apache/bin/rotatelogs $install_dir/apache/logs/access_log.%Y-%m-%d 86400 480#g" $install_dir/apache/conf/httpd.conf
#prepare *.conf
\cp $apache/http-mpm.conf $install_dir/apache/conf/extra/
\cp $apache/rewrite.conf $install_dir/apache/conf/
\cp $apache/cHost.conf $install_dir/apache/conf/

# Boot up
echo "$install_dir/apache/bin/apachectl -k start" >> /etc/rc.local

Copy the code

Deployment of 5.

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