上篇文章中,我们将Ansible进行了容器化。在这篇文章中我们将利用Ansible镜像集成到Jenkins的Docker Cloud中,这个过程与之前的Jenkins Slave集成、RobotFramework自动化测试集成、Jmeter性能测试集成一样。
1、自动化部署节点
我们可以按照Slave、Robot、Jmeter节点方式来将Ansible自动化部署节点加入到Jenkins中,目的为了将Ansible自动化部署成为流水线的一部分。
在此之前我们需要将Ansible容器化,具体可以参考Devops关键工具及技术(八)—基于Pipeline的Ansible自动化部署(Ansible容器化)。
- JenkinsMaster上配置Ansible自动化部署节点的信息
在做配置之前我们也还是需要对该加入进来的节点做一下配置,具体的操作可以从上面提到的文章中获知,这里不做过多讲解。可参考下面的截图
2、Pipeline流水线的集成
基于之前Jmeter性能测试的流水线,我们加入Ansible自动化部署的Stage。在此之前我们的Pipeline里面加入了Checkout Code、Mvn Build、Sonar、Bash Deploy、RobotFramework、Jmeter等Stage,这次我们在后面加上Ansible的Stage。
- Ansible自动化部署脚本
脚本可以在这里找到:https://github.com/zbbkeepgoing/springboot-demo/tree/master/ansible
hosts文件
[all]
192.168.88.128 ansible_ssh_user=root ansible_connection=ssh ansible_ssh_pass=1qaz@WSX
deploy.yaml文件
- name: deploy demo jar
hosts: all
gather_facts: no
remote_user: root
roles:
- demo
roles/demo/tasks/main.yaml文件
- name: Copy sample.jar to remote vm //拷贝文件
copy: src=/opt/tmp/sample.jar dest=~/ mode=0755
- name: Get running sample.jar //得到原进程id
shell: "ps -ef | grep -v grep | grep -w sample.jar | awk '{print $2}'"
register: running_processes
- name: Kill running sample.jar //杀死原进程
shell: "kill {
{ item }}"
with_items: "{
{ running_processes.stdout_lines }}"
- wait_for: //确认进程是否已删除
path: "/proc/{
{ item }}/status"
state: absent
with_items: "{
{ running_processes.stdout_lines }}"
ignore_errors: yes
register: killed_processes
- name: Force kill stuck sample.jar //强制删除进程
shell: "kill -9 {
{ item }}"
with_items: "{
{ killed_processes.results | select('failed') | map(attribute='item') | list }}"
- name: Sleep 10s //睡眠10s
shell: sleep 10s;
- name: Deploy sample.jar //部署新工程
shell: nohup java -jar ~/sample.jar > ~/nohup.out &
- Pipeline内容
内容也可以在Github中找到
https://github.com/zbbkeepgoing/pipeline-sample
pipeline {
agent none
stages {
stage('Preparation') {
agent { node { label 'master' } }
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'binbin', url: 'https://github.c