首先安装Jenkins
https://blog.csdn.net/qq_32272499/article/details/103717463
安装ansible
https://blog.csdn.net/qq_32272499/article/details/103718037
在Jenkins 管理页面
1、安装ansible 插件
2、添加ansible 工具配置
ansible2.xx
/usr/bin (是ansible 默认安装路径 也有/usr/local/bin/)
3、添加凭证(目标服务器的密码)
4、配置构建(点击构建)
先添加Invoke Ansible Ad-Hoc Command
再添加Invoke Ansible Playbook
依次添加
所有配置保存后配置演员和剧本
/etc/ansible/create_user.yaml 是剧本可自行编写(注意缩进,空格等字符)
- hosts: local
gather_facts: False
# vars:
# key: Ansible
tasks:
- name: display Host Variable from hostfile
debug: msg=" The {{ inventory_hostname }} Value is {{ key }}"
- name: "stop course-situation-center-service"
shell: "ps ax | grep 'course-situation-center-service.jar' | grep java | head -1 | awk '{print $1}'|xargs kill -9"
- name: "clean the directory"
shell: rm -rf /search/odin/daemon/course-situation-center-service/bin/*.jar
- name: "delivery the jar to the remote_host"
copy:
src=/search/odin/daemon/course-situation-center-service/tmp/course-situation-center-service.jar
dest=/search/odin/daemon/course-situation-center-service/bin/
- name: start server
shell: "java -Xmx512m -jar /search/odin/daemon/course-situation-center-service/bin/course-situation-center-service.jar 2>&1 | cronolog /search/odin/daemon/course-situation-center-service/log/course-situation-center-service.%Y-%m-%d.log >> /dev/null &"
/etc/ansible/hosts
[local]
127.0.0.1 key=ansible-host-85
然后就可以点击构建
大功告成,如有问题可留言+q:910206656