1. 程式人生 > 實用技巧 >Ansible playbook 分離部署lamp

Ansible playbook 分離部署lamp

準備4臺伺服器

安裝的環境 IP/主機名
ansible 192.168.220.10/apache
apache 192.168.220.20/apache
mysql 192.168.220.30/mysql
php 192.168.220.40/php

關閉關閉防火牆和selinux:

 systemctl stop firewalld
 setenforce 0

安裝ansible:

//配置yum源
[root@ansible ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
[root@ansible ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo [root@ansible ~]# sed -i 's#\$releasever#8#g' /etc/yum.repos.d/CentOS-Base.repo [root@ansible ~]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm [root@ansible ~]# sed
-i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel* [root@ansible ~]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel* [root@ansible ~]# sed -i 's#\$releasever#8#g' /etc/yum.repos.d/epel.repo //安裝ansible [root@ansible ~]# yum -y install ansible
//檢視ansible版本 [root@ansible ~]# ansible --version ansible 2.9.16 config file = /etc/ansible/ansible.cfg configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python3.6/site-packages/ansible executable location = /usr/bin/ansible python version = 3.6.8 (default, Dec 5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] //ssh免密登入 [root@ansible lamp]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.220.10 ansible 192.168.220.20 apache 192.168.220.30 mysql 192.168.248.40 php [root@ansible ~]# ssh-keygen -t rsa [root@ansible ~]# ssh-copy-id root@apache [root@ansible ~]# ssh-copy-id root@mysql [root@ansible ~]# ssh-copy-id root@php

將被控機IP加入到主控機清單:

[root@ansible ~]# mkdir lamp
[root@ansible ~]# cd lamp
[root@ansible lamp]# cp /etc/ansible/ansible.cfg  .
 //建立清單檔案
[root@ansible lamp]# vim inventory
[group_apache]
apache
 
[group_mysql]
mysql
 
[group_php]
php

//測試
[root@ansible lamp]# ansible all -m ping
php | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
apache | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
mysql | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
[root@ansible lamp]# 

搭建源

[root@ansible lamp]# vim  yum.yml
---
- hosts: all
  tasks:
    - name: copy yum
      copy:
        src: /etc/yum.repos.d/
        dest: /etc/yum.repos.d/
    - name: copy key
      copy:
        src: /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8
        dest: /etc/pki/rpm-gpg
    - name: clean
      command: yum clean all
    - name: makecache
      command: yum makecache
[root@ansible lamp]# ansible-playbook  yum.yml

apache安裝:

//已經下載的安裝包
[root@ansible ~]# ls
anaconda-ks.cfg    apr-util-1.6.1.tar.gz  lamp
apr-1.7.0.tar.bz2  httpd-2.4.46.tar.gz    mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz

playbook安裝apache:

//安裝
[root@ansible lamp]# vim apache/scripts/apr.sh
#!/bin/bash
tar -xf /root/apr-1.7.0.tar.bz2
sed -i 's/\$RM "$cfgfile"/\#\$RM "$cfgfile"/' /root/apr-1.7.0/configure
cd /root/apr-1.7.0
./configure  --prefix=/usr/local/apr
make && make install
 
[root@ansible lamp]# vim apache/scripts/apr_util.sh
#!/bin/bash
tar -xf /root/apr-util-1.6.1.tar.gz
cd /root/apr-util-1.6.1
./configure  --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make && make install
 
[root@ansible lamp]# vim apache/scripts/httpd.sh
#!/bin/bash
tar -xf /root/httpd-2.4.46.tar.gz
cd /root/httpd-2.4.46
./configure --prefix=/usr/local/apache \
    --sysconfdir=/etc/httpd24 \
    --enable-so \
    --enable-ssl \
    --enable-cgi \
    --enable-rewrite \
    --with-zlib \
    --with-pcre \
    --with-apr=/usr/local/apr \
    --with-apr-util=/usr/local/apr-util/ \
    --enable-modules=most \
    --enable-mpms-shared=all \
    --with-mpm=prefork
make && make install
 
[root@ansible lamp]# vim apache/scripts/config.sh
#!/bin/bash
#variable
echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
source /etc/profile.d/httpd.sh
#include
ln -s /usr/local/apache/include/ /usr/include/httpd
#config
sed -i '/proxy.so/ s/#//g ' /etc/httpd24/httpd.conf
sed -i '/fcgi.so/ s/#//g' /etc/httpd24/httpd.conf
sed -i '/index.html/ s/index.html/index.php index.html/g'  /etc/httpd24/httpd.conf
sed -i 's|AddType application/x-gzip .gz .tgz|AddType application/x-gzip .gz .tgz\n    AddType application/x-httpd-php .php\n    AddType application/x-httpd-php-source .phps|' /etc/httpd24/httpd.conf
#set virtualhost
echo -e '<VirtualHost *:80>\n    DocumentRoot "/usr/local/apache/htdocs/"\n    ServerName chouyu.com\n    ProxyRequests Off\n    ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.220.40:9000/var/www/html/$1\n    <Directory "/usr/local/apache/htdocs/">\n        Options none\n        AllowOverride none\n        Require all granted\n    </Directory>\n</VirtualHost>' >> /etc/httpd24/httpd.conf
#start
apachectl start
 
 
 
[root@ansible lamp]# vim apache/httpd.yml
---
- hosts: httpd
  vars:
    package: openssl-devel,pcre-devel,expat-devel,libtool,gcc,gcc-c++,make
  tasks:
    - name: install
      yum:
        name: '{{ package }}'
        state: present
    - name: package group
      yum:
        name: "@Development tools"
        state: present
    - name: create user
      user:
        name: apache
        system: yes
        create_home: no
        shell: /sbin/nologin
        state: present
    - name: copy apr-util
      copy:
        src: /root/apr-util-1.6.1.tar.gz
        dest: /root
    - name: copy apr
      copy:
        src: /root/apr-1.7.0.tar.bz2
        dest: /root/
    - name: copy httpd
      copy:
       src: /root/apr-1.7.0.tar.bz2
        dest: /root/
    - name: copy httpd
      copy:
        src: /root/httpd-2.4.46.tar.gz
        dest: /root/
    - name: install apr
      script: /root/lamp/apache/scripts/apr.sh
    - name: install apr-util
      script: /root/lamp/apache/scripts/apr_util.sh
    - name: install httpd
      script: /root/lamp/apache/scripts/httpd.sh
    - name: set config start
      script: /root/lamp/apache/scripts/config.sh
                                                                                      
//執行playbook
[root@ansible lamp]# ansible-playbook  apache/httpd.yml

mysql安裝:

[root@ansible ~]# ls
anaconda-ks.cfg  lamp  mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
 
//寫入配置
[root@ansible lamp]# vim mysql/scripts/install.sh
#!/bin/bash
#uncompress
tar  -xf /root/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
#link
ln -s /usr/local/mysql-5.7.31-linux-glibc2.12-x86_64/  /usr/local/mysql
#chown
chown -R mysql.mysql /usr/local/mysql*
#variable
echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/myslq.sh
source /etc/profile.d/myslq.sh
#include
ln -s /usr/local/mysql/include/  /usr/include/mysql
#lib
echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/msqly.conf
ldconfig
#initizlize mysql
/usr/local/mysql/bin/mysqld  --initialize  --user=mysql   --datadir=/mydata > /root/password 2>&1
#start script
cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
sed -ri 's#^(datadir=).*#\1/mydata#g' /etc/init.d/mysqld
#start mysql
service mysqld start
#set password
/usr/local/mysql/bin/mysql  -uroot -p"$(awk  '/password/{print$NF}' /root/password)"  --connect-expired-password -e "set password = password(\"root123\");"
 
//配置musql的模板
[root@ansible lamp]# vim mysql/templates/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /mydata
socket = /tmp/mysql.sock
port = 3306
pid-file = /mydata/mysql.pid
user = mysql
skip-name-resolve
        
            
#playbook
[root@ansible lamp]# vim  mysql/mysql.yml
---
- hosts: mysql
  vars:
    depend: ncurses-devel,openssl-devel,openssl,cmake,mariadb-devel,ncurses-compat-libs
  tasks:
    - name: install package
      yum:
       name: '{{ depend }}'
       state: present
    - name: copy mysql
      copy:
        src: /root/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
        dest: /root/
    - name: create user
      user:
        name: mysql
        system: yes
        create_home: no
        shell: /sbin/nologin
        state: present
    - name: create datadir
      file:
        path: /mydata
        owner: mysql
        group: mysql
        state: directory
    - name: my.cnf
      template:
        src: /root/lamp/mysql/templates/my.cnf
        dest: /etc/
    - name: script
      script: /root/lamp/mysql/scripts/install.sh
 
//執行playbook
[root@ansible lamp]# ansible-playbook    mysql/mysql.yml
//測試
[root@mysql ~]# mysql -uroot -p'root123'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.31 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

php安裝

#配置php
[root@ansible lamp]# vim php/scripts/phpcfg.sh
#!/bin/bash
echo -e  "<?php\n\tphpinfo();\n?>" > /var/www/html/index.php
chown -R apache.apache  /var/www/html/index.php
sed -i 's#listen = /run/php-fpm/www.sock#listen=0.0.0.0:9000#' /etc/php-fpm.d/www.conf
sed -i 's#127.0.0.1#192.168.220.20#' /etc/php-fpm.d/www.conf
 
#playbook
 
 
---
- hosts: php
  vars:
     package: libxml2,libxml2-devel,openssl,openssl-devel,bzip2,bzip2-devel,libcurl,libcurl-devel,libicu-devel,libjpeg,libjpeg-devel,libpng,libpng-devel,openldap-devel,pcre-devel,freetype,freetype-devel,gmp,gmp-devel,libmcrypt,libmcrypt-devel,readline,readline-devel,libxslt,libxslt-devel,mhash,mhash-devel,php-mysqlnd
  tasks:
    - name: install depend
      yum:
        name: '{{ package }}'
        state: present
    - name: install php
      yum:
        name: php-*
        state: present
    - name: config
      script: /root/lamp/php/scripts/phpcfg.sh
    - name: start php-fpm
      service:
        name: php-fpm
        state: started
 
//執行playbook
[root@ansible lamp]# ansible-playbook  php/php.yml

測試