更新代码
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