deploy_playbook.yml 2.1 KB

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