• ansible剧本批量部署jdk+tomcat+jenkins+nginx


    关注 码龄 粉丝数 原力等级 -- 被采纳 被点赞 采纳率 m0_61806533 2024-03-29 20:33 采纳率: 0% 浏览 3 首页/ 运维 / ansible剧本批量部署jdk+tomcat+jenkins+nginx 运维 使用ansible剧本批量部署jdk+tomcat+jenkins+nginx,实现访问nginx就能访问到jenkins服务 收起 写回答 好问题 0 提建议 追加酬金 关注问题 微信扫一扫 点击复制链接 分享 邀请回答 编辑 收藏 删除 结题 收藏 举报 追加酬金 (90%的用户在追加酬金后获得了解决方案) 当前问题酬金 ¥ 0 (可追加 ¥500) ¥ 15¥ 20¥ 50¥ 100¥ 200 支付方式 扫码支付 二维码出错 点击刷新 支付金额 15 元 提供问题酬金的用户不参与问题酬金结算和分配 支付即为同意 《付费问题酬金结算规则》 结题 再想想 删除 再等等

    1条回答 默认 最新

    • 专家-赤兔[在线] 全栈领域优质创作者 2024-03-29 20:33
      关注

      引自免费微信小程序:皆我百晓生

      在 Ansible 的 playbook 中批量部署 Java、Tomcat、Jenkins 和 Nginx 服务器时,可以采用以下步骤实现访问 Nginx 能够访问 Jenkins 服务:

      1. 定义 playbooks

        • base.yml: 定义基础配置文件,如 /etc/ansible/hosts,将服务器及其网络信息映射到 host 模板中。
        • jdk.yml: 分别部署 Java 环境(JDK 8 或更高版本)、Tomcat 和 Nginx 服务器,并设置虚拟主机映射到各个环境的对应端口。
        • jenkins.yml: 同样,定义 Playbook 用于部署 Jenkins 服务,配置服务器环境和端口映射。
        • nginx.yml: 配置 Nginx 服务器,创建一个静态资源服务器并为每个环境设置域名。
      2. 准备模板脚本

        # base.yml
        hosts:
          - myserver.example.com
        
        tasks:
        
           - name: Install JDK 8 or higher
              shell: |
                sudo apt-get update && sudo apt-get install oracle-java8-jdk-oracle-java8-headless -y
        
           - name: Install Tomcat
              shell: |
                sudo apt-get update && sudo apt-get install tomcat7 -y
        
           - name: Install Nginx
              shell: |
                sudo apt-get update && sudo apt-get install nginx -y
        
           - name: Configure Nginx and Java Hosts
              hosts: myserver.example.com
              become: yes
        
              tasks:
        
                 - name: Define Nginx proxy for each environment
                    include_role:
                      remote_user: "jenkins"
                      role_path: /etc/ansible/roles/jenkins.j2 RoleFile::default.j2
                    run_once:
                      command: |
                        sed -i 's//${JAVA_HOSTNAME}/g' /etc/nginx/sites-available/default
        
                 - name: Configure JRE URLs for Java environments
                    include_role:
                      remote_user: "jdk"
                      role_path: /etc/ansible/roles/java.j2 RoleFile::default.j2
                    run_once:
                      command: |
                        sed -i 's/${JAVA_HOSTNAME}/${JAVA_HOST}-u/g' /etc/sysconfig/java/jre/config.xml
        
                 - name: Set domain mapping in Nginx configuration
                    include_role:
                      remote_user: "nginx"
                      role_path: /etc/nginx/sites-available/default
                    run_once:
                      command: |
                        sed -i 's| listen \([0-9]*\.([a-z]{2,4})\)$| server { listen 80; server_name ${JAVA_HOSTNAME}; location /{ proxy_pass http://localhost:8080/jenkins; } } |' /etc/nginx/sites-available/default
        
           - name: Set static content server
              hosts: myserver.example.com
              become: yes
        
              tasks:
        
                 - name: Configure Nginx with static content directory
                    include_role:
                      remote_user: "nginx"
                      role_path: /etc/nginx/sites-available/default
                    run_once:
                      command: |
                        sed -i 's|location /{ proxy_pass http://localhost:8080/ContentServer; default_type application/octet-stream; root /var/www/html; }' /etc/nginx/sites-available/default
        
        # jdk.yml
        hosts:
          - myserver.example.com
        
        tasks:
        
           - name: Install Oracle Java 8 (or higher)
              shell: |
                sudo yum install java-8-openjdk-devel -y
        
           - name: Copy Java environment variables
              template:
                src: <<~EOF
                  [System]
                  JAVA_HOME=/opt/oracle/product/javajdk-${JAVA_VERSION}/bin/java_home
                  PATH=$JAVA_HOME/bin:$PATH
                  CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
                EOF
                dest: /etc/profile.d/java.sh
                owner: root
                group: root
                mode: 0644
        
           - name: Modify .bashrc to set JAVA_HOME
              template:
                src: <<~EOF
                  export JAVA_HOME=/opt/oracle/product/javajdk-${JAVA_VERSION}/bin/java_home
                EOF
                dest: ~/.bashrc
                owner: root
                group: root
                mode: 0644
        
           - name: Set environmental variables
              task:
                uses: sed@https://raw.githubusercontent.com/docker/cli/master/install-sed.sh
                args:
                  input_file: |
                     - /etc/profile.d/java.sh
                     - |
                       # Set JAVA_HOME
                       export JAVA_HOME=/opt/oracle/product/javajdk-${JAVA_VERSION}/bin/java_home
        
                       # Add the following lines to the .bashrc file to start Java 8 or higher
                       # on startup
                       # # Add the path to the jdk executable
                       # export JAVA_PATH=/opt/oracle/product/javajdk-${JAVA_VERSION}/bin/java
                       # source /etc/profile.d/java.sh
        
                       # Initialize the Java Development Kit (JDK) if it's not already installed.
                       echo 'export JAVA_HOME=$JAVA_HOME' >> /etc/profile.d/java.sh
        
                       # Set JAVA_HOME as the system variable
                       export JAVA_HOME
                  env:
                    JAVA_HOME: $JAVA_HOME
                only_if: run_on_os(linux)
        
        # jdk.yml
        hosts:
          - myserver.example.com
        
        tasks:
        
           - name: Update Python dependencies
              shell: |
                pip install --upgrade pip setuptools wheel
        
           - name: Configure virtualenv and pip requirements.txt
              shell: |
                cp requirements.txt .
                python3 -m venv myvirtualenv
                deactivate
                source myvirtualenv/bin/activate
                pip install -r requirements.txt
        
           - name: Install and configure Apache Maven
              shell: |
                cd /path/to/my/maven-repo
                mvn clean install
        
           - name: Copy Apache Maven configuration files
              template:
                src: <<~EOF
                  cp maven-pom.xml myproject/pom.xml
                EOF
                dest: myproject/conf/
                owner: root
                group: root
                mode: 0644
        
           - name: Configure Apache Maven and local project properties
              includes:
                - maven.properties
              template:
                src: <<~EOF
                  cp myproject/maven.properties myproject/settings/local.properties
                  cp myproject/settings/global.properties myproject/settings/local.properties
                EOF
                dest: myproject/conf/
                owner: root
                group: root
                mode: 0644
        
           - name: Create pom.xml file
              shell: |
                cp myproject/settings/local.properties myproject/settings/pom.xml
                rm myproject/settings/local.properties
                cp settings/local.properties myproject/settings/pom.xml
                edit 
                add 
                  artifacts {
                    archives = ['src/main/java', 'src/main/resources']
                  }
                  packaging = jar
                  manifest = main/resources/MANIFEST.MF
                save
                exit 0
              env:
                MAVEN_OPTS: "-Xmx1G -XX:MaxPermSize=512M"
        
        # tomcat.yml
        hosts:
          - myserver.example.com
        
        tasks:
        
           - name: Install Tomcat 7 or higher
              shell: |
                sudo yum install java-tomcat7 -y
        
           - name: Copy Tomcat webapps directory structure
              template:
                src: <<~EOF
                  cp tomcat-webapps /usr/share/tomcat/webapps
                EOF
                dest: /usr/share/tomcat/webapps
                owner: root
                group: root
                mode: 0755
        
           - name: Set tomcat start script and run URL
              shell: |
                #!/bin/bash
                exec $CATALINA_HOME/bin/startup.sh
        
                # Set the URL to access the JNDI system context for the JRE and System VM
                # by using a pre-defined entry point, such as 'java:jndi-context'
                jndiContextPath=$(echo ${JAVA_HOSTNAME} + ":#{PORT}")
                cd /usr/share/tomcat/webapps/myapp/
                bash run-war.sh
                echo "App is running at http://${JAVA_HOSTNAME}:${PORT}/myapp" >> /var/log/tomcat.log
                exit 0
              env:
                JAVA_HOME: /opt/oracle/product/javajdk-${JAVA_VERSION}/bin/java
                PORT: 8080
                JNDI_CONTEXT_PATH: ${jndiContextPath}
        
        # nginx.yml
        hosts:
          - myserver.example.com
        
        tasks:
        
           - name: Install Nginx 1.14 or higher
              shell: |
                sudo yum install openresty-1.14 -y
        
           - name: Copy Nginx configuration files
              template:
                src: <<~EOF
                  cp nginx.conf /etc/nginx/sites-available/
                EOF
                dest: /etc/nginx/sites-available/
                owner: root
                group: root
                mode: 0755
        
           - name: Configure Nginx SSL/TLS and reverse proxy
              includes:
                - ssl.dhparam
                - server_names_hash_algorithm
                - ssl_certificate_key
                - ssl_certificate
                - rewrite_log
                - rewrite
                - error_page 500 502 503 50
        

      展开全部

      评论 编辑记录
      编辑
      预览

      报告相同问题?

    • 相关阅读:
      IO模型介绍(select、poll、epoll)
      2021年中青杯B题港珠澳大桥桥梁设计与安全策略数学建模全过程文档及程序
      2022国赛C题解析
      linux笔记--nohup的使用及设置用户为root用户
      51单片机 矩阵键盘
      矩阵按键简单使用
      自研ORM 子查询&嵌套查询
      老版本Typora强制付费!试试这款开源替代品
      攻破《数组与指针》相关笔试题(二)
      是否应该升级到ChatGPT 4.0?深度对比ChatGPT 3.5与4.0的差异
    • 原文地址:https://ask.csdn.net/questions/8081160