deploy_playbook.yml 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. ---
  2. - name: Deploy Vue3 Frontend Application
  3. hosts: webservers
  4. vars:
  5. app_name: mall-vue3
  6. app_version: "{{ lookup('env', 'APP_VERSION') | default('latest', true) }}"
  7. deploy_path: /usr/share/nginx/html/mall
  8. nginx_config_path: /etc/nginx/sites-available
  9. nginx_enabled_path: /etc/nginx/sites-enabled
  10. backup_path: /usr/share/nginx/html/mall/backup
  11. dist_archive: dist-prod.tar.gz
  12. frontend_path: /usr/share/nginx/html/mall/backendui
  13. tasks:
  14. - name: Compress dist-prod directory locally
  15. local_action:
  16. module: archive
  17. path: ../../dist-prod
  18. dest: ../../{{ dist_archive }}
  19. format: gz
  20. run_once: true
  21. - name: Create deployment directories
  22. ansible.builtin.file:
  23. path: "{{ item }}"
  24. state: directory
  25. mode: "0755"
  26. with_items:
  27. - "{{ deploy_path }}"
  28. - "{{ backup_path }}"
  29. - "{{ frontend_path }}"
  30. become: true
  31. - name: Clean target directory
  32. ansible.builtin.file:
  33. path: "{{ frontend_path }}"
  34. state: directory
  35. mode: "0755"
  36. become: true
  37. - name: Upload compressed dist files
  38. ansible.builtin.copy:
  39. src: ../../{{ dist_archive }}
  40. dest: "{{ deploy_path }}/{{ dist_archive }}"
  41. mode: "0644"
  42. become: true
  43. - name: Extract dist files
  44. ansible.builtin.unarchive:
  45. src: "{{ deploy_path }}/{{ dist_archive }}"
  46. dest: "{{ frontend_path }}"
  47. remote_src: true
  48. become: true
  49. - name: Debug - List directory contents
  50. ansible.builtin.shell: "ls -la {{ frontend_path }}"
  51. register: dir_contents
  52. become: true
  53. - name: Debug - Show directory contents
  54. ansible.builtin.debug:
  55. var: dir_contents.stdout_lines
  56. - name: Remove archive file
  57. ansible.builtin.file:
  58. path: "{{ deploy_path }}/{{ dist_archive }}"
  59. state: absent
  60. become: true
  61. - name: Set correct permissions
  62. ansible.builtin.file:
  63. path: "{{ frontend_path }}"
  64. state: directory
  65. recurse: yes
  66. owner: www-data
  67. group: www-data
  68. mode: "0755"
  69. become: true
  70. - name: Copy nginx configuration
  71. ansible.builtin.template:
  72. src: templates/mall.conf.j2
  73. dest: "{{ nginx_config_path }}/mall.conf"
  74. mode: "0644"
  75. notify: Reload nginx
  76. become: true
  77. - name: Create symlink to enable directory
  78. file:
  79. src: "{{ nginx_config_path }}/mall.conf"
  80. dest: "{{ nginx_enabled_path }}/mall.conf"
  81. state: link
  82. force: yes
  83. become: true
  84. - name: Ensure nginx is running
  85. ansible.builtin.service:
  86. name: nginx
  87. state: started
  88. enabled: true
  89. become: true
  90. handlers:
  91. - name: Reload nginx
  92. ansible.builtin.service:
  93. name: nginx
  94. state: reloaded