Jenkins+ansible+Tomcat实现项目远程自动部署

1. Jenkins主机安装ansible并配置主机清单

[root@localhost ~]# vim /etc/ansible/hosts 尾部添加
[tomcat]
192.168.20.120
[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.20.120
[root@localhost ~]# ansible tomcat -m ping
192.168.20.120 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
[root@localhost ~]# which ansible-playbook
/usr/bin/ansible-playbook
[root@localhost ~]# ll /usr/bin/ansible-playbook
lrwxrwxrwx 1 root root 29 Jul 11 14:41 /usr/bin/ansible-playbook -> /usr/bin/ansible-playbook-2.7
[root@localhost test]# chmod +s /usr/bin/ansible-playbook

2. 配置Jenks任务

在这里插入图片描述
在这里插入图片描述
post steps里边的内容是

export BUILD_ID=DONTKILLME
cd /tmp/test
ansible-playbook deploy.yml

编辑脚本及剧本

[root@localhost ~]# mkdir /tmp/test
[root@localhost ~]# cd /tmp/test/
[root@localhost test]# vim wget.sh
#!/bin/bash
wget -O /tmp/gameoflife.war -c http://192.168.20.119:8080/job/game-of-life/lastSuccessfulBuild/artifact/gameoflife-web/target/gameoflife.war
[root@localhost test]# chmod +x wget.sh

[root@localhost test]# vim backup.sh
#!/bin/bash
DIR=`date +%Y%m%d%H%M%S`
DEST=/backup/$DIR
APPBASE=/usr/local/tomcat/webapps/ROOT
mkdir -p $DEST
mv $APPBASE $DEST
mkdir -p $APPBASE

[root@localhost test]# chmod +x backup.sh
[root@localhost test]# vim deploy.yml
- hosts: all
  remote_user: root
  tasks:
    - name: transfer scirpt
      copy: src=/tmp/test/backup.sh dest=/tmp
    - name: run the script
      shell: cd /tmp/ && sh backup.sh
    - name: download war file
      script: wget.sh
    - name: deploy the war file
      unarchive: src=/tmp/gameoflife.war dest=/usr/local/tomcat/webapps/ROOT remote_src=yes
[root@localhost test]# ansible-playbook deploy.yml --syntax-check

playbook: deploy.yml

3. 触发Jenks构建代码

在这里插入图片描述
提示秘钥验证失败,说明Jenks用户还是没有权限执行的,我们切换到Jenks用户,然后做一个免密登陆

[root@localhost ~]# vim /etc/passwd
jenkins:x:988:983:Jenkins Automation Server:/var/lib/jenkins:/bin/bash
[root@localhost ~]# su jenkins
bash-4.2$ exit
exit
[root@localhost ~]# vim .bash_profile 尾部添加
export PS1='[\u@\h \W] '
[root@localhost ~]# source .bash_profile 
[root@localhost ~]# vim /etc/sudoers
root    ALL=(ALL)       ALL
jenkins ALL = NOPASSWD: ALL #新加的

[root@localhost ~] su jenkins
[jenkins@localhost root] ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/var/lib/jenkins/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /var/lib/jenkins/.ssh/id_rsa.
Your public key has been saved in /var/lib/jenkins/.ssh/id_rsa.pub.
The key fingerprint is:
0e:9c:37:ab:c1:2a:4f:c8:a0:4d:d4:9d:06:d4:d0:90 jenkins@localhost
The key's randomart image is:
+--[ RSA 2048]----+
|   .=*           |
|   .Eoo.         |
|  . . +          |
| .   o .         |
|. .   + S        |
|.= . . + o       |
|. + . o o        |
|  .. . o         |
|   oo .          |
+-----------------+
[jenkins@localhost root ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.20.120

4. Tomcat需要做的事

保持开启状态,项目会进行自动更新,无需重启,最后的效果是,项目部署之前是一个页面,项目部署后,刷新一下页面,会出来新的页面

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值