|
@@ -0,0 +1,92 @@
|
|
|
+---
|
|
|
+- name: Manage Vue3 Frontend Application
|
|
|
+ hosts: webservers
|
|
|
+ vars:
|
|
|
+ app_name: mall-vue3
|
|
|
+ deploy_path: /usr/share/nginx/html/mall
|
|
|
+ nginx_config_path: /etc/nginx/conf.d
|
|
|
+ backup_path: /usr/share/nginx/html/mall/backup
|
|
|
+ nginx_access_log: /var/log/nginx/mall_access.log
|
|
|
+ nginx_error_log: /var/log/nginx/mall_error.log
|
|
|
+ max_backups: 5
|
|
|
+
|
|
|
+ tasks:
|
|
|
+ - name: Check nginx status
|
|
|
+ ansible.builtin.command: systemctl status nginx
|
|
|
+ register: nginx_status
|
|
|
+ when: action is defined and action == "status"
|
|
|
+
|
|
|
+ - name: Display nginx status
|
|
|
+ ansible.builtin.debug:
|
|
|
+ var: nginx_status.stdout_lines
|
|
|
+ when: action is defined and action == "status"
|
|
|
+
|
|
|
+ - name: Reload nginx configuration
|
|
|
+ ansible.builtin.service:
|
|
|
+ name: nginx
|
|
|
+ state: reloaded
|
|
|
+ become: yes
|
|
|
+ when: action is defined and action == "reload"
|
|
|
+
|
|
|
+ - name: View nginx access logs
|
|
|
+ ansible.builtin.command: "tail -n 100 {{ nginx_access_log }}"
|
|
|
+ register: access_logs
|
|
|
+ become: yes
|
|
|
+ when: action is defined and action == "access_logs"
|
|
|
+
|
|
|
+ - name: Display access logs
|
|
|
+ ansible.builtin.debug:
|
|
|
+ var: access_logs.stdout_lines
|
|
|
+ when: action is defined and action == "access_logs"
|
|
|
+
|
|
|
+ - name: View nginx error logs
|
|
|
+ ansible.builtin.command: "tail -n 100 {{ nginx_error_log }}"
|
|
|
+ register: error_logs
|
|
|
+ become: yes
|
|
|
+ when: action is defined and action == "error_logs"
|
|
|
+
|
|
|
+ - name: Display error logs
|
|
|
+ ansible.builtin.debug:
|
|
|
+ var: error_logs.stdout_lines
|
|
|
+ when: action is defined and action == "error_logs"
|
|
|
+
|
|
|
+ - name: List backups
|
|
|
+ ansible.builtin.find:
|
|
|
+ paths: "{{ backup_path }}"
|
|
|
+ patterns: "backup_*.tar.gz"
|
|
|
+ register: backup_files
|
|
|
+ when: action is defined and action == "list_backups"
|
|
|
+
|
|
|
+ - name: Display backup files
|
|
|
+ ansible.builtin.debug:
|
|
|
+ var: backup_files.files
|
|
|
+ when: action is defined and action == "list_backups"
|
|
|
+
|
|
|
+ - name: Clean old backups
|
|
|
+ ansible.builtin.shell: |
|
|
|
+ cd {{ backup_path }} && \
|
|
|
+ ls -t backup_*.tar.gz | tail -n +{{ max_backups + 1 }} | xargs -r rm --
|
|
|
+ become: yes
|
|
|
+ when: action is defined and action == "clean_backups"
|
|
|
+
|
|
|
+ - name: Rollback to previous version
|
|
|
+ block:
|
|
|
+ - name: Find latest backup
|
|
|
+ ansible.builtin.shell: "ls -t {{ backup_path }}/backup_*.tar.gz | head -n1"
|
|
|
+ register: latest_backup
|
|
|
+
|
|
|
+ - name: Extract backup
|
|
|
+ ansible.builtin.unarchive:
|
|
|
+ src: "{{ latest_backup.stdout }}"
|
|
|
+ dest: "{{ deploy_path }}"
|
|
|
+ remote_src: yes
|
|
|
+ notify: reload nginx
|
|
|
+ become: yes
|
|
|
+ when: action is defined and action == "rollback"
|
|
|
+
|
|
|
+ handlers:
|
|
|
+ - name: reload nginx
|
|
|
+ ansible.builtin.service:
|
|
|
+ name: nginx
|
|
|
+ state: reloaded
|
|
|
+ become: yes
|