Jenkins机器安装ansbile
[root@jenkins ~]# yum -y install epel-release ; yum -y install ansible[root@jenkins ~]# ansible --versionansible 2.9.27config file = /etc/ansible/ansible.cfgconfigured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']ansible python module location = /usr/lib/python2.7/site-packages/ansibleexecutable location = /usr/bin/ansiblepython version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
配置ansible主机清单
[root@jenkins ~]# cat /etc/hosts192.168.91.135 tomcat1[root@jenkins ~]# cat /etc/ansible/hosts[tomcat-server]tomcat1[root@jenkins ~]# ssh-keygen #一路回车[root@jenkins ~]# ssh-copy-id tomcat1[root@jenkins ~]# ansible -i /etc/ansible/hosts tomcat1 -m ping
jenkins安装ansible插件

jenkins配置ansible工具

Jenkins使用ansible命令





其实理论上应该需要提前配置Credentials,如下:


[root@jenkins ~]# cat /root/.ssh/id_rsa



jenkins使用ansible-playbook剧本
[root@jenkins ~]# cat /etc/ansible/test.yaml---- hosts: tomcat-servertasks:- name: ipshell: ip aregister: result # 显示结果为"result"- debug: var=result # 将显示结果var赋值给result


jenkins+ansible构建maven项目
jenkins安装git
[root@jenkins ~]# yum -y install git
添加maven和jdk工具

jenkins配置gitee公钥
[root@jenkins ~]# cat /root/.ssh/id_rsa.pub
项目配置

git参数化构建

[root@jenkins ~]# cat /root/.ssh/id_rsa

gitee仓库地址:git@gitee.com:youngfit/easy-springmvc-maven.git
配置maven构建参数
配置ansible运行指令



tomcat服务器配置
安装jdk和tomcat
配置环境变量
安装jdk8和tomcat[root@tomcat local]# cat /etc/profileTOMCAT_HOME=/data/application/tomcatJAVA_HOME=/usr/local/javaMAVEN_HOME=/usr/local/java/mavenCLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jarPATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/binexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
配置脚本
[root@tomcat local]# cat /opt/script/app-jenkins.sh#!/usr/bin/bash#本脚本适用于jenkins持续集成,实现备份war包到代码更新上线!使用时请注意全局变量。#================#Defining variablesexport JAVA_HOME=/usr/local/javawebapp_path="/data/application/tomcat/webapps"tomcat_run="/data/application/tomcat/bin"updata_path="/data/update/`date +%F-%T`"backup_path="/data/backup/`date +%F-%T`"tomcat_pid=`ps -ef | grep tomcat | grep -v grep | awk '{print $2}'`files_dir="easy-springmvc-maven"files="easy-springmvc-maven.war"job_path="/root/upload"#Preparation environmentecho "Creating related directory"mkdir -p $updata_pathmkdir -p $backup_pathecho "Move the uploaded war package to the update directory"mv $job_path/$files $updata_pathecho "========================================================="cd /optecho "Backing up java project"if [ -f $webapp_path/$files ];thentar czf $backup_path/`date +%F-%H`.tar.gz $webapp_pathif [ $? -ne 0 ];thenecho "打包失败,自动退出"exit 1elseecho "Checking if tomcat is started"if [ -n "$tomcat_pid" ];thenkill -9 $tomcat_pidif [ $? -ne 0 ];thenecho "tomcat关闭失败,将会自动退出"exit 2fificd $webapp_pathrm -rf $files && rm -rf $files_dircp $updata_path/$files $webapp_pathcd /opt$tomcat_run/startup.shsleep 5echo "显示tomcat的pid"echo "`ps -ef | grep tomcat | grep -v grep | awk '{print $2}'`"echo "tomcat startup"echo "请手动查看tomcat日志。脚本将会自动退出"fielseecho "Checking if tomcat is started"if [ -n "$tomcat_pid" ];thenkill -9 $tomcat_pidif [ $? -ne 0 ];thenecho "tomcat关闭失败,将会自动退出"exit 2fificp $updata_path/$files $webapp_path$tomcat_run/startup.shsleep 5echo "显示tomcat的pid"echo "`ps -ef | grep tomcat | grep -v grep | awk '{print $2}'`"echo "tomcat startup"echo "请手动查看tomcat日志。脚本将会自动退出"fi
构建测试

验证


来一次版本回退试试


没毛病

