关于使用ansible-playbook部署java业务代码

文章目录

[隐藏]

  • 01. 背景
  • 02. 需求
  • 03. ansible-playbook内容
  • 04. 简单解释
01. 背景

一般地,在公司的生产环境中,由于需求变更和代码更新频繁的问题,那么应对措施就是工具化平台化使用到该生产环境中。ansible-playbook可以简单便捷地管理配置服务。

02. 需求

对于代码部分,一般是git或svn作为代码管理仓库。语言使用java,那么打包则由maven来打包,使用maven私服来管理包。即是先在git下载代码,然后使用mvn来打包,经过传输到生产服务器,然后启动服务,最终检查服务是否成功。

03. ansible-playbook内容
---  - name: Local ops    hosts: 127.0.0.1    remote_user: mai    become: yes    become_user: root    gather_facts: False    vars:    - super_user: mai    - git_user: autouser    - git_passwd: 123456    - project_name: testauto    - project_gitlocal_dir: /tmp/logstest/{{ project_name }}    tasks:    - name: Delete {{ project_name }} code.      file:        path: '{{ project_gitlocal_dir }}'        state: absent      - name: Downloading git code.      git:        repo: https://{{ git_user | urlencode }}:{{ git_passwd | urlencode}}@git.mairoot.com/test/{{ project_name }}.git        version: develop        dest: '{{ project_gitlocal_dir }}'        force: yes      - name: Mvn make pack.      shell: cd {{ project_gitlocal_dir }}; /data/apps/apache-maven-3.5.4-java8/bin/mvn clean package -Pdev      - name: Remote ops    hosts: testhost    remote_user: mai    become: yes    become_user: root    gather_facts: False    vars:    - project_name: testauto    - project_api_name: testautoapi    - project_gitlocal_dir: /tmp/logstest/{{ project_name }}    - project_api_dir_name: /data/apps/backend/{{ project_api_name }}    - run_user: testauto    - run_group: suwe    tasks:    - name: Mkdir {{ project_name }}      file:        path: '{{ project_api_dir_name }}'        state: directory        owner: '{{ run_user }}'        group: '{{ run_group }}'        mode: 0755      - name: Copy file to remote install.      copy:        src: "{{ item.src }}"        dest: "{{ item.dest }}"      with_items:      - { src: '{{ project_gitlocal_dir }}/testauto-api/target/lib', dest: '{{ project_api_dir_name }}' }      - { src: '{{ project_gitlocal_dir }}/testauto-api/target/testauto.jar', dest: '{{ project_api_dir_name }}' }      - name: Isn't docker-java or not.      shell: docker ps -a| grep {{ project_api_name }}      register: docker_{{ project_api_name }}_value      ignore_errors: True      - name: Create and Start docker-java server      when: docker_{{ project_api_name }}_value | failed      shell: docker run -itd -p 8089:8080 --name {{ project_api_name }} -v {{ project_api_dir_name }}:/app -v {{ project_api_dir_name }}/logs:/logs java:8 /bin/bash -c 'java -jar -Dspring.profiles.active=prod -XX:+HeapDumpOnOutOfMemoryError -XX:+UseParNewGC -XX:NewSize=16G -XX:MaxPermSize=16G -Xms16G -Xmx16G -server -Duser.timezone=Asia/Shanghai -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8080 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=172.17.0.1 /app/testauto.jar'      - name: Start docker-java server      when: docker_{{ project_api_name }}_value | succeeded      shell: docker restart {{ project_api_name }}      - name: Local ops    hosts: 127.0.0.1    remote_user: mai    become: yes    become_user: root    gather_facts: False    vars:    - project_name: testauto    - project_gitlocal_dir: /tmp/logstest/{{ project_name }}    tasks:    - name: Delete testauto code.      file:        path: '{{ project_gitlocal_dir }}'        state: absent  
04. 简单解释

简单解析上面内容,主要应用到的是可以把git密码保存到文件中,一个ansible-playbook里有多个play,一个play里可以多个tasks。

原文出处:mairoot -> https://www.mairoot.com/?p=2158

本站所发布的一切资源仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如果侵犯你的利益,请发送邮箱到 [email protected],我们会很快的为您处理。
超哥软件库 » 关于使用ansible-playbook部署java业务代码