Jenkins机器安装ansbile

  1. [root@jenkins ~]# yum -y install epel-release ; yum -y install ansible
  2. [root@jenkins ~]# ansible --version
  3. ansible 2.9.27
  4. config file = /etc/ansible/ansible.cfg
  5. configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  6. ansible python module location = /usr/lib/python2.7/site-packages/ansible
  7. executable location = /usr/bin/ansible
  8. python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]

配置ansible主机清单

  1. [root@jenkins ~]# cat /etc/hosts
  2. 192.168.91.135 tomcat1
  3. [root@jenkins ~]# cat /etc/ansible/hosts
  4. [tomcat-server]
  5. tomcat1
  6. [root@jenkins ~]# ssh-keygen #一路回车
  7. [root@jenkins ~]# ssh-copy-id tomcat1
  8. [root@jenkins ~]# ansible -i /etc/ansible/hosts tomcat1 -m ping

ansible主机清单

jenkins安装ansible插件

jenkins安装ansible插件
jenkins安装ansible插件

jenkins配置ansible工具

jenkins配置ansible工具

Jenkins使用ansible命令

Jenkins使用ansible命令

Jenkins使用ansible命令

image.png

image.png

image.png

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

image.png

image.png

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

image.png

image.png

image.png

jenkins使用ansible-playbook剧本

  1. [root@jenkins ~]# cat /etc/ansible/test.yaml
  2. ---
  3. - hosts: tomcat-server
  4. tasks:
  5. - name: ip
  6. shell: ip a
  7. register: result # 显示结果为"result"
  8. - debug: var=result # 将显示结果var赋值给result

image.png

image.png

image.png

jenkins+ansible构建maven项目

jenkins安装git

  1. [root@jenkins ~]# yum -y install git

添加maven和jdk工具

image.png

image.png

jenkins配置gitee公钥

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

image.png

项目配置

image.png

image.png

git参数化构建

image.png

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

image.png
gitee仓库地址:git@gitee.com:youngfit/easy-springmvc-maven.git

image.png

配置maven构建参数

image.png

配置ansible运行指令

image.png

image.png

image.png

image.png
—>保存

tomcat服务器配置

安装jdk和tomcat

配置环境变量

  1. 安装jdk8tomcat
  2. [root@tomcat local]# cat /etc/profile
  3. TOMCAT_HOME=/data/application/tomcat
  4. JAVA_HOME=/usr/local/java
  5. MAVEN_HOME=/usr/local/java/maven
  6. CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
  7. PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin
  8. export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH

配置脚本

  1. [root@tomcat local]# cat /opt/script/app-jenkins.sh
  2. #!/usr/bin/bash
  3. #本脚本适用于jenkins持续集成,实现备份war包到代码更新上线!使用时请注意全局变量。
  4. #================
  5. #Defining variables
  6. export JAVA_HOME=/usr/local/java
  7. webapp_path="/data/application/tomcat/webapps"
  8. tomcat_run="/data/application/tomcat/bin"
  9. updata_path="/data/update/`date +%F-%T`"
  10. backup_path="/data/backup/`date +%F-%T`"
  11. tomcat_pid=`ps -ef | grep tomcat | grep -v grep | awk '{print $2}'`
  12. files_dir="easy-springmvc-maven"
  13. files="easy-springmvc-maven.war"
  14. job_path="/root/upload"
  15. #Preparation environment
  16. echo "Creating related directory"
  17. mkdir -p $updata_path
  18. mkdir -p $backup_path
  19. echo "Move the uploaded war package to the update directory"
  20. mv $job_path/$files $updata_path
  21. echo "========================================================="
  22. cd /opt
  23. echo "Backing up java project"
  24. if [ -f $webapp_path/$files ];then
  25. tar czf $backup_path/`date +%F-%H`.tar.gz $webapp_path
  26. if [ $? -ne 0 ];then
  27. echo "打包失败,自动退出"
  28. exit 1
  29. else
  30. echo "Checking if tomcat is started"
  31. if [ -n "$tomcat_pid" ];then
  32. kill -9 $tomcat_pid
  33. if [ $? -ne 0 ];then
  34. echo "tomcat关闭失败,将会自动退出"
  35. exit 2
  36. fi
  37. fi
  38. cd $webapp_path
  39. rm -rf $files && rm -rf $files_dir
  40. cp $updata_path/$files $webapp_path
  41. cd /opt
  42. $tomcat_run/startup.sh
  43. sleep 5
  44. echo "显示tomcat的pid"
  45. echo "`ps -ef | grep tomcat | grep -v grep | awk '{print $2}'`"
  46. echo "tomcat startup"
  47. echo "请手动查看tomcat日志。脚本将会自动退出"
  48. fi
  49. else
  50. echo "Checking if tomcat is started"
  51. if [ -n "$tomcat_pid" ];then
  52. kill -9 $tomcat_pid
  53. if [ $? -ne 0 ];then
  54. echo "tomcat关闭失败,将会自动退出"
  55. exit 2
  56. fi
  57. fi
  58. cp $updata_path/$files $webapp_path
  59. $tomcat_run/startup.sh
  60. sleep 5
  61. echo "显示tomcat的pid"
  62. echo "`ps -ef | grep tomcat | grep -v grep | awk '{print $2}'`"
  63. echo "tomcat startup"
  64. echo "请手动查看tomcat日志。脚本将会自动退出"
  65. fi

构建测试

image.png

image.png

验证

image.png

image.png
来一次版本回退试试

image.png

image.png
没毛病