设为首页 收藏本站
查看: 1450|回复: 1

[经验分享] ansible一键部署lnvamp

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-2-10 09:01:21 | 显示全部楼层 |阅读模式
  lnvamp的意思是Linux(centos7.2),Nginx,varnish,Apache,MySQL,PHP。我在这个平台之上构建的discuz论坛,我写了一个roles来实现,这个脚本很不健壮幂等性不足,需要大量的判断继续完善,由于时间原因我就不继续完善了,先把草稿贴出来。同时这个脚本的复用性也不好,基本上没有使用变量,后期会慢慢改善。     架构图如下
spacer.jpg QQ截图20170210090001.png
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来完成,我在宿主机上执行的,这里我们只好手动编译,手动编译脚本如下
1
2
3
4
5
6
yum install python-devel mysql-devel zlib-devel openssl-devel -y
yum groupinstall development tools -y
wget https://pypi.python.org/packages ... 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
- 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
1
/wordpress 172.16.0.0/16(rw,no_root_squash)



vim roles/filesystem/files/grant.sql
1
grant all privileges on wpdb.* to wpuser@'172.16.29.%'identified by "wpprass";



server的ansible内容写法
vim roles/amp/tasks/main.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
1
2
3
4
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
1
2
3
4
5
6
- 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- 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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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调用方法
1
2
3
4
5
vim file.yml
---
- hosts: file
  roles:
    - filesystem



server主机的ansible调用方法
1
2
3
4
5
vim amp.yml
---
- hosts: lamp
  roles:
    - amp



nginx主机的ansible调用方法
1
2
3
4
5
vim nginx.yml
---
- hosts: nginx
  roles:
    - nginx



varnish主机的ansible调用方法
1
2
3
4
5
vim varnish.yml
---
- hosts: varnish
  roles:
    - varnish







运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-339958-1-1.html 上篇帖子: Linux轻量级自动运维工具-Ansible浅析 下篇帖子: ansible安装
累计签到:16 天
连续签到:1 天
发表于 2017-2-13 10:52:12 | 显示全部楼层
回复挣积分

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表