引自免费微信小程序:皆我百晓生
在 Ansible 的 playbook 中批量部署 Java、Tomcat、Jenkins 和 Nginx 服务器时,可以采用以下步骤实现访问 Nginx 能够访问 Jenkins 服务:
-
定义 playbooks:
base.yml: 定义基础配置文件,如/etc/ansible/hosts,将服务器及其网络信息映射到 host 模板中。jdk.yml: 分别部署 Java 环境(JDK 8 或更高版本)、Tomcat 和 Nginx 服务器,并设置虚拟主机映射到各个环境的对应端口。jenkins.yml: 同样,定义 Playbook 用于部署 Jenkins 服务,配置服务器环境和端口映射。nginx.yml: 配置 Nginx 服务器,创建一个静态资源服务器并为每个环境设置域名。
-
准备模板脚本:
# 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
