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任务

Jenkins+ansible+Tomcat实现项目远程自动部署
Jenkins+ansible+Tomcat实现项目远程自动部署
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构建代码

Jenkins+ansible+Tomcat实现项目远程自动部署
提示秘钥验证失败,说明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需要做的事

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

上一篇:ansible(playbook)批量自动化部署nginx


下一篇:The year of 2019,Good Good Study Linux,Day Day UP