使用ansible-playbook部署java业务代码(转)

  1. 背景 一般地,在公司的生产环境中,由于需求变更和代码更新频繁的问题,那么应对措施就是工具化平台化使用到该生产环境中。ansible-playbook可以简单便捷地管理配置服务。
  2. 需求 对于代码部分,一般是git或svn作为代码管理仓库。语言使用java,那么打包则由maven来打包,使用maven私服来管理包。即是先在git下载代码,然后使用mvn来打包,经过传输到生产服务器,然后启动服务,最终检查服务是否成功。

  3. 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
  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: