第五章 5-1~5-5 Freestyle Job實戰成
- 一、三劍客環境介紹(Jenkins,Ansible,Gitlab)
- 二、三劍客環境搭建(Jenkins,Ansible,Gitlab)
- 2.1、驗證Jenkisns下的ansible環境和ssh免密登錄
- 2.2、編寫nginx_playbooks文件
- 2.2.1、進入nginx_playbooks文件夾中編寫deploy.yml文件
- 2.2.2、創建dev和prod文件
- 2.2.3、編寫prod文件
- 2.2.4、編寫dev文件
- 2.2.5、修改roles/nginx/files下的文件
- 2.2.6、修改roles/nginx/templates下的文件
- 2.3、編寫wordpress_playbooks文件
- 三、編寫playbook實現靜態網頁遠程部署
- 四、將playbook部署腳本提交到Gitlab
- 五、Freestyle任務構建和自動化部署
一、三劍客環境介紹(Jenkins,Ansible,Gitlab)
如上圖所示的一個交付的流程圖,我們需要準備三臺雲主機(也可以使用虛擬機來創建三臺虛擬機)。
需要準備的三臺主機分別安裝的環境和IP分別如下。
二、三劍客環境搭建(Jenkins,Ansible,Gitlab)
2.1、驗證Jenkisns下的ansible環境和ssh免密登錄
# 登錄到jenkins主機(203)
ssh [email protected]
# 切換到deploy用戶
su - deploy
# 加載Python3.6的環境
source /home/deploy/.py3-a2.5-env/bin/activate
# 在Python3.6環境中加載ansible
source /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q
# 測試ansible-playbook是否可用
ansible-playbook --version
# 測試是否可以通過遠程登錄到目標主機(testbox)
ssh [email protected]
2.2、編寫nginx_playbooks文件
在之前的windows機下的repo文件夾下,將之前ansible部分編寫的test_playbooks放入,然後打開git brash命令窗口;
# 拷貝一份test_playbooks並將文件夾重命名爲nginx_playbooks
cp -a test_playbooks nginx_playbooks
2.2.1、進入nginx_playbooks文件夾中編寫deploy.yml文件
修改爲下圖
2.2.2、創建dev和prod文件
將testenv文件,複製一份,並重命名爲dev,prod
2.2.3、編寫prod文件
下圖中,在[nginx]下可以添加多個dns記錄,對應多臺主機
2.2.4、編寫dev文件
2.2.5、修改roles/nginx/files下的文件
修改testbox文件夾的名稱爲nginx
刪除files文件夾下的foo.sh腳本文件
並創建health_check.sh腳本文件用來檢查網站的健康狀況
#!/bin/sh
# 將傳入的變量賦值給URL
URL=$1
curl -Is http://$URL > /dev/null && echo "The remote side is healthy" || echo "The remote side is failed, please check"
創建一個index.html文件
# 寫一個文本語句到index.html文件中
echo "This is my first website" > index.html
2.2.6、修改roles/nginx/templates下的文件
切換到templates文件夾
使用vim打開這個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;
}
}
}
2.3、編寫wordpress_playbooks文件
複製一份nginx_playbooks到wordpress_playbooks
2.3.1、編寫deploy.yml主入口文件
- hosts: "wordpress"
gather_facts: true
remote_user: root
roles:
- wordpress
2.3.2、編寫inventory下的文件
進入到inventory目錄下
2.3.2.1、編寫dev文件
[wordpress]
test.example.com
[wordpress:vars]
server_name=test.example.com
port=8080
user=deploy
worker_processes=2
max_open_file=30000
root=/data/www
gitlab_user='root'
gitlab_pass='nis123456'
2.3.2.1、編寫prod文件
# 將dev文件複製到prod文件中
cp -rf dev prod
可以看到和上面dev文件的內容一致
然後添加一個gitlab的賬號和密碼
gitlab_user='root'
gitlab_pass='nis123456'
完整的文件如下
三、編寫playbook實現靜態網頁遠程部署
進入到nginx_playbook文件夾中的/nginx_playbooks/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 }}"
四、將playbook部署腳本提交到Gitlab
之前編輯的playbook的文件的目錄結構如下所示。
在Git Bash命令窗口上將目錄位置移動到如下圖所示
在Gitlab上創建一個ansible-playbook-repo項目。然後通過下面提示的git語句將上面編寫的ansible的playbook提交到git上。
五、Freestyle任務構建和自動化部署
5.1、添加一個nginx-freestyle-job的自由風格的任務
5.2、添加描述
5.3、添加Git
複製git倉庫地址
將複製的git添加到下圖的配置中
5.4、添加參數
5.5、添加構建
#!/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 barnch=$barnch -e env=$deploy_env
5.6、測試構建
進入輸出控制檯
從上圖的輸出日誌中可以看到本次構建成功!
查看windows主機的hosts文件,添加如下的DNS記錄。
打開瀏覽器使用:test.example.com網址訪問
至此,freestyle構建部署腳本的實例演示成功!