首页 > 其他分享 >6.jenkins调用ansible实现滚动更新

6.jenkins调用ansible实现滚动更新

时间:2022-11-11 13:34:05浏览次数:42  
标签:滚动 log 192.168 server nginx ansible jenkins name

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

标签:滚动,log,192.168,server,nginx,ansible,jenkins,name
From: https://www.cnblogs.com/yangtao416/p/16880227.html

相关文章

  • Jenkins + Docker + Spring Boot实现自动化部署
    环境:CentOS7+Git(Gitee)实现步骤:在Docker安装Jenkins,配置Jenkins基本信息,利用Dockerfile和Shell脚本实现项目自动拉取打包并运行。一、安装Docker1、下载......
  • ansible配置mysql主从
    ansible配置mysql主从主机清单[root@ansible~]#cd/etc/ansible/[root@ansible/etc/ansible]#vimhosts...[mysql]node1node2[mysql_master]node1[mysql_slave......
  • Fix 手记|Jenkins 构建 Apk 报错:error=13,Permission denied
    文章备份~前言最近入职新公司,测试打包时突然提示Jenkins构建失败,随后个人尝试了一波,其实也就是通过互联网的力量查询解决问题的方式而已。特此记录。异常以及解决方案首先......
  • ansible角色部署mysql主从复制
    ansible角色部署mysql主从复制[root@ansibleansible]#vimhosts[mysql]node1node4[mysql_master]node1[mysql_slave]node4[root@ansibleansible]#cdrole......
  • Jenkins用户管理(二):不同用户分配不同的任务访问权限
    需求:不同用户访问到不同的Jenkins任务。依赖插件:Role-basedAuthorizationStrategy 1.插件安装进入【系统管理】-【插件管理】-【可用插件】,搜索Role-basedAu......
  • vue3中使用vue3-seamless-scroll(最新版本滚动插件)
    npm安装npminstallvue3-seamless-scroll--saveyarn安装yarnaddvue3-seamless-scrollbrowser安装<scriptsrc="https://unpkg.com/browse/vue3-seamless-scr......
  • Jenkins 安全 tips
    Jenkins作为一个开放的、可定制的平台,即使在默认状态下也提供了不错的安全性。但是鉴于Jenkins连接了许多行业工具,因此也存在一定安全隐患。本篇文章将会介绍一些方法和......
  • 超详细的 Jenkins 安全tips
    Jenkins作为一个开放的、可定制的平台,即使在默认状态下也提供了不错的安全性。但是鉴于Jenkins连接了许多行业工具,因此也存在一定安全隐患。本篇文章将会介绍一些方法......
  • jenkins配置项目CICD
    1、插件安装MavenIntegrationpluginGitpluginGiteePluginAnsiColorGenericWebhookTriggerPluginEmailExtensionPluginLocalization:Chinese(Simplified......
  • Jenkins: Generic Webhook Trigger
     GitHub:PayloadURL:https://jenkins-new.jam.only.sap/generic-webhook-trigger/invoke?token=resurrect&jobQuietPeriod=1      Pipeline:pipe......