• Jenkins+ansible+Gitlab集成环境搭建


    一、概念图

    二、演示过程

      

    [root@gitleb ~]# git config --global http.sslVerify false #关闭安全认证,避免clone 时报错证书错误  
    
    [root@gitleb ~]# git clone https://gitlab.example.com/root/ansible-playbook-repo.git
    
    [root@gitleb ~]# cd ansible-playbook-repo.git
    
    修改文件内容后进行提交
    
    [root@gitleb nginx_playbooks]# git add  .
    
    [root@gitleb nginx_playbooks]# git commit -m"This is my first commit"
    
    [root@gitleb nginx_playbooks]# git push origin master 

            目录结构:

            

             文件内容:

              

      1 [root@gitleb nginx_playbooks]# ll
      2 总用量 8
      3 -rw-r--r-- 1 root root 17 2月  23 15:46 deploy.retry
      4 -rw-r--r-- 1 root root 79 2月  23 15:49 deploy.yml
      5 drwxrwxr-x 2 root root 29 2月  23 15:53 inventory
      6 drwxrwxr-x 3 root root 19 2月  23 15:54 roles
      7 [root@gitleb nginx_playbooks]# cat deploy.retry 
      8 test.example.com
      9 [root@gitleb nginx_playbooks]# cat deploy.yml 
     10 - hosts: "nginx"
     11   gather_facts: true
     12   remote_user: root
     13   roles:
     14     - nginx
     15 
     16 [root@gitleb nginx_playbooks]# cat inventory/prod 
     17 [nginx]
     18 test.example.com
     19 
     20 [nginx:vars]
     21 server_name=test.example.com
     22 port=80
     23 user=deploy
     24 worker_processes=4
     25 max_open_file=65505
     26 root=/www
     27 
     28 [root@gitleb nginx_playbooks]# cat inventory/dev 
     29 [nginx]
     30 test.example.com
     31 
     32 [nginx:vars]
     33 server_name=test.example.com
     34 port=80
     35 user=deploy
     36 worker_processes=4
     37 max_open_file=65505
     38 root=/www
     39 
     40 [root@gitleb nginx_playbooks]# cat roles/nginx/files/health_check.sh 
     41 #!/bin/sh
     42 
     43 URL=$1
     44 
     45 curl -Is http://$URL > /dev/null && echo "The remote side is healthy" || echo "The remote side is failed, please check"
    46 [root@gitleb nginx_playbooks]# cat roles/nginx/files/index.html 47 This is my first website 48 49 50 [root@gitleb nginx_playbooks]# cat roles/nginx/templates/nginx.conf.j2 51 # For more information on configuration, see: 52 user {{ user }}; 53 worker_processes {{ worker_processes }}; 54 55 error_log /var/log/nginx/error.log; 56 57 pid /var/run/nginx.pid; 58 59 events { 60 worker_connections {{ max_open_file }}; 61 } 62 63 64 http { 65 include /etc/nginx/mime.types; 66 default_type application/octet-stream; 67 68 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 69 '$status $body_bytes_sent "$http_referer" ' 70 '"$http_user_agent" "$http_x_forwarded_for"'; 71 72 access_log /var/log/nginx/access.log main; 73 74 sendfile on; 75 #tcp_nopush on; 76 77 #keepalive_timeout 0; 78 keepalive_timeout 65; 79 80 #gzip on; 81 82 # Load config files from the /etc/nginx/conf.d directory 83 # The default server is in conf.d/default.conf 84 #include /etc/nginx/conf.d/*.conf; 85 server { 86 listen {{ port }} default_server; 87 server_name {{ server_name }}; 88 89 #charset koi8-r; 90 91 #access_log logs/host.access.log main; 92 93 location / { 94 root {{ root }}; 95 index index.html index.htm; 96 } 97 98 error_page 404 /404.html; 99 location = /404.html { 100 root /usr/share/nginx/html; 101 } 102 103 # redirect server error pages to the static page /50x.html 104 # 105 error_page 500 502 503 504 /50x.html; 106 location = /50x.html { 107 root /usr/share/nginx/html; 108 } 109 110 } 111 112 } 113 114 115 116 [root@gitleb nginx_playbooks]# cat roles/nginx/tasks/main.yml 117 - name: Disable system firewall 118 service: name=firewalld state=stopped 119 120 - name: Disable SELINUX 121 selinux: state=disabled 122 123 - name: setup nginx yum source 124 yum: pkg=epel-release state=latest 125 126 - name: write then nginx config file 127 template: src=roles/nginx/templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf 128 129 - name: create nginx root folder 130 file: 'path={{ root }} state=directory owner={{ user }} group={{ user }} mode=0755' 131 132 - name: copy index.html to remote 133 copy: 'remote_src=no src=roles/nginx/files/index.html dest=/www/index.html mode=0755' 134 135 - name: restart nginx service 136 service: name=nginx state=restarted 137 138 - name: run the health check locally 139 shell: "sh roles/nginx/files/health_check.sh {{ server_name }}" 140 delegate_to: localhost 141 register: health_status 142 143 - debug: msg="{{ health_status.stdout }}"

    三、Jenkins 开始构建

     

    脚本

    #/bin/sh
    
    set +x
    source /home/deploy/.py3-a2.5-env/bin/activate
    source /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q
    
    cd $WORKSPACE/nginx-playbooks
    ansible --version
    ansible-playbook --version
    
    ansible-playbook -i inventory/$deploy_env ./deploy.yml -e project=nginx -e branch=$branch -e env=$deploy_env
    View Code

    查看构建日志:

     构建成功 !

    本地电脑添加hosts 解析后,访问我们构建的成果:

     至此,利用jenkins 下的 freestyle任务,集成gitlab与ansible 成功的将静态页面部署到到远程主机中,实现了一键自动化部署,持续交付的流程操作。

  • 相关阅读:
    「日常训练」Bad Luck Island(Codeforces Round 301 Div.2 D)
    「日常训练」Ice Cave(Codeforces Round 301 Div.2 C)
    「日常训练」School Marks(Codeforces Round 301 Div.2 B)
    「知识学习」二分图的最大匹配、完美匹配和匈牙利算法(HDU-2063)
    「日常训练」 Counting Cliques(HDU-5952)
    「日常训练」Maximum Multiple(HDU-6298)
    「日常训练」 Yukari's Birthday(ZOJ-3665)
    「Python」Numpy equivalent of MATLAB's cell array
    「Python」Convert map object to numpy array in python 3
    「Python」matplotlib备忘录
  • 原文地址:https://www.cnblogs.com/lanbojini/p/14437189.html
Copyright © 2020-2023  润新知