1. 程式人生 > >Jenkins+Gitlab+Ansible自動化部署(五)

Jenkins+Gitlab+Ansible自動化部署(五)

Freestyle Job實現靜態網站部署交付(接Jenkins+Gitlab+Ansible自動化部署(四)https://www.cnblogs.com/zd520pyx1314/p/10244504.html

  • 環境構建
  • 編寫ansible playbook指令碼實現靜態網頁遠端部署
  • 將playbook部署指令碼提交到GitLab倉庫
  • 構建Freestyle Job任務框架
  • Jenkins整合Ansible與Gitlab實現靜態網頁的自動化部署

首先確定自己的環境已經準備完畢。

登入gitlab檢視

登入Jenkins首頁

登入Jenkins主機檢視Ansible2.5+python 3.6虛擬環境

$ ssh [email protected]192.168.244.131
Last login: Wed Jan  9 13:40:48 2019 from 192.168.244.1
[[email protected] ~]# su - deploy
Last login: Wed Jan  9 20:24:43 CST 2019 on pts/0
[[email protected] ~]$ source /home/deploy/.py3-a2.5-env/bin/activate           (.py3-a2.5-env) [[email protected]
~]$ source /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q (.py3-a2.5-env) [[email protected] ~]$ ansible --version ansible 2.5.14 (stable-2.5 c748512c4c) last updated 2019/01/09 20:03:39 (GMT +800) config file = None configured module search path = ['/home/deploy/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules
'] ansible python module location = /home/deploy/.py3-a2.5-env/ansible/lib/ansible executable location = /home/deploy/.py3-a2.5-env/ansible/bin/ansible python version = 3.6.8 (default, Jan 9 2019, 19:44:42) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] (.py3-a2.5-env) [[email protected] ~]$

準備就緒,開始編寫ansible playbook指令碼實現靜態網頁遠端部署,開啟Git bash命令列,將之前建立好的ansible-playbook-repo倉庫clone到本地:

[email protected] MINGW64 ~
$ git config --global http.sslVerify false
[email protected] MINGW64 ~
$ cd Desktop/repo/
$  git clone https://gitlab.example.com/root/ansible-playbook-repo.git
Cloning into 'ansible-playbook-repo'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
[email protected] MINGW64 ~/Desktop/repo/ansible-playbook-repo (master)
$ ll
total 9
-rw-r--r-- 1 xueji 1049089   0 1月  10 11:57 ansible-playbook.txt
drwxr-xr-x 1 xueji 1049089   0 1月  10 12:02 nginx_playbooks/
-rw-r--r-- 1 xueji 1049089 312 7月  15 08:33 nginx-freestyle-job.sh
drwxr-xr-x 1 xueji 1049089   0 1月  10 12:02 test_playbooks/

開始配置

[email protected] MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master)
$ ll
total 2
-rw-r--r-- 1 xueji 1049089 17 7月  17 21:06 deploy.retry
-rw-r--r-- 1 xueji 1049089 80 7月  17 21:06 deploy.yml
drwxr-xr-x 1 xueji 1049089  0 1月  10 12:02 inventory/
drwxr-xr-x 1 xueji 1049089  0 1月  10 12:02 roles/

[email protected] MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master)
$ vim deploy.yml
- hosts: "nginx"
  gather_facts: true
  remote_user: root
  roles:
    - nginx

[email protected] MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master)
$ vim inventory/prod
[nginx]
test.example.com

[nginx:vars]
server_name=test.example.com
port=80
user=deploy
worker_processes=4
max_open_file=65505
root=/www

[email protected] MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master)
$ vim inventory/dev
[nginx]
test.example.com

[nginx:vars]
server_name=test.example.com
port=80
user=deploy
worker_processes=4
max_open_file=65505
root=/www

[email protected] MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master)
$ vim roles/nginx/files/health_check.sh
#!/bin/sh

URL=$1

curl -Is http://$URL > /dev/null && echo "The remote side is healthy" || echo "The remote side is failed, please check"

[email protected] MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master)
$ cat roles/nginx/files/index.html
This is my first website


[email protected] MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master)
$ vim roles/nginx/templates/nginx.conf.j2
# For more information on configuration, see:
user              {{ user }};
worker_processes  {{ worker_processes }};

error_log  /var/log/nginx/error.log;

pid        /var/run/nginx.pid;

events {
    worker_connections  {{ max_open_file }};
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    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;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    # Load config files from the /etc/nginx/conf.d directory
    # The default server is in conf.d/default.conf
    #include /etc/nginx/conf.d/*.conf;
    server {
        listen       {{ port }} default_server;
        server_name  {{ server_name }};

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        error_page  404              /404.html;
        location = /404.html {
            root   /usr/share/nginx/html;
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

    }

}


[email protected] MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master)
$ vim roles/nginx/tasks/main.yml
- name: Disable system firewall
  service: name=firewalld state=stopped

- name: Disable SELINUX
  selinux: state=disabled

- name: setup nginx yum source
  yum: pkg=epel-release state=latest

- name: write then nginx config file
  template: src=roles/nginx/templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf

- name: create nginx root folder
  file: 'path={{ root }} state=directory owner={{ user }} group={{ user }} mode=0755'

- name: copy index.html to remote
  copy: 'remote_src=no src=roles/nginx/files/index.html dest=/www/index.html mode=0755'

- name: restart nginx service
  service: name=nginx state=restarted

- name: run the health check locally
  shell: "sh roles/nginx/files/health_check.sh {{ server_name }}"
  delegate_to: localhost
  register: health_status

- debug: msg="{{ health_status.stdout }}"

將配置好的檔案,提交到遠端gitlab

[email protected] MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master)
$ cd ~/Desktop/repo/ansible-playbook-repo/

[email protected] MINGW64 ~/Desktop/repo/ansible-playbook-repo (master)
$ git add .

[email protected] MINGW64 ~/Desktop/repo/ansible-playbook-repo (master)
$ git commit -m"First commit"
[master 06431dc] First commit
 8 files changed, 122 deletions(-)
 delete mode 100644 test_playbooks/deploy.retry
 delete mode 100644 test_playbooks/deploy.yml
 delete mode 100644 test_playbooks/inventory/dev
 delete mode 100644 test_playbooks/inventory/prod
 delete mode 100644 test_playbooks/roles/nginx/files/health_check.sh
 delete mode 100644 test_playbooks/roles/nginx/files/index.html
 delete mode 100644 test_playbooks/roles/nginx/tasks/main.yml
 delete mode 100644 test_playbooks/roles/nginx/templates/nginx.conf.j2

[email protected] MINGW64 ~/Desktop/repo/ansible-playbook-repo (master)
$ git push origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 212 bytes | 212.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
To https://gitlab.example.com/root/ansible-playbook-repo.git
   169dec7..06431dc  master -> master

返回到Jenkins web管理頁,點選“New 任務”

新增描述

新增Git倉庫(該倉庫地址即為上述配置的ansible-playbook-repo的倉庫地址)

引數化構建過程

新增構建步驟

在執行shell彈出的輸入框內輸入以下內容

#/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

點選“Save”,然後點選“Build with Parameters”,在右側下拉列表中選擇dev,點選“Build”

檢視輸出資訊

驗證目標主機是否部署成功,在瀏覽器輸入test.exmaple.com檢視

前提是保證本地windows主機下的C:\Windows\System32\drivers\etc\hosts檔案中末尾有如下對應關係

192.168.244.130 gitlab.example.com
192.168.244.131 jenkins.example.com
192.168.244.132 ansible.example.com
192.168.244.133 test.example.com

可以看到已經成功部署~