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

时间:2023-12-22 16:15:02

Freestyle Job实现静态网站部署交付(接Jenkins+Gitlab+Ansible自动化部署(四)https://www.cnblogs.com/zd520pyx1314/p/10244504.html

  • 环境构建
  • 编写ansible playbook脚本实现静态网页远程部署
  • 将playbook部署脚本提交到GitLab仓库
  • 构建Freestyle Job任务框架
  • Jenkins集成Ansible与Gitlab实现静态网页的自动化部署

首先确定自己的环境已经准备完毕。

登录gitlab查看

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

登录Jenkins首页

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

登录Jenkins主机查看Ansible2.5+python 3.6虚拟环境

$ ssh root@192.168.244.131
Last login: Wed Jan :: from 192.168.244.1
[root@jenkins ~]# su - deploy
Last login: Wed Jan :: CST on pts/
[deploy@jenkins ~]$ source /home/deploy/.py3-a2.-env/bin/activate (.py3-a2.-env) [deploy@jenkins ~]$ source /home/deploy/.py3-a2.-env/ansible/hacking/env-setup -q
(.py3-a2.-env) [deploy@jenkins ~]$ ansible --version
ansible 2.5. (stable-2.5 c748512c4c) last updated // :: (GMT +)
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.-env/ansible/lib/ansible
executable location = /home/deploy/.py3-a2.-env/ansible/bin/ansible
python version = 3.6. (default, Jan , ::) [GCC 4.8. (Red Hat 4.8.-)]
(.py3-a2.-env) [deploy@jenkins ~]$

准备就绪,开始编写ansible playbook脚本实现静态网页远程部署,打开Git bash命令行,将之前创建好的ansible-playbook-repo仓库clone到本地:

xueji@xueji MINGW64 ~
$ git config --global http.sslVerify false
xueji@xueji MINGW64 ~
$ cd Desktop/repo/
$ git clone https://gitlab.example.com/root/ansible-playbook-repo.git
Cloning into 'ansible-playbook-repo'...
remote: Enumerating objects: , done.
remote: Counting objects: % (/), done.
remote: Total (delta ), reused (delta )
Unpacking objects: % (/), done.
xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo (master)
$ ll
total
-rw-r--r-- xueji 1月 : ansible-playbook.txt
drwxr-xr-x xueji 1月 : nginx_playbooks/
-rw-r--r-- xueji 7月 : nginx-freestyle-job.sh
drwxr-xr-x xueji 1月 : test_playbooks/

开始配置

xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master)
$ ll
total
-rw-r--r-- xueji 7月 : deploy.retry
-rw-r--r-- xueji 7月 : deploy.yml
drwxr-xr-x xueji 1月 : inventory/
drwxr-xr-x xueji 1月 : roles/ xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master)
$ vim deploy.yml
- hosts: "nginx"
gather_facts: true
remote_user: root
roles:
- nginx xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master)
$ vim inventory/prod
[nginx]
test.example.com [nginx:vars]
server_name=test.example.com
port=
user=deploy
worker_processes=
max_open_file=
root=/www xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master)
$ vim inventory/dev
[nginx]
test.example.com [nginx:vars]
server_name=test.example.com
port=
user=deploy
worker_processes=
max_open_file=
root=/www xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master)
$ vim roles/nginx/files/health_check.sh
#!/bin/sh URL=$ curl -Is http://$URL > /dev/null && echo "The remote side is healthy" || echo "The remote side is failed, please check" xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master)
$ cat roles/nginx/files/index.html
This is my first website xueji@xueji 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 ;
keepalive_timeout ; #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;
} } } xueji@xueji 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

xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo/nginx_playbooks (master)
$ cd ~/Desktop/repo/ansible-playbook-repo/ xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo (master)
$ git add . xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo (master)
$ git commit -m"First commit"
[master 06431dc] First commit
files changed, deletions(-)
delete mode test_playbooks/deploy.retry
delete mode test_playbooks/deploy.yml
delete mode test_playbooks/inventory/dev
delete mode test_playbooks/inventory/prod
delete mode test_playbooks/roles/nginx/files/health_check.sh
delete mode test_playbooks/roles/nginx/files/index.html
delete mode test_playbooks/roles/nginx/tasks/main.yml
delete mode test_playbooks/roles/nginx/templates/nginx.conf.j2 xueji@xueji MINGW64 ~/Desktop/repo/ansible-playbook-repo (master)
$ git push origin master
Enumerating objects: , done.
Counting objects: % (/), done.
Delta compression using up to threads
Compressing objects: % (/), done.
Writing objects: % (/), bytes | 212.00 KiB/s, done.
Total (delta ), reused (delta )
To https://gitlab.example.com/root/ansible-playbook-repo.git
169dec7..06431dc master -> master

返回到Jenkins web管理页,点击“New 任务”

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

添加描述

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

添加Git仓库(该仓库地址即为上述配置的ansible-playbook-repo的仓库地址)

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

参数化构建过程

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

添加构建步骤

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

在执行shell弹出的输入框内输入以下内容

#/bin/sh

set +x
source /home/deploy/.py3-a2.-env/bin/activate
source /home/deploy/.py3-a2.-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”

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

查看输出信息

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

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

验证目标主机是否部署成功,在浏览器输入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

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

可以看到已经成功部署~