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

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

批量 jdk及Tomcat部署、配置

[root@ansible-server src]# cat tomcat.yml 
- hosts: webservers
  user: root
  tasks:
##配置JDK,上传jdk、tomcat的安装包到/usr/src
  - name: configure Jdk1.8
    copy: src=/usr/src/jdk-8u211-linux-x64.tar.gz  dest=/usr/src
  - name: unzip
    shell: tar -xvzf /usr/src/jdk-8u211-linux-x64.tar.gz -C /usr/local
  - name: rename to java
    shell: mv /usr/local/jdk1.8.0_211 /usr/local/java
  - name: configure envirement1
    shell: echo "JAVA_HOME=/usr/local/java" >> /etc/profile
  - name: configure envirement2
    shell: echo 'PATH=$JAVA_HOME/bin:$PATH' >> /etc/profile
##Tomcat
  - name: copy tomcat
    copy: src=/usr/src/apache-tomcat-8.5.45.tar.gz dest=/usr/src
  - name: unzip tomcat
    shell: tar -xvzf /usr/src/apache-tomcat-8.5.45.tar.gz -C /usr/local
  - name: rename to tomcat
    shell: mv /usr/local/apache-tomcat-8.5.45 /usr/local/tomcat
  - name: copy startup file
    copy:  src=/usr/src/startup.sh dest=/usr/local/tomcat/bin
    notify: start tomcat
  handlers:
  - name: start tomcat
    shell: 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: webserver2
  user: root
  vars_files:
   - /etc/ansible/vars/path.yml
  tasks:
  - name: copy jenkins.war
    copy: src={{ src_path }} dest={{ dest_path }}
  - name: start tomcat
    shell: nohup /usr/local/tomcat/bin/startup.sh &
[root@ansible ansible]# ansible-playbook jenkins.yml

5.配置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

Linux Ansible——通过Ansible实现批量部署jdk+Tomcat+Jenkins_第1张图片

playbook剧本

[root@ansible ansible]# cat jenkins.yml
- hosts: ansible-web1
  user: root
  vars_files:
  - /etc/ansible/vars/file.yml
  tasks:
##配置JDK,上传jdk、tomcat的安装包到/usr/src
  - name: configure JDK1.8
    copy: src={{ src_jdk_path }}  dest={{ dest_jdk_path }}
  - name: unzip JDK
    shell: tar -xvzf /usr/src/jdk-8u211-linux-x64.tar.gz -C /usr/local
  - name: rename to java
    shell: mv /usr/local/jdk1.8.0_211 /usr/local/java
  - name: configure JDK envirement1
    shell: echo "JAVA_HOME=/usr/local/java" >> /etc/profile
  - name: configure JDK envirement2
    shell: echo 'PATH=$JAVA_HOME/bin:$PATH' >> /etc/profile
##Tomcat
  - name: copy tomcat
    copy: src={{ src_tomcat_path }} dest={{ dest_tomcat_path }}
  - name: unzip tomcat
    shell: tar -xvzf /usr/src/apache-tomcat-8.5.45.tar.gz -C /usr/local
  - name: rename to tomcat
    shell: mv /usr/local/apache-tomcat-8.5.45 /usr/local/tomcat
  - name: copy startup file
    copy: src=/usr/src/startup.sh dest=/usr/local/tomcat/bin
##Jenkins
  - name: copy jenkins
    copy: src=/usr/src/jenkins.war  dest=/usr/local/tomcat/webapps/
    notify: start jenkins
  handlers:
  - name: start jenkins
    shell: nohup /usr/local/tomcat/bin/startup.sh &

你可能感兴趣的:(Linux,中阶——企业应用实战,云计算初阶-Linux运维实战,云计算初阶-Linux入门,tomcat,jenkins,nginx反向代理,运维,云计算)