deploy_playbook.yml 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. ---
  2. - hosts: all
  3. vars:
  4. deploy_path: /www/wwwroot/xiaoding
  5. backup_dir: /www/wwwroot
  6. timestamp: "{{ ansible_date_time.year }}-{{ ansible_date_time.month }}-{{ ansible_date_time.day }}-{{ ansible_date_time.hour }}:{{ ansible_date_time.minute }}:{{ ansible_date_time.second }}"
  7. backup_file: "xiaoding_backup_{{ timestamp }}.tar.gz"
  8. project_archive: "owl-admin.tar.gz"
  9. tasks:
  10. - name: Create backup of existing deployment
  11. archive:
  12. path: "{{ deploy_path }}"
  13. dest: "{{ backup_dir }}/{{ backup_file }}"
  14. format: gz
  15. ignore_errors: yes
  16. - name: Create local project archive
  17. local_action:
  18. module: archive
  19. path: "{{ playbook_dir }}/../../*"
  20. dest: "/tmp/{{ project_archive }}"
  21. format: gz
  22. exclude_path:
  23. - "*.git"
  24. - "node_modules"
  25. - "vendor"
  26. run_once: true
  27. - name: Ensure deploy directory exists
  28. file:
  29. path: "{{ deploy_path }}"
  30. state: directory
  31. mode: '0755'
  32. - name: Copy project archive to remote
  33. copy:
  34. src: "/tmp/{{ project_archive }}"
  35. dest: "/tmp/{{ project_archive }}"
  36. - name: Clean deploy directory
  37. file:
  38. path: "{{ deploy_path }}"
  39. state: absent
  40. - name: Create deploy directory
  41. file:
  42. path: "{{ deploy_path }}"
  43. state: directory
  44. mode: '0755'
  45. - name: Extract project archive
  46. unarchive:
  47. src: "/tmp/{{ project_archive }}"
  48. dest: "{{ deploy_path }}"
  49. remote_src: yes
  50. - name: Run composer install
  51. command:
  52. cmd: composer install --no-dev
  53. chdir: "{{ deploy_path }}"
  54. - name: Set proper permissions
  55. file:
  56. path: "{{ deploy_path }}"
  57. owner: www-data
  58. group: www-data
  59. recurse: yes
  60. mode: '0755'
  61. - name: Clean up remote archive
  62. file:
  63. path: "/tmp/{{ project_archive }}"
  64. state: absent
  65. - name: Clean up local archive
  66. local_action:
  67. module: file
  68. path: "/tmp/{{ project_archive }}"
  69. state: absent
  70. run_once: true