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 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 任务”

添加描述

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

参数化构建过程

添加构建步骤

在执行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”

查看输出信息


验证目标主机是否部署成功,在浏览器输入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自动化部署(五)的更多相关文章
- Jenkins+Gitlab+Ansible自动化部署(六)
Pipeline Job实现Nginix+MySQL+PHP+Wordpress实现自动化部署交付(Jenkins+Gitlab+Ansible自动化部署(五)https://www.cnblogs. ...
- Jenkins+Gitlab+Ansible自动化部署(三)
接Jenkins+Gitlab+Ansible自动化部署(一)https://www.cnblogs.com/zd520pyx1314/p/10210727.html 和(二)https://www. ...
- Jenkins+Gitlab+Ansible自动化部署(一)
首先准备实验环境 虚拟机 主机名 IP地址 服务 系统版本 内核版本 Vmware Workstation 14 gitlab.example.com 192.168.244.130 gitlab ...
- Jenkins+Gitlab+Ansible自动化部署(四)
接Jenkins+Gitlab+Ansible自动化部署(三)https://www.cnblogs.com/zd520pyx1314/p/10235394.html Jenkins应用 Jenkin ...
- Jenkins+Gitlab+Ansible自动化部署(二)
接Jenkins+Gitlab+Ansbile自动化部署(一):https://www.cnblogs.com/zd520pyx1314/p/10210727.html Ansible的配置与部署 工 ...
- 【开发工具】Jenkins+Gitlab实现自动化部署
我在尝试在容器中安装Jenkins时,初衷是希望使用docker in docker 的模式来实现Jenkins slave容器按需创建.在实现的时候需要在Jenkins 中安装Kubernetes插 ...
- 基于Jenkins+Gitlab的自动化部署实战
故事背景 一个中小型企业,是典型的互联网公司,当初期的时候可能运维只能标配到2~3人,此时随着公司的发展,项目会逐渐增多.前期部署项目可能都是手动的, 俗称“人肉部署”,这简直是无比的痛苦,不能忍受的 ...
- jenkins +gitlab +docker 自动化部署tomcat 项目
实验环境 实验设备 三台服务器 centos 7.X 以上 内存 2-3G左右 192.168.1.195 (jenkins最新+ git 2.8+maven 3.5 +tomcat 8+java1. ...
- 从Docker 到Jenkins 到Ansible的部署经验
从Docker 到Jenkins 到Ansible的部署经验 工作中,除了开发功能,还负责系统的部署工作.我从频繁的部署工作中,逐渐找到了一些偷懒的方法.从传统的Java -jar命令启动服务,到通过 ...
随机推荐
- 【MFC】动态创建CMFCToolbar图标不显示问题
最近遇到一个问题,需要动态的从xml文件读取一系列图标文件,加载到一个toolbar中,由于使用的是vs2008 with sp1 feature pack,自然想到用CMFCToolbar来做,思路 ...
- 白痴qwerta的胡言乱语(一句话日度感想?
10.2 >我tm吹爆这个Latex插件!!!太漂亮了吧?!!! 10.3 >什么鬼气考试 还有这考试的大家也肽水了吧 >再吹一次这个Latex插件!!! 10.6 >今天在琢 ...
- docker基于宿主机系统版本创建镜像
这里讲如何定制自己centos镜像,仅供测试docker使用. A) 安装软件 yum -y install febootstrap B)下载镜像febootstrap -i bash -i wget ...
- python3 + selenium + eclipse 中报错:'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
解决:提示chrome driver没有放置在正确的路径下,于是下载chrome dirver,然后放置到C:\Python36的目录下,再次运行就OK了!
- 常量指针-指向常量的指针,指针常量-指针本身是常量,常量-不能更改值的常量,数组指针-是指针int (*p)[n] 指针数组-是数组int *p[n]
1.常量指针 定义:具有只能够读取内存中数据,却不能够修改内存中数据的属性的指针,称为指向常量的指针,简称常量指针. 声明:const int * p; int const * p; 注:可以将一个常 ...
- day1 java基础回顾- 文件路径
绝对路径 以根目录或某盘符开头的路径(或者说完整的路径) 例如: l c:/a.txt (Windows操作系统中) l c:/xxx/a.txt (Windows操作系统中) l /var/x ...
- HDU 3572 Task Schedule (最大流)
C - Task Schedule Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- linux 下消息队列发送后没有信息
在使用消息队列时,调用 #include <stdio.h> #include <stdlib.h> #include <string.h> #include &l ...
- HDU - 3001 Travelling(三进制状压dp)
Travelling After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best ch ...
- HDU 2112 HDU Today Dij+map转化
HDU Today 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线,并 ...