- hosts: '{{hosts}}' remote_user: root vars: src: "/data/wwwroot/{{ pj }}" # 仓库源 tmp: "/data/cache/{{ pj }}/{{ env }}" # 本机备份目录 dest_path: "/data/wwwroot/{{ domain }}/{{ pj }}" # 项目发布路径 url: "https://yaohong_lin:Aa111111...@gz-gitlab.vipthink.cn/" # git地址 tasks: # 获得日期 - name: get deploy time shell: date +%Y-%m-%d-%H%M%S register: date_str run_once: true connection: local # 检查目录有没有创建,如果没有则创建 - name: create local directory if they don't exist file: path: "{{ path }}" state: directory owner: shop group: shop mode: 0775 loop: - "{{ tmp }}" loop_control: loop_var: path connection: local # 处理分支 : refs/master > master - name: Generate branch shell: echo {{ tag }}|awk -F '/' '{print $NF}' register: tag run_once: true connection: local # 拉代码 - name: "pull {{ git }} {{ tag }}" git: repo: "{{ url }}{{ git }}" dest: "{{ src }}" force: yes version: "{{ tag }}" run_once: true connection: local # 检查文件是否存在/如果不存在则退出报错 - name: check sonar-project.properties exist shell: ls /data/wwwroot/{{ pj }}/ ignore_errors: yes connection: local register: result - fail: msg="sonar-project.properties in not exist" when: result.rc != 0 - debug: var=result # 保留最近20个版本 - name: housekeep last 20 rollback release shell: "ls -t|awk 'NR>20'|xargs rm -rf" args: chdir: "{{ tmp }}" ignore_errors: yes connection: local # 打包 jar 包 - name: "mvn clean package" shell: "rm -f target/*.jar && mvn clean package -e -U -Dmaven.test.skip=true" args: chdir: "{{ src }}" run_once: true connection: local - debug: var=result # 复制jar文件到临时目录 - name: copy jar file shell: "mv {{ src }}/target/*.jar {{ tmp }}/{{ pj }}-{{ env }}-{{ tag }}-{{ date_str.stdout }}.jar" run_once: true connection: local # 检查远程目录有没有创建,如果没有则创建 - name: create remote directory if they don't exist file: path: "{{ path }}" state: directory owner: shop group: shop mode: 0775 loop: - "{{ dest_path }}" - /data/logs/java/ - /data/logs/web/ - /data/logs/queue/ loop_control: loop_var: path # 发送jar文件到服务器 - name: rsync jar synchronize: src: "{{ tmp }}/{{ pj }}-{{ env }}-{{ tag }}-{{ date_str.stdout }}.jar" dest: "{{ dest_path }}" partial: yes compress: yes rsync_timeout: 0 rsync_opts: ['--chown=shop:shop','--chmod=ugo=rwX'] perms: no use_ssh_args: no # set_remote_user: no # 保留远端机器最近20个版本 - name: housekeep last 20 rollback release shell: "ls -t *.jar|awk 'NR>20'|xargs rm -rf " args: chdir: "{{ dest_path }}" ignore_errors: yes # 创建软链 - name: create soft link file: #src: "{{ dest_path }}/{{ pj }}-{{ env }}-{{ tag }}-{{ date_str.stdout }}.jar" src: "{{ pj }}-{{ env }}-{{ tag }}-{{ date_str.stdout }}.jar" dest: "{{ dest_path }}/{{ pj }}.jar" owner: shop group: shop state: link force: yes become: yes become_method: sudo become_user: shop # 生成supervisor配置文件 - name: "rsync {{ pj }} supervisord_deploy.ini.j2" template: src: template/supervisord_deploy.ini.j2 dest: /etc/supervisord.d/{{ pj }}.ini # 已存在就不覆盖 force: no notify: supervisorctl update # 立即执行 handlers - meta: flush_handlers # 重启一次 - name: "restart {{ pj }}" shell: "supervisorctl restart {{ pj }}" # 延迟15s,等待 java 启动 - name: sleep 15 shell: sleep 15 # 检查 进程 - name: check_process shell: ps -ef | grep "{{ pj }}" | grep -v "grep" | wc -l register: check_process tags: always - debug: var=check_process - name: "restart service" shell: "supervisorctl update {{ pj }};supervisorctl restart {{ pj }}" - name: "wait for {{ port }}..." wait_for: "port={{ port }} timeout=60" when: env in ["dev","prod","gray","pre"] and pj != 'socket' # 加载supervisor配置文件 handlers: - name: supervisorctl update shell: supervisorctl update # ansible op -m archive -a "path=/data/cache/devops/ format=gz dest=/data/cache/devops/test.tar.gz"