更新代码
web01 \<----------------|
        \               ^ 
          slb01 -->  jenkins  -->  gitlab   --> dev
        /               |
web02 /<----------------|
           更新代码

1.web服务器准备

192.168.1.43
192.168.1.254
1.1 nginx配置文件

cat /etc/nginx/conf.d/81.conf

server {
        listen       81;
        server_name  localhost;
        access_log /var/log/nginx/test_81_access.log;
        error_log  /var/log/nginx/test_81_error.log;

        location / {
            root   /html/web;
            index  index.html index.htm;
        }
}

2. slb服务器准备

192.168.1.191

nginx七层反向代理 upstream

root@rongbiz:~# cat /etc/nginx/conf.d/slb.conf 
upstream ansible_slb {
   server 192.168.1.43:81;
   server 192.168.1.254:81;
}

upstream ansible_slb_test {
   server 192.168.1.191:81 weight=1;
   server 10.1.0.101:81  weight=1;
}

server {
    listen 80;
    server_name test.yangtao.com;
    access_log /var/log/nginx/ansible_slb_test_access.log;
    error_log  /var/log/nginx/ansible_slb_test_error.log;
    location / {
      proxy_pass http://ansible_slb_test;
  }
}

server {
    listen 80;
    server_name web.yangtao.com;
    access_log /var/log/nginx/ansible_slb_access.log;
    error_log  /var/log/nginx/ansible_slb_error.log;
    location / {
      proxy_pass http://ansible_slb;
  }
}

3.jenkins 配置

3.1 ansible配置

3.1.1 inventory主机配置文件
[root@jenkins code_update]# cat hosts_test 
[webservice]
192.168.1.191
[root@jenkins code_update]# cat hosts_prod 
[webservice]
192.168.1.43
192.168.1.254

3.1.2 ansible role集合文件
[root@jenkins code_update]# cat ../code_update.yaml 
- hosts: webservice 
  serial: 1          #每次并发执行主机数量是2个
  roles:
    - code_update 
3.1.3 ansible task文件
[root@jenkins code_update]# cat tasks/main.yaml 
- name: 0. Get System Time
  shell:
    cmd: "echo $(date +%F_%H_%M)"
  register: Date
  delegate_to: 127.0.0.1

- name: 1. Get System Time
  shell:
    cmd: "echo ${git_commit_id}|cut -c 1-8"
  register: Git_Commit_Id
  delegate_to: 127.0.0.1
- name: Print Git_Commit_Id
  debug:
    msg:
      - " 打印 git commit id {{ Git_Commit_Id.stdout }}"

- name: 2. Get Workspace Path
  shell:
    cmd: "echo ${WORKSPACE}"
  register: Workspace
  delegate_to: 127.0.0.1

- name: 3. Archive Src Code
  archive:
    path: "{{ Workspace.stdout }}/*"
    dest: "/opt/web_{{ Date.stdout }}_{{ Git_Commit_Id.stdout }}.tar.gz"
  delegate_to: 127.0.0.1

- name: 4. Create Web Site Directory
  file:
    path: "/html/web_{{ Date.stdout }}_{{ Git_Commit_Id.stdout }}"
    state: directory
    recurse: yes

- name: 5. Unarchive Web Code
  unarchive:
    src: "/opt/web_{{ Date.stdout }}_{{ Git_Commit_Id.stdout }}.tar.gz"
    dest: "/html/web_{{ Date.stdout }}_{{ Git_Commit_Id.stdout }}/"

- name: 6. Remove The Service
  replace:
    regexp: "server {{ ansible_default_ipv4.address }}"
    path: /etc/nginx/conf.d/slb.conf
    replace: "#server {{ ansible_default_ipv4.address }}" 
  delegate_to: 192.168.1.191
  notify: Reload_Nginx

- meta: flush_handlers


- name: 7. Unlink Path
  file:
    path: "/html/web"
    state: absent

- name: 8. Create Link Path
  file:
    src: "/html/web_{{ Date.stdout }}_{{ Git_Commit_Id.stdout }}"
    path: "/html/web"
    state: link

- name: Test Web Info #测试用 可以删除        
  shell: 
    cmd: echo "{{ ansible_default_ipv4.address.split('.')[-1] }}" >> /html/web/1.html


- name: Wait 20s       #测试用 可以删除
  command:
    cmd: sleep 20s

- name: 9. Create The Service
  replace:
    regexp: "#server {{ ansible_default_ipv4.address }}"
    path: /etc/nginx/conf.d/slb.conf
    replace: "server {{ ansible_default_ipv4.address }}"
  delegate_to: 192.168.1.191
  notify: Reload_Nginx

- meta: flush_handlers
3.1.4 ansible handlers触发器
[root@jenkins code_update]# cat handlers/main.yaml 
- name: Reload_Nginx
  command:
    cmd: nginx -s reload 
  delegate_to: 192.168.1.191
3.2 git选项参数 git_commit_id 配置

3.3 git远程仓库配置

4.构建

原文地址:http://www.cnblogs.com/yangtao416/p/16880227.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性