deploy_playbook.yml 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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: Remove existing frontend directory
  44. ansible.builtin.file:
  45. path: '{{ frontend_path }}/dist-prod'
  46. state: absent
  47. become: true
  48. - name: Extract dist files
  49. ansible.builtin.unarchive:
  50. src: '{{ deploy_path }}/{{ dist_archive }}'
  51. dest: '{{ frontend_path }}'
  52. remote_src: true
  53. become: true
  54. - name: Debug - List directory contents
  55. ansible.builtin.shell: 'ls -la {{ frontend_path }}'
  56. register: dir_contents
  57. become: true
  58. - name: Debug - Show directory contents
  59. ansible.builtin.debug:
  60. var: dir_contents.stdout_lines
  61. - name: Remove archive file
  62. ansible.builtin.file:
  63. path: '{{ deploy_path }}/{{ dist_archive }}'
  64. state: absent
  65. become: true
  66. - name: Set correct permissions
  67. ansible.builtin.file:
  68. path: '{{ frontend_path }}'
  69. state: directory
  70. recurse: yes
  71. owner: www-data
  72. group: www-data
  73. mode: '0755'
  74. become: true
  75. - name: Copy nginx configuration
  76. ansible.builtin.template:
  77. src: templates/malladmin.conf.j2
  78. dest: '{{ nginx_config_path }}/malladmin.conf'
  79. mode: '0644'
  80. notify: Reload nginx
  81. become: true
  82. - name: Create symlink to enable directory
  83. file:
  84. src: '{{ nginx_config_path }}/malladmin.conf'
  85. dest: '{{ nginx_enabled_path }}/malladmin.conf'
  86. state: link
  87. force: yes
  88. become: true
  89. - name: Ensure nginx is running
  90. ansible.builtin.service:
  91. name: nginx
  92. state: started
  93. enabled: true
  94. become: true
  95. handlers:
  96. - name: Reload nginx
  97. ansible.builtin.service:
  98. name: nginx
  99. state: reloaded