manage_playbook.yml 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. ---
  2. - name: Manage Vue3 Frontend Application
  3. hosts: webservers
  4. vars:
  5. app_name: mall-vue3
  6. deploy_path: /usr/share/nginx/html/mall
  7. nginx_config_path: /etc/nginx/conf.d
  8. backup_path: /usr/share/nginx/html/mall/backup
  9. nginx_access_log: /var/log/nginx/mall_access.log
  10. nginx_error_log: /var/log/nginx/mall_error.log
  11. max_backups: 5
  12. tasks:
  13. - name: Check nginx status
  14. ansible.builtin.command: systemctl status nginx
  15. register: nginx_status
  16. when: action is defined and action == "status"
  17. - name: Display nginx status
  18. ansible.builtin.debug:
  19. var: nginx_status.stdout_lines
  20. when: action is defined and action == "status"
  21. - name: Reload nginx configuration
  22. ansible.builtin.service:
  23. name: nginx
  24. state: reloaded
  25. become: yes
  26. when: action is defined and action == "reload"
  27. - name: View nginx access logs
  28. ansible.builtin.command: "tail -n 100 {{ nginx_access_log }}"
  29. register: access_logs
  30. become: yes
  31. when: action is defined and action == "access_logs"
  32. - name: Display access logs
  33. ansible.builtin.debug:
  34. var: access_logs.stdout_lines
  35. when: action is defined and action == "access_logs"
  36. - name: View nginx error logs
  37. ansible.builtin.command: "tail -n 100 {{ nginx_error_log }}"
  38. register: error_logs
  39. become: yes
  40. when: action is defined and action == "error_logs"
  41. - name: Display error logs
  42. ansible.builtin.debug:
  43. var: error_logs.stdout_lines
  44. when: action is defined and action == "error_logs"
  45. - name: List backups
  46. ansible.builtin.find:
  47. paths: "{{ backup_path }}"
  48. patterns: "backup_*.tar.gz"
  49. register: backup_files
  50. when: action is defined and action == "list_backups"
  51. - name: Display backup files
  52. ansible.builtin.debug:
  53. var: backup_files.files
  54. when: action is defined and action == "list_backups"
  55. - name: Clean old backups
  56. ansible.builtin.shell: |
  57. cd {{ backup_path }} && \
  58. ls -t backup_*.tar.gz | tail -n +{{ max_backups + 1 }} | xargs -r rm --
  59. become: yes
  60. when: action is defined and action == "clean_backups"
  61. - name: Rollback to previous version
  62. block:
  63. - name: Find latest backup
  64. ansible.builtin.shell: "ls -t {{ backup_path }}/backup_*.tar.gz | head -n1"
  65. register: latest_backup
  66. - name: Extract backup
  67. ansible.builtin.unarchive:
  68. src: "{{ latest_backup.stdout }}"
  69. dest: "{{ deploy_path }}"
  70. remote_src: yes
  71. notify: reload nginx
  72. become: yes
  73. when: action is defined and action == "rollback"
  74. handlers:
  75. - name: reload nginx
  76. ansible.builtin.service:
  77. name: nginx
  78. state: reloaded
  79. become: yes