码迷,mamicode.com
首页 > 其他好文 > 详细

ansible一键部署lnvamp

时间:2017-02-08 23:20:46      阅读:387      评论:0      收藏:0      [点我收藏+]

标签:ansible

     lnvamp的意思是Linux(centos7.2),Nginx,varnish,Apache,MySQL,PHP。我在这个平台之上构建的discuz论坛,我写了一个roles来实现,这个脚本很不健壮幂等性不足,需要大量的判断继续完善,由于时间原因我就不继续完善了,先把草稿贴出来。同时这个脚本的复用性也不好,基本上没有使用变量,后期会慢慢改善。

     架构图如下

技术分享技术分享

nginx提供反向代理,安装nginx

varnish提供缓存,安装varnish

server提供页面处理,安装httpd,php,php-mysql,nfs-utils

file提供数据库和网页文件,安装mysql-server,mysql-python,nfs-utils


file的ansible内容写法

注意

    因为手头没有主机了,这里file主机我使用的是centos6

     mysql-python没有对应的rpm包,由于考虑幂等性的原因这里我没有使用ansible来完成,我在宿主机上执行的,这里我们只好手动编译,手动编译脚本如下

yum install python-devel mysql-devel zlib-devel openssl-devel -y
yum groupinstall development tools -y
wget https://pypi.python.org/packages/9a/81/924d6799494cf7fb24370335c2f782088d6ac4f79e4137d4df94cea3582c/MySQL-python-1.2.3.tar.gz
tar xf MySQL-python-1.2.3.tar.gz
cd MySQL-python-1.2.3
python setup.py install

vim roles/filesystem/tasks/main.yml

- name: copy wordpress files
  copy: src=Discuz_X3.2_SC_UTF8.zip dest=/
- name: unzip wordpress files
  command: unzip /Discuz_X3.2_SC_UTF8.zip -d /
- name: chmod wordpress files
  command: chown -R apache:apache /wordpress
- name: copy nfs file
#- name: share wordpress files
#  file: dest=/wordpress mode=766 owner=root group=root state=directory
- name: copy wordpress files
  copy: src=Discuz_X3.2_SC_UTF8.zip dest=/
- name: unzip wordpress files
  command: unzip /Discuz_X3.2_SC_UTF8.zip -d /
- name: chmod wordpress files
  command: chown -R apache:apache /wordpress
- name: copy nfs file
  copy: src=exports dest=/etc/exports
- name: install nfs-utils
  yum: name=nfs-utils state=present
- name: start nfs server
  service: name=nfs state=started enabled=true
- name: install mysql-server
  yum: name=mysql-server state=present
- name: start mysql server
  service: name=mysqld state=started enabled=true
#- name: install mysql-python
#  yum: name=mysql-server state=present
- name: Create a new database with name ‘wpdb‘
  mysql_db: name=wpdb state=present
- name: Copy database sqlfile
  copy: src=grant.sql dest=/tmp
- name: Restore database
  mysql_db:
    name: wpdb
    state: import
    target: /tmp/grant.sql

vim roles/filesystem/files/exports

/wordpress 172.16.0.0/16(rw,no_root_squash)

vim roles/filesystem/files/grant.sql

grant all privileges on wpdb.* to wpuser@‘172.16.29.%‘identified by "wpprass";

server的ansible内容写法

vim roles/amp/tasks/main.yml

- name: install httpd
  yum: name=httpd state=present
- name: install php
  yum: name=php state=present
- name: install php-mysql
  yum: name=php-mysql state=present
- name: install nfs-utils
  yum: name=nfs-utils state=present
- file:
    path: /wordpress
    owner: apache
    group: apache
    state: directory
    mode: 0750
- name: copy fstab file
  copy: src=fstab dest=/etc/fstab
- name: copy http file
  copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
- name: mount wordpress files
  command: mount -a
- name: start httpd server
  service: name=httpd state=started enabled=true

vim roles/amp/files/httpd.conf

ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
<Directory />
    AllowOverride none
    Require all denied
</Directory>
DocumentRoot "/wordpress/upload"
<Directory "/var/www">
    AllowOverride None
    Require all granted
</Directory>
<Directory "/wordpress/upload">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>
<Files ".ht*">
    Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>
<IfModule mime_module>
    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf

vim roles/amp/files/fstab

UUID=098eb04e-16b2-4c66-8a02-943994f669cb /                       xfs     defaults        0 0
UUID=ddf83638-3fbf-48a6-8633-05e869a5dad1 /boot                   xfs     defaults        0 0
/dev/sda3   swap swap defaults 0 0
172.16.29.1:/wordpress /wordpress nfs defaults 0 0

nginx的ansible内容写法


vim roles/nginx/tasks/main.yml

- name: install nginx
  yum: name=nginx state=present
- name: copy nignx.conf file
  copy: src=nginx.conf dest=/etc/nginx/nginx.conf
- name: start nginx server
  service: name=nginx state=started enabled=true

 

vim roles/nginx/files/nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
    worker_connections 1024;
}
http {
    log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘
                      ‘$status $body_bytes_sent "$http_referer" ‘
                      ‘"$http_user_agent" "$http_x_forwarded_for"‘;
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    upstream websrvs {
        server 172.16.29.10:80;
        server 172.16.29.20:80;
        ip_hash;
    }
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        location / {
            proxy_pass http://172.16.29.30;
            proxy_set_header Host $host;
            proxy_set_header   X-Real-IP    $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}


varnish的ansible内容写法


vim roles/varnish/tasks/main.yml

- name: install varnish
  yum: name=varnish state=present
- name: copy varnish.params file
  copy: src=varnish.params dest=/etc/varnish/varnish.params
- name: copy default.vcl file
  copy: src=default.vcl dest=/etc/varnish/default.vcl
- name: start varnish server
  service: name=varnish state=started enabled=true
vim roles/varnish/files/varnish.params
RELOAD_VCL=1
VARNISH_VCL_CONF=/etc/varnish/default.vcl
VARNISH_LISTEN_PORT=80
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1
VARNISH_ADMIN_LISTEN_PORT=6082
VARNISH_SECRET_FILE=/etc/varnish/secret
VARNISH_STORAGE="malloc,512m"
VARNISH_TTL=120
VARNISH_USER=varnish
VARNISH_GROUP=varnish
vim roles/varnish/files/default.vcl

vcl 4.0;
import directors;
backend default {
    .host = "127.0.0.1";
    .port = "8080";
}
probe check {
    .url = "/robots.txt";
    .window = 5;
    .threshold = 3;
    .interval = 2s;
    .timeout = 1s;
}
backend server1 {
    .host = "172.16.29.10";
    .port = "80";
    .probe = check;
}
backend server2 {
    .host = "172.16.29.20";
    .port = "80";
    .probe = check;
}
sub vcl_init {
    new static = directors.round_robin();
    static.add_backend(server1);
    static.add_backend(server2);
}
sub vcl_recv {
    set req.backend_hint = static.backend();
}
sub vcl_backend_response {
}
sub vcl_deliver {
}



调用roles的ansible内容写法


file主机的ansible调用方法

vim file.yml
---
- hosts: file
  roles:
    - filesystem

server主机的ansible调用方法

vim amp.yml
---
- hosts: lamp
  roles:
    - amp

nginx主机的ansible调用方法

vim nginx.yml
---
- hosts: nginx
  roles:
    - nginx

varnish主机的ansible调用方法

vim varnish.yml
---
- hosts: varnish
  roles:
    - varnish



总结

     这个实验基本上没有说原理和操作步骤,只是简单罗列文件,最后我把我编写的完整roles的下载路径过一天补上,已经上传等待审核

技术分享


本文出自 “老王linux旅程” 博客,请务必保留此出处http://oldking.blog.51cto.com/10402759/1896183

ansible一键部署lnvamp

标签:ansible

原文地址:http://oldking.blog.51cto.com/10402759/1896183

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!