Linux Ansible——通过Ansible实现批量部署jdk+Tomcat+Jenkins

通过Ansible实现批量部署jdk+Tomcat

批量 jdk及Tomcat部署、配置

[root@ansible-server src]# cat tomcat.yml 
- hosts: webserversuser: roottasks:
##配置JDK,上传jdk、tomcat的安装包到/usr/src- name: configure Jdk1.8copy: src=/usr/src/jdk-8u211-linux-x64.tar.gz  dest=/usr/src- name: unzipshell: tar -xvzf /usr/src/jdk-8u211-linux-x64.tar.gz -C /usr/local- name: rename to javashell: mv /usr/local/jdk1.8.0_211 /usr/local/java- name: configure envirement1shell: echo "JAVA_HOME=/usr/local/java" >> /etc/profile- name: configure envirement2shell: echo 'PATH=$JAVA_HOME/bin:$PATH' >> /etc/profile
##Tomcat- name: copy tomcatcopy: src=/usr/src/apache-tomcat-8.5.45.tar.gz dest=/usr/src- name: unzip tomcatshell: tar -xvzf /usr/src/apache-tomcat-8.5.45.tar.gz -C /usr/local- name: rename to tomcatshell: mv /usr/local/apache-tomcat-8.5.45 /usr/local/tomcat- name: copy startup filecopy:  src=/usr/src/startup.sh dest=/usr/local/tomcat/binnotify: start tomcathandlers:- name: start tomcatshell: nohup /usr/local/tomcat/bin/startup.sh &
[root@java-server src]# ls
apache-tomcat-8.5.45         debug                       kernels     tomcat.retry
apache-tomcat-8.5.45.tar.gz  jdk-8u211-linux-x64.tar.gz  startup.sh  tomcat.yml
[root@java-server src]# head -2 startup.sh 
#!/bin/sh
source /etc/profile

**

批量部署Jenkins

描述:
·
1.准备两台机器,一台作为nginx代理。一台为tomcat服务器。
2.tomcat服务器手动部署tomcat服务,并将webapps目录下面的内容提前删掉。
3.将jenkins.war包上传到nginx服务器。通过ansible将war包拷贝过去。并启动tomcat
4.配置nginx反向代理tomcat,实现访问jenkins。

一、tomcat服务器
1.安装jdk与tomcat略。
2.添加tomcat启动脚本中添加环境变量
[root@ansible-web2 ~]# vim /usr/local/tomcat/bin/startup.sh  #需要添加如下内容
source /etc/profile
====================================
二、nginx服务器:
1.安装nginx与ansible,上传jenkins的war包略。
2.ansible配置如下:
3.定义变量:
[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# mkdir vars
[root@ansible ansible]# vim vars/path.yml
src_path: /root/jenkins.war
dest_path: /usr/local/tomcat/webapps/4.配置playbook:
[root@ansible ansible]# vim jenkins.yml
- hosts: webserver2user: rootvars_files:- /etc/ansible/vars/path.ymltasks:- name: copy jenkins.warcopy: src={{ src_path }} dest={{ dest_path }}- name: start tomcatshell: nohup /usr/local/tomcat/bin/startup.sh &
[root@ansible ansible]# ansible-playbook jenkins.yml5.配置nginx反向代理
[root@ansible ansible]# vim /etc/nginx/conf.d/jenkins.conf
server {listen       80;server_name  localhost;charset koi8-r;access_log  /var/log/nginx/host.access.log  main;location /jenkins {proxy_pass http://192.168.62.181:8080;proxy_set_header Host $host:$server_port;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}6.启动nginx
7.检查nginx与tomcat是否启动成功!
8.访问nginx服务器http://ip/jenkins。

批量部署Jdk+Tomcat+Jenkins

将Jdk、Tomcat、Jenkins的安装包上传到ansbile控制节点的/usr/src下

[root@java-server ansible]# head -2 /usr/src/startup.sh 	//startup.sh是tomcat的启动脚本
#!/bin/sh
source /etc/profile    #加上此行,是为了启动加载到环境变量

变量文件

[root@ansible ansible]# cat /etc/ansible/vars/file.yml

在这里插入图片描述

playbook剧本

[root@ansible ansible]# cat jenkins.yml
- hosts: ansible-web1user: rootvars_files:- /etc/ansible/vars/file.ymltasks:
##配置JDK,上传jdk、tomcat的安装包到/usr/src- name: configure JDK1.8copy: src={{ src_jdk_path }}  dest={{ dest_jdk_path }}- name: unzip JDKshell: tar -xvzf /usr/src/jdk-8u211-linux-x64.tar.gz -C /usr/local- name: rename to javashell: mv /usr/local/jdk1.8.0_211 /usr/local/java- name: configure JDK envirement1shell: echo "JAVA_HOME=/usr/local/java" >> /etc/profile- name: configure JDK envirement2shell: echo 'PATH=$JAVA_HOME/bin:$PATH' >> /etc/profile
##Tomcat- name: copy tomcatcopy: src={{ src_tomcat_path }} dest={{ dest_tomcat_path }}- name: unzip tomcatshell: tar -xvzf /usr/src/apache-tomcat-8.5.45.tar.gz -C /usr/local- name: rename to tomcatshell: mv /usr/local/apache-tomcat-8.5.45 /usr/local/tomcat- name: copy startup filecopy: src=/usr/src/startup.sh dest=/usr/local/tomcat/bin
##Jenkins- name: copy jenkinscopy: src=/usr/src/jenkins.war  dest=/usr/local/tomcat/webapps/notify: start jenkinshandlers:- name: start jenkinsshell: nohup /usr/local/tomcat/bin/startup.sh &


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部